This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const getRecipe = (root, { id }, { user }) => { | |
| throwErrorIfUserNotAuthenticated(user); | |
| return Recipe.findById(id).exec(); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const secret = process.env.TOKEN_SECRET; | |
| const expiresIn = process.env.EXPIRES_IN || '1 day'; | |
| export const signIn = payload => jsonwebtoken.sign(payload, secret, { expiresIn }); | |
| export const verify = token => { | |
| return new Promise((resolve, reject) => { | |
| jsonwebtoken.verify(token, secret, {}, (err, payload) => { | |
| if(err){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { verify } from './api/modules/auth'; | |
| const setMiddleware = (app) => { | |
| app.use(async (req, res, next) => { | |
| try{ | |
| const token = req.headers.authorization || ''; | |
| const user = await verify(token); | |
| req.user = user; | |
| } | |
| catch(e) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const userResolvers = { | |
| Query: { | |
| getMe | |
| }, | |
| Mutation: { | |
| createUser, //createUser: createUser | |
| loginUser //loginUser: loginUser | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| User: { | |
| recipes(user) { | |
| return Recipe.find({ userId: user.id }).exec() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type User { | |
| id: ID! | |
| email: String! | |
| name: String! | |
| token: String | |
| recipes: [Recipe]! | |
| createdAt: String! | |
| updatedAt: String! | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { User } from './user.model'; | |
| import { Recipe } from '../recipe/recipe.model'; | |
| import { signIn, verify } from '../../modules/auth';'' | |
| const loginUser = async(root, { input }) => { | |
| const { email, password } = input; | |
| const user = await User.findOne({ email }).exec(); | |
| const errorMsg = 'Wrong credentials. Please try again'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { User } from './user.model'; | |
| import { Recipe } from '../recipe/recipe.model'; | |
| import { signIn, verify } from '../../modules/auth';'' | |
| const loginUser = async(root, { input }) => { | |
| const { email, password } = input; | |
| const user = await User.findOne({ email }).exec(); | |
| const errorMsg = 'Wrong credentials. Please try again'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Recipe { | |
| id: ID! | |
| name: String! | |
| description: String! | |
| difficultyLevel: Int! | |
| image: String | |
| steps: [String]! | |
| averageTimeForCompletion: Int! | |
| user: User! | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| input UpdateRecipe { | |
| id: ID! | |
| name: String | |
| description: String | |
| difficultyLevel: Int! | |
| fileUrl: String | |
| steps: [String] | |
| averageTimeForCompletion: Int! | |
| } |