|
1 | | -import mongoose from 'mongoose' |
2 | | -import { makeExecutableSchema } from 'graphql-tools' |
3 | | -import jwt from 'jsonwebtoken' |
4 | | -import bcrypt from 'bcrypt' |
| 1 | +import { makeExecutableSchema } from 'graphql-tools'; |
| 2 | +import jwt from 'jsonwebtoken'; |
| 3 | +import bcrypt from 'bcrypt'; |
5 | 4 |
|
6 | | -import typeDefs from '../graphql/typeDefs' |
7 | | -import resolvers from '../graphql/resolvers' |
8 | | -import context from '../graphql/context' |
9 | | -import model from '../graphql/user_schema/user.model' |
| 5 | +import typeDefs from '../graphql/typeDefs'; |
| 6 | +import resolvers from '../graphql/resolvers'; |
| 7 | +import context from '../graphql/context'; |
| 8 | +import model from '../graphql/user_schema/user.model'; |
10 | 9 |
|
11 | | -const userModel = model.User |
12 | | -const fakeContext= mongoose.model('Fake', mongoose.Schema({})) |
| 10 | +const userModel = model.User; |
13 | 11 |
|
14 | | -const saltRounds = 15 |
15 | | - let authController = {} |
16 | | - const SECRET = 'secret' |
| 12 | +const authController = {}; |
| 13 | +const SECRET = 'secret'; |
17 | 14 |
|
18 | | - authController.login = (req, res) => { |
19 | | - userModel.findOne({ email: req.body.email }) |
20 | | - .then( |
21 | | - (user) => { |
22 | | - bcrypt.compare(req.body.password, user.password, (error, success) => { |
23 | | - if (error) { |
24 | | - res.send(401, { error: error, message: 'Password mismatch'}) |
25 | | - } else if (success) { |
26 | | - const token = jwt.sign({ user_id: user._id }, SECRET, { expiresIn: '3h' }) |
27 | | - return res.send( |
28 | | - 200, |
29 | | - { |
30 | | - _id: user._id, |
31 | | - nickname : user.nickname, |
32 | | - username: user.username, |
33 | | - photo : user.photo, |
34 | | - email : user.email, |
35 | | - token: token, |
36 | | - } |
37 | | - ) |
38 | | - } |
39 | | - }) |
40 | | - } |
41 | | - ) |
42 | | - .catch(error => res.send(401, { error: error, message: 'Error, user does not exists' })) |
43 | | - } |
44 | | - |
45 | | - const generateSecureSchema = () => { |
46 | | - const schema = makeExecutableSchema({ |
47 | | - typeDefs, |
48 | | - resolvers, |
| 15 | +authController.login = (req, res) => { |
| 16 | + userModel.find({ email: req.body.email }) |
| 17 | + .then((user) => { |
| 18 | + bcrypt.compare(req.body.password, user.password, (error, success) => { |
| 19 | + if (error) { |
| 20 | + res.send(401, { error, message: 'Password mismatch' }); |
| 21 | + } else if (success) { |
| 22 | + const token = jwt.sign({ user_id: user._id }, SECRET, { expiresIn: '3h' }); |
| 23 | + return res.send( |
| 24 | + 200, |
| 25 | + { |
| 26 | + _id: user._id, |
| 27 | + nickname: user.nickname, |
| 28 | + username: user.username, |
| 29 | + photo: user.photo, |
| 30 | + email: user.email, |
| 31 | + token, |
| 32 | + }, |
| 33 | + ); |
| 34 | + } |
| 35 | + }); |
49 | 36 | }) |
| 37 | + .catch(error => res.send(401, { error, message: 'Error, user does not exists' })); |
| 38 | +}; |
50 | 39 |
|
51 | | - let generated = {schema, context} |
52 | | - |
53 | | - return generated |
54 | | - } |
| 40 | +const generateSecureSchema = () => { |
| 41 | + const schema = makeExecutableSchema({ |
| 42 | + typeDefs, |
| 43 | + resolvers, |
| 44 | + }); |
55 | 45 |
|
56 | | - authController.verifyToken = (req, res) => { |
57 | | - const TOKEN = req.header('Authorization') |
58 | | - let secureSchema |
| 46 | + const generated = { schema, context }; |
59 | 47 |
|
60 | | - if (TOKEN) |
61 | | - jwt.verify(TOKEN, SECRET, |
62 | | - (error, decoded) => { |
63 | | - if (error) |
64 | | - res.send(401, {message: 'Error: invalid token'}) |
65 | | - else |
66 | | - secureSchema = generateSecureSchema() |
67 | | - } |
68 | | - ) |
| 48 | + return generated; |
| 49 | +}; |
69 | 50 |
|
70 | | - else |
71 | | - res.send(401, {message: 'Error: token is required'}) |
| 51 | +authController.verifyToken = (req, res) => { |
| 52 | + const TOKEN = req.header('Authorization'); |
| 53 | + let secureSchema; |
72 | 54 |
|
73 | | - return secureSchema |
| 55 | + if (TOKEN) { |
| 56 | + jwt.verify( |
| 57 | + TOKEN, SECRET, |
| 58 | + (error, decoded) => { |
| 59 | + if (error) { |
| 60 | + res.send(401, { message: 'Error: invalid token' }); |
| 61 | + } else if (decoded) { |
| 62 | + secureSchema = generateSecureSchema(); |
| 63 | + } |
| 64 | + }, |
| 65 | + ); |
| 66 | + } else { |
| 67 | + res.send(401, { message: 'Error: token is required' }); |
74 | 68 | } |
75 | 69 |
|
76 | | -export default authController |
| 70 | + return secureSchema; |
| 71 | +}; |
| 72 | + |
| 73 | +export default authController; |
0 commit comments