| @@ -31,6 +31,15 @@ tokenSchema.statics.findByCredentials = async (email, password) => { | |||
| return user | |||
| } | |||
| tokenSchema.statics.generateAuthToken = async function(userArg) { | |||
| const user = userArg | |||
| const token = jwt.sign({ _id: user._id.toString() }, 'ovoJeSecret') | |||
| user.tokens = user.tokens.concat({ token }) | |||
| await user.save() | |||
| return token | |||
| } | |||
| const Token = mongoose.model('Token', tokenSchema) | |||
| module.exports = Token | |||
| @@ -17,7 +17,7 @@ router.post('/login', async (req, res) => { | |||
| return res.status(400).send('Password is incorrect!') | |||
| } | |||
| const token = await findUser.generateAuthToken() | |||
| const token = await Token.generateAuthToken(findUser) | |||
| return res.send(findUser) | |||
| }) | |||