|
| 1 | +import { User } from '../resources/user/user.model' |
| 2 | +import jwt from 'jsonwebtoken' |
| 3 | +import config from '../../config' |
| 4 | +import expressJwt from 'express-jwt' |
| 5 | + |
| 6 | +const checkToken = expressJwt({ secret: config.secrets.JWT_SECRET }) |
| 7 | + |
| 8 | +export const signin = (req, res, next) => { |
| 9 | + // req.user will be there from the middleware |
| 10 | + // verify user. Then we can just create a token |
| 11 | + // and send it back for the client to consume |
| 12 | + const token = signToken(req.user.id) |
| 13 | + res.json({token: token}) |
| 14 | +} |
| 15 | + |
| 16 | +export const decodeToken = () => (req, res, next) => { |
| 17 | + if (config.disableAuth) { |
| 18 | + return next() |
| 19 | + } |
| 20 | + // make it optional to place token on query string |
| 21 | + // if it is, place it on the headers where it should be |
| 22 | + // so checkToken can see it. See follow the 'Bearer 034930493' format |
| 23 | + // so checkToken can see it and decode it |
| 24 | + if (req.query && req.query.hasOwnProperty('access_token')) { |
| 25 | + req.headers.authorization = 'Bearer ' + req.query.access_token |
| 26 | + } |
| 27 | + |
| 28 | + // this will call next if token is valid |
| 29 | + // and send error if its not. It will attached |
| 30 | + // the decoded token to req.user |
| 31 | + checkToken(req, res, next) |
| 32 | +} |
| 33 | + |
| 34 | +export const getFreshUser = () => (req, res, next) => { |
| 35 | + return User.findById(req.user.id) |
| 36 | + .then(function(user) { |
| 37 | + if (!user) { |
| 38 | + // if no user is found it was not |
| 39 | + // it was a valid JWT but didn't decode |
| 40 | + // to a real user in our DB. Either the user was deleted |
| 41 | + // since the client got the JWT, or |
| 42 | + // it was a JWT from some other source |
| 43 | + res.status(401).send('Unauthorized') |
| 44 | + } else { |
| 45 | + // update req.user with fresh user from |
| 46 | + // stale token data |
| 47 | + req.user = user |
| 48 | + next() |
| 49 | + } |
| 50 | + }) |
| 51 | + .catch(error => next(error)) |
| 52 | +} |
| 53 | + |
| 54 | +export const verifyUser = () => (req, res, next) => { |
| 55 | + const username = req.body.username |
| 56 | + const password = req.body.password |
| 57 | + |
| 58 | + // if no username or password then send |
| 59 | + if (!username || !password) { |
| 60 | + res.status(400).send('You need a username and password') |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + // look user up in the DB so we can check |
| 65 | + // if the passwords match for the username |
| 66 | + User.findOne({username: username}) |
| 67 | + .then(function(user) { |
| 68 | + if (!user) { |
| 69 | + res.status(401).send('No user with the given username') |
| 70 | + } else { |
| 71 | + // checking the passowords here |
| 72 | + if (!user.authenticate(password)) { |
| 73 | + res.status(401).send('Wrong password') |
| 74 | + } else { |
| 75 | + // if everything is good, |
| 76 | + // then attach to req.user |
| 77 | + // and call next so the controller |
| 78 | + // can sign a token from the req.user._id |
| 79 | + req.user = user; |
| 80 | + next() |
| 81 | + } |
| 82 | + } |
| 83 | + }) |
| 84 | + .catch(error => next(err)) |
| 85 | +} |
| 86 | + |
| 87 | +export const signToken = (id) => jwt.sign( |
| 88 | + {id}, |
| 89 | + config.secrets.JWT_SECRET, |
| 90 | + {expiresIn: config.expireTime} |
| 91 | +) |
| 92 | + |
| 93 | +export const protect = [decodeToken(), getFreshUser()] |
0 commit comments