From ebb84f1f01396cf75e7fb70bb0e2987bbc0b14b2 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 10 Aug 2018 13:14:27 -0400 Subject: 30 --- .../28/backend/src/resolvers/Mutation.js | 95 ++++++++++++++++++++++ stepped-solutions/28/backend/src/schema.graphql | 17 ++++ stepped-solutions/28/frontend/components/Nav.js | 36 ++++++++ stepped-solutions/28/frontend/components/Signin.js | 76 +++++++++++++++++ stepped-solutions/28/frontend/components/Signup.js | 86 ++++++++++++++++++++ stepped-solutions/28/frontend/components/User.js | 27 ++++++ stepped-solutions/28/frontend/pages/signup.js | 18 ++++ 7 files changed, 355 insertions(+) create mode 100755 stepped-solutions/28/backend/src/resolvers/Mutation.js create mode 100755 stepped-solutions/28/backend/src/schema.graphql create mode 100755 stepped-solutions/28/frontend/components/Nav.js create mode 100755 stepped-solutions/28/frontend/components/Signin.js create mode 100755 stepped-solutions/28/frontend/components/Signup.js create mode 100755 stepped-solutions/28/frontend/components/User.js create mode 100755 stepped-solutions/28/frontend/pages/signup.js (limited to 'stepped-solutions/28') diff --git a/stepped-solutions/28/backend/src/resolvers/Mutation.js b/stepped-solutions/28/backend/src/resolvers/Mutation.js new file mode 100755 index 0000000..7bcbb9c --- /dev/null +++ b/stepped-solutions/28/backend/src/resolvers/Mutation.js @@ -0,0 +1,95 @@ +const bcrypt = require('bcryptjs'); +const jwt = require('jsonwebtoken'); + +const Mutations = { + async createItem(parent, args, ctx, info) { + // TODO: Check if they are logged in + + const item = await ctx.db.mutation.createItem( + { + data: { + ...args, + }, + }, + info + ); + + console.log(item); + + return item; + }, + updateItem(parent, args, ctx, info) { + // first take a copy of the updates + const updates = { ...args }; + // remove the ID from the updates + delete updates.id; + // run the update method + return ctx.db.mutation.updateItem( + { + data: updates, + where: { + id: args.id, + }, + }, + info + ); + }, + async deleteItem(parent, args, ctx, info) { + const where = { id: args.id }; + // 1. find the item + const item = await ctx.db.query.item({ where }, `{ id title}`); + // 2. Check if they own that item, or have the permissions + // TODO + // 3. Delete it! + return ctx.db.mutation.deleteItem({ where }, info); + }, + async signup(parent, args, ctx, info) { + // lowercase their email + args.email = args.email.toLowerCase(); + // hash their password + const password = await bcrypt.hash(args.password, 10); + // create the user in the database + const user = await ctx.db.mutation.createUser( + { + data: { + ...args, + password, + permissions: { set: ['USER'] }, + }, + }, + info + ); + // create the JWT token for them + const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET); + // We set the jwt as a cookie on the response + ctx.response.cookie('token', token, { + httpOnly: true, + maxAge: 1000 * 60 * 60 * 24 * 365, // 1 year cookie + }); + // Finalllllly we return the user to the browser + return user; + }, + async signin(parent, { email, password }, ctx, info) { + // 1. check if there is a user with that email + const user = await ctx.db.query.user({ where: { email } }); + if (!user) { + throw new Error(`No such user found for email ${email}`); + } + // 2. Check if their password is correct + const valid = await bcrypt.compare(password, user.password); + if (!valid) { + throw new Error('Invalid Password!'); + } + // 3. generate the JWT Token + const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET); + // 4. Set the cookie with the token + ctx.response.cookie('token', token, { + httpOnly: true, + maxAge: 1000 * 60 * 60 * 24 * 365, + }); + // 5. Return the user + return user; + }, +}; + +module.exports = Mutations; diff --git a/stepped-solutions/28/backend/src/schema.graphql b/stepped-solutions/28/backend/src/schema.graphql new file mode 100755 index 0000000..a8ad765 --- /dev/null +++ b/stepped-solutions/28/backend/src/schema.graphql @@ -0,0 +1,17 @@ +# import * from './generated/prisma.graphql' + +type Mutation { + createItem(title: String, description: String, price: Int, image: String, largeImage: String): Item! + updateItem(id: ID!, title: String, description: String, price: Int): Item! + deleteItem(id: ID!): Item + signup(email: String!, password: String!, name: String!): User! + signin(email: String!, password: String!): User! +} + +type Query { + items(where: ItemWhereInput, orderBy: ItemOrderByInput, skip: Int, first: Int): [Item]! + item(where: ItemWhereUniqueInput!): Item + itemsConnection(where: ItemWhereInput): ItemConnection! + me: User + +} diff --git a/stepped-solutions/28/frontend/components/Nav.js b/stepped-solutions/28/frontend/components/Nav.js new file mode 100755 index 0000000..28e94d3 --- /dev/null +++ b/stepped-solutions/28/frontend/components/Nav.js @@ -0,0 +1,36 @@ +import Link from 'next/link'; +import NavStyles from './styles/NavStyles'; +import User from './User'; + +const Nav = () => ( + + {({ data: { me } }) => ( + + + Shop + + {me && ( + <> + + Sell + + + Orders + + + Account + + + )} + {!me && ( + + Sign In + + + )} + + )} + +); + +export default Nav; diff --git a/stepped-solutions/28/frontend/components/Signin.js b/stepped-solutions/28/frontend/components/Signin.js new file mode 100755 index 0000000..4fd3013 --- /dev/null +++ b/stepped-solutions/28/frontend/components/Signin.js @@ -0,0 +1,76 @@ +import React, { Component } from 'react'; +import { Mutation } from 'react-apollo'; +import gql from 'graphql-tag'; +import Form from './styles/Form'; +import Error from './ErrorMessage'; +import { CURRENT_USER_QUERY } from './User'; + +const SIGNIN_MUTATION = gql` + mutation SIGNIN_MUTATION($email: String!, $password: String!) { + signin(email: $email, password: $password) { + id + email + name + } + } +`; + +class Signin extends Component { + state = { + name: '', + password: '', + email: '', + }; + saveToState = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + render() { + return ( + + {(signup, { error, loading }) => ( +
{ + e.preventDefault(); + await signup(); + this.setState({ name: '', email: '', password: '' }); + }} + > +
+

Sign into your account

+ + + + + +
+
+ )} +
+ ); + } +} + +export default Signin; diff --git a/stepped-solutions/28/frontend/components/Signup.js b/stepped-solutions/28/frontend/components/Signup.js new file mode 100755 index 0000000..3e59a2b --- /dev/null +++ b/stepped-solutions/28/frontend/components/Signup.js @@ -0,0 +1,86 @@ +import React, { Component } from 'react'; +import { Mutation } from 'react-apollo'; +import gql from 'graphql-tag'; +import Form from './styles/Form'; +import Error from './ErrorMessage'; +import { CURRENT_USER_QUERY } from './User'; + +const SIGNUP_MUTATION = gql` + mutation SIGNUP_MUTATION($email: String!, $name: String!, $password: String!) { + signup(email: $email, name: $name, password: $password) { + id + email + name + } + } +`; + +class Signup extends Component { + state = { + name: '', + password: '', + email: '', + }; + saveToState = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + render() { + return ( + + {(signup, { error, loading }) => ( +
{ + e.preventDefault(); + await signup(); + this.setState({ name: '', email: '', password: '' }); + }} + > +
+

Sign Up for An Account

+ + + + + + +
+
+ )} +
+ ); + } +} + +export default Signup; diff --git a/stepped-solutions/28/frontend/components/User.js b/stepped-solutions/28/frontend/components/User.js new file mode 100755 index 0000000..af074c0 --- /dev/null +++ b/stepped-solutions/28/frontend/components/User.js @@ -0,0 +1,27 @@ +import { Query } from 'react-apollo'; +import gql from 'graphql-tag'; +import PropTypes from 'prop-types'; + +const CURRENT_USER_QUERY = gql` + query { + me { + id + email + name + permissions + } + } +`; + +const User = props => ( + + {payload => console.log(payload) || props.children(payload)} + +); + +User.PropTypes = { + children: PropTypes.func.isRequired, +}; + +export default User; +export { CURRENT_USER_QUERY }; diff --git a/stepped-solutions/28/frontend/pages/signup.js b/stepped-solutions/28/frontend/pages/signup.js new file mode 100755 index 0000000..28ed2f4 --- /dev/null +++ b/stepped-solutions/28/frontend/pages/signup.js @@ -0,0 +1,18 @@ +import Signup from '../components/Signup'; +import Signin from '../components/Signin'; +import styled from 'styled-components'; + +const Columns = styled.div` + display: grid; + grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); + grid-gap: 20px; +`; + +const SignupPage = props => ( + + + + +); + +export default SignupPage; -- cgit v1.3