|
| 1 | +const jwt = require('jsonwebtoken') |
| 2 | +const User = require('../user/user.model') |
| 3 | + |
| 4 | +// // // // |
| 5 | + |
| 6 | +// POST /api/auth/register |
| 7 | +// { username, password } |
| 8 | +exports.register = (req, res) => { |
| 9 | + |
| 10 | + // Parses username, password parameters from req.body |
| 11 | + const { username, password } = req.body |
| 12 | + let newUser = null |
| 13 | + |
| 14 | + // create a new user if does not exist |
| 15 | + const create = (user) => { |
| 16 | + if(user) { |
| 17 | + throw new Error('username exists') |
| 18 | + } else { |
| 19 | + return User.create(username, password) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // count the number of the user |
| 24 | + const count = (user) => { |
| 25 | + newUser = user |
| 26 | + return User.count({}).exec() |
| 27 | + } |
| 28 | + |
| 29 | + // assign admin if count is 1 |
| 30 | + const assign = (count) => { |
| 31 | + if(count === 1) { |
| 32 | + return newUser.assignAdmin() |
| 33 | + } else { |
| 34 | + // if not, return a promise that returns false |
| 35 | + return Promise.resolve(false) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // respond to the client |
| 40 | + const respond = (isAdmin) => { |
| 41 | + res.json({ |
| 42 | + message: 'registered successfully', |
| 43 | + admin: isAdmin ? true : false |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + // run when there is an error (username exists) |
| 48 | + const onError = (error) => { |
| 49 | + res.status(409).json({ |
| 50 | + message: error.message |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + // check username duplication |
| 55 | + User.findOneByUsername(username) |
| 56 | + .then(create) |
| 57 | + .then(count) |
| 58 | + .then(assign) |
| 59 | + .then(respond) |
| 60 | + .catch(onError) |
| 61 | +} |
| 62 | + |
| 63 | +// // // // |
| 64 | + |
| 65 | +// POST /api/auth/login |
| 66 | +// { username, password } |
| 67 | +exports.login = (req, res) => { |
| 68 | + const {username, password} = req.body |
| 69 | + const secret = req.app.get('jwt-secret') |
| 70 | + |
| 71 | + // check the user info & generate the jwt |
| 72 | + const check = (user) => { |
| 73 | + if(!user) { |
| 74 | + // user does not exist |
| 75 | + throw new Error('login failed') |
| 76 | + } else { |
| 77 | + // user exists, check the password |
| 78 | + if(user.verify(password)) { |
| 79 | + // create a promise that generates jwt asynchronously |
| 80 | + const p = new Promise((resolve, reject) => { |
| 81 | + jwt.sign( |
| 82 | + { |
| 83 | + _id: user._id, |
| 84 | + username: user.username, |
| 85 | + admin: user.admin |
| 86 | + }, |
| 87 | + secret, |
| 88 | + { |
| 89 | + expiresIn: '7d', |
| 90 | + issuer: 'velopert.com', |
| 91 | + subject: 'userInfo' |
| 92 | + }, (err, token) => { |
| 93 | + if (err) reject(err) |
| 94 | + resolve(token) |
| 95 | + }) |
| 96 | + }) |
| 97 | + return p |
| 98 | + } else { |
| 99 | + throw new Error('login failed') |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // respond the token |
| 105 | + const respond = (token) => { |
| 106 | + res.json({ |
| 107 | + message: 'logged in successfully', |
| 108 | + token |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + // error occured |
| 113 | + const onError = (error) => { |
| 114 | + res.status(403).json({ |
| 115 | + message: error.message |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + // find the user |
| 120 | + User.findOneByUsername(username) |
| 121 | + .then(check) |
| 122 | + .then(respond) |
| 123 | + .catch(onError) |
| 124 | + |
| 125 | +} |
0 commit comments