Skip to content

Commit

Permalink
addUser mutation, Modify dashboard for editor routes, redesig login s…
Browse files Browse the repository at this point in the history
…ystem to hash passwords
  • Loading branch information
elevenpassin committed Jan 9, 2018
1 parent 80fd0a9 commit b353c7c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 23 deletions.
21 changes: 17 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const mongoose = require('mongoose');
const { ObjectID } = require('mongodb');
const assert = require('assert');
const bodyParser = require('body-parser');
const { promisify } = require('util');
const credential = require('credential')(),
phash = promisify(credential.hash),
pverify = promisify(credential.verify);
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');

Expand Down Expand Up @@ -44,6 +48,15 @@ const resolvers = {
getPost: async (_, { postid }) => await Post.findOne({ _id: postid })
},
Mutation: {
addUser: async (_, { name, password }) => {
const hashedPassword = await phash(password);
console.log("hassed it", hashedPassword);
const newUser = new User({ name, password: hashedPassword });
return newUser.save((err, user) => {
if (err) console.error(err);
return user;
})
},
addPost: async (_, { title, userid, content }) => {
const newPost = new Post({ title, userid, content })
return newPost.save((err, post) => {
Expand Down Expand Up @@ -72,11 +85,11 @@ app.use(bodyParser.urlencoded({ extended: false }));

app.post('/login', async (req, res) => {
console.log('Got user information: \n', req.body);
const { username, password } = req.body;

try {
const userData = await User.findOne({ name: username, password });
if (userData.password === password) {
const { username, password } = req.body;
const userData = await User.findOne({ name: username });
const hashedPassword = await pverify(userData.password, password);
if (hashedPassword) {
res.send(JSON.stringify({
auth: true
}));
Expand Down
56 changes: 37 additions & 19 deletions client/src/pages/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
import React, { Component } from 'react';
import { Col, Row } from 'antd';
import React, { Component, Fragment } from "react";
import { Col, Row, Button } from "antd";
import { Link, Route } from "react-router-dom";

import ShortBlogpostContainer from '../components/ShortBlogpostContainer.jsx';
import ShortBlogpostContainer from "../components/ShortBlogpostContainer.jsx";

// Subpages
import Editor from './Editor.jsx';

const UserDashboard = (
<Fragment>
<Row style={{ background: "#ECECEC", padding: "30px" }}>
<Col span={24}>
<h1>Dashboard goes here</h1>
</Col>
</Row>
<Row style={{ padding: "30px" }}>
<Row>
<Col span={12}>
<h1>Posts</h1>
</Col>
<Col offset={10} span={2}>
<Link to="/new/post">
<Button>Add Post</Button>
</Link>
</Col>
</Row>
<Row>
<Col>
<ShortBlogpostContainer />
</Col>
</Row>
</Row>
</Fragment>
);

export default class Dashboard extends Component {
render() {
return (
<div>
<Row style={{ background: '#ECECEC', padding: '30px' }}>
<Col span={24}>
<h1>
Dashboard goes here
</h1>
</Col>
</Row>
<Row style={{ padding: '30px' }}>
<h1>Posts</h1>
<Row>
<Col span={12}>
<ShortBlogpostContainer />
</Col>
</Row>
</Row>
<Route path="/new/post" component={Editor} />
<Route path="/" render={() => UserDashboard} />
</div>
)
);
}
}
28 changes: 28 additions & 0 deletions client/src/pages/Editor.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { Component } from 'react';
import { Col, Row } from 'antd';

class Editor extends Component {
constructor(props) {
super(props);
this.state = {

}
}

render () {
return (
<div>
<Row>
<Col span={12}>
Editor goes here
</Col>
<Col span={12}>
Preview goes here
</Col>
</Row>
</div>
)
}
}

export default Editor;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"apollo-server-express": "^1.3.2",
"body-parser": "^1.18.2",
"credential": "^2.0.0",
"dotenv": "^4.0.0",
"express": "^4.16.2",
"graphql": "^0.12.3",
Expand Down
1 change: 1 addition & 0 deletions typeDefs.gql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = `
}

type Mutation {
addUser(name: String!, password: String!): User
addPost(title: String, userid: String!, content: String): Post
deletePost(postid: String): Int
}
Expand Down

0 comments on commit b353c7c

Please sign in to comment.