import passport from 'koa-passport';
import User from '../src/models/users'
import { Strategy } from 'passport-local'

passport.serializeUser((user, done) => {
  done(null, user.id)
})

passport.deserializeUser(async (id, done) => {
  try {
    const user = await User.findById(id, '-password')
    done(null, user)
  } catch(err) {
    done(err)
  }
})

passport.use('local', new Strategy({
  usernameField: 'username',
  passwordField: 'password'
}, async (email, password, done) => {
  try {
    const user = await User.findOne({ email, password })
    if(!user) { return done(null, false) }

    done(null, user)
  } catch(err) {
    return done(err)
  }
}))

@@ -452,21 +452,24 @@
  async (
-email
+username
 , passwo
@@ -533,23 +533,132 @@
 e(%7B 
-email, 
+username %7D)%0A    if(!user) %7B return done(null, false) %7D%0A%0A    try %7B%0A      const isMatch = await user.validatePassword(
 password
  %7D)%0A
@@ -653,32 +653,36 @@
 password
- %7D
 )
+%0A
 %0A    
+  
 if(!
-user
+isMatch
 ) %7B retu
@@ -705,24 +705,26 @@
 lse) %7D%0A%0A    
+  
 done(null, u
@@ -720,32 +720,74 @@
 one(null, user)%0A
+    %7D catch(err) %7B%0A      done(err)%0A    %7D%0A%0A
   %7D catch(err) %7B

Add password validation to passport strategy