summaryrefslogtreecommitdiffstats
path: root/model/User.js
diff options
context:
space:
mode:
Diffstat (limited to 'model/User.js')
-rw-r--r--model/User.js28
1 files changed, 10 insertions, 18 deletions
diff --git a/model/User.js b/model/User.js
index cbd1e60..ce34cbe 100644
--- a/model/User.js
+++ b/model/User.js
@@ -47,15 +47,11 @@ const userSchema = new mongoose.Schema({
userSchema.statics.checkRefreshToken = async (email, refresh) => {
const user = await User.findOne({ email })
- if (!user) {
- throw new Error('User not found')
- }
+ if (!user) throw new Error('User not found')
const isMatch = await bcrypt.compare(refresh, user.refresh)
- if (!isMatch) {
- throw new Error('Wrong refresh token')
- }
+ if (!isMatch) throw new Error('Wrong refresh token')
return user
}
@@ -63,31 +59,27 @@ userSchema.statics.checkRefreshToken = async (email, refresh) => {
userSchema.statics.findByCredentials = async (email, password) => {
const user = await User.findOne({ email })
- if (!user) {
- throw new Error('Unable to login')
- }
+ if (!user) throw new Error('Unable to login')
const isMatch = await bcrypt.compare(password, user.password)
- if (!isMatch) {
- throw new Error('Unable to login')
- }
+ if (!isMatch) throw new Error('Unable to login')
return user
}
userSchema.statics.newRefreshToken = async (user) => {
const newRefreshToken = randomBytes(32).toString('hex')
+
user.refresh = newRefreshToken
+ await user.save()
- try {
- await user.save()
- return newRefreshToken
- } catch(err) {
- return ''
- }
+ return newRefreshToken
}
+userSchema.statics.removeRefreshToken = async (email) => (
+ await User.findOneAndUpdate({ email }, { refresh: '' })
+)
userSchema.pre('save', async function(next){
const user = this