|
|
|
|
|
|
|
|
if(!user) { |
|
|
if(!user) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const checkMatch = await bcrypt.compare(password, user.password) |
|
|
const checkMatch = await bcrypt.compare(password, user.password) |
|
|
console.log(password) |
|
|
|
|
|
console.log(user.password) |
|
|
|
|
|
console.log(checkMatch) |
|
|
|
|
|
if(!checkMatch) { |
|
|
|
|
|
|
|
|
if(checkMatch) { |
|
|
return user |
|
|
return user |
|
|
} |
|
|
} |
|
|
return user |
|
|
|
|
|
|
|
|
return null |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
tokenSchema.statics.generateAuthToken = async function(userArg) { |
|
|
tokenSchema.statics.generateAuthToken = async function(userArg) { |
|
|
const user = userArg |
|
|
const user = userArg |
|
|
const token = jwt.sign({ _id: user._id.toString() }, 'ovoJeSecret') |
|
|
|
|
|
user.tokens = user.tokens.concat({ token }) |
|
|
|
|
|
|
|
|
const token = jwt.sign({ _id: user._id.toString() }, 'ovoJeSecret', { expiresIn: 60 * 20 }) |
|
|
|
|
|
const newToken = new Token({ token: token, userId: user._id }) |
|
|
|
|
|
await newToken.save() |
|
|
|
|
|
user.tokens = user.tokens.concat({ token: newToken }) |
|
|
await user.save() |
|
|
await user.save() |
|
|
|
|
|
|
|
|
|
|
|
console.log(newToken) |
|
|
|
|
|
|
|
|
return token |
|
|
return token |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
tokenSchema.statics.refreshAuthToken = async function(token, refreshOptions) { |
|
|
|
|
|
const payload = jwt.verify(token, 'ovoJeSecret', refreshOptions.verify); |
|
|
|
|
|
delete payload.iat; |
|
|
|
|
|
delete payload.exp; |
|
|
|
|
|
delete payload.nbf; |
|
|
|
|
|
delete payload.jti; |
|
|
|
|
|
const jwtSignOptions = Object.assign({ }, this.options, { jwtid: refreshOptions.jwtid }); |
|
|
|
|
|
|
|
|
|
|
|
return jwt.sign(payload, this.secretOrPrivateKey, jwtSignOptions); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const Token = mongoose.model('Token', tokenSchema) |
|
|
const Token = mongoose.model('Token', tokenSchema) |
|
|
|
|
|
|
|
|
module.exports = Token |
|
|
module.exports = Token |