diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-08-10 13:14:27 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-08-10 13:14:27 -0400 |
| commit | ebb84f1f01396cf75e7fb70bb0e2987bbc0b14b2 (patch) | |
| tree | 17030a43e9d6a617f8ab269195823096dce071ea /stepped-solutions/27 | |
| parent | 6daab5e176382954d2229d8a9251ae819572a29e (diff) | |
30
Diffstat (limited to 'stepped-solutions/27')
| -rwxr-xr-x | stepped-solutions/27/backend/src/index.js | 33 | ||||
| -rwxr-xr-x | stepped-solutions/27/backend/src/resolvers/Query.js | 21 | ||||
| -rwxr-xr-x | stepped-solutions/27/backend/src/schema.graphql | 15 | ||||
| -rwxr-xr-x | stepped-solutions/27/frontend/components/Nav.js | 32 | ||||
| -rwxr-xr-x | stepped-solutions/27/frontend/components/User.js | 27 |
5 files changed, 128 insertions, 0 deletions
diff --git a/stepped-solutions/27/backend/src/index.js b/stepped-solutions/27/backend/src/index.js new file mode 100755 index 0000000..d95ec58 --- /dev/null +++ b/stepped-solutions/27/backend/src/index.js @@ -0,0 +1,33 @@ +const cookieParser = require('cookie-parser'); +const jwt = require('jsonwebtoken'); + +require('dotenv').config({ path: 'variables.env' }); +const createServer = require('./createServer'); +const db = require('./db'); + +const server = createServer(); + +server.express.use(cookieParser()); + +// decode the JWT so we can get the user Id on each request +server.express.use((req, res, next) => { + const { token } = req.cookies; + if (token) { + const { userId } = jwt.verify(token, process.env.APP_SECRET); + // put the userId onto the req for future requests to access + req.userId = userId; + } + next(); +}); + +server.start( + { + cors: { + credentials: true, + origin: process.env.FRONTEND_URL, + }, + }, + deets => { + console.log(`Server is now running on port http://localhost:${deets.port}`); + } +); diff --git a/stepped-solutions/27/backend/src/resolvers/Query.js b/stepped-solutions/27/backend/src/resolvers/Query.js new file mode 100755 index 0000000..8055122 --- /dev/null +++ b/stepped-solutions/27/backend/src/resolvers/Query.js @@ -0,0 +1,21 @@ +const { forwardTo } = require('prisma-binding'); + +const Query = { + items: forwardTo('db'), + item: forwardTo('db'), + itemsConnection: forwardTo('db'), + me(parent, args, ctx, info) { + // check if there is a current user ID + if (!ctx.request.userId) { + return null; + } + return ctx.db.query.user( + { + where: { id: ctx.request.userId }, + }, + info + ); + }, +}; + +module.exports = Query; diff --git a/stepped-solutions/27/backend/src/schema.graphql b/stepped-solutions/27/backend/src/schema.graphql new file mode 100755 index 0000000..f2cc283 --- /dev/null +++ b/stepped-solutions/27/backend/src/schema.graphql @@ -0,0 +1,15 @@ +# 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! +} + +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/27/frontend/components/Nav.js b/stepped-solutions/27/frontend/components/Nav.js new file mode 100755 index 0000000..df6e47f --- /dev/null +++ b/stepped-solutions/27/frontend/components/Nav.js @@ -0,0 +1,32 @@ +import Link from 'next/link'; +import NavStyles from './styles/NavStyles'; +import User from './User'; + +const Nav = () => ( + <NavStyles> + <User> + {({ data: { me } }) => { + console.log(me); + if (me) return <p>{me.name}</p>; + return null; + }} + </User> + <Link href="/items"> + <a>Shop</a> + </Link> + <Link href="/sell"> + <a>Sell</a> + </Link> + <Link href="/signup"> + <a>Signup</a> + </Link> + <Link href="/orders"> + <a>Orders</a> + </Link> + <Link href="/me"> + <a>Account</a> + </Link> + </NavStyles> +); + +export default Nav; diff --git a/stepped-solutions/27/frontend/components/User.js b/stepped-solutions/27/frontend/components/User.js new file mode 100755 index 0000000..addd79d --- /dev/null +++ b/stepped-solutions/27/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 => ( + <Query {...props} query={CURRENT_USER_QUERY}> + {payload => props.children(payload)} + </Query> +); + +User.PropTypes = { + children: PropTypes.func.isRequired, +}; + +export default User; +export { CURRENT_USER_QUERY }; |
