{"size": 2226, "ext": "js", "max_stars_repo_path": "src/config/strategies/local.js", "max_stars_repo_name": "bwyfed/koa-mongo-server", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/config/strategies/local.js", "max_issues_repo_name": "bwyfed/koa-mongo-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/config/strategies/local.js", "max_forks_repo_name": "bwyfed/koa-mongo-server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.9090909091, "max_line_length": 92, "alphanum_fraction": 0.6455525606}
// Load the module dependencies
const passport = require('koa-passport')
const LocalStrategy = require('passport-local').Strategy
// Get User model
const User = require('mongoose').model('User')

// fetch user info
const fetchUserInfo = async (username) => {
  // Fetch data from remote server

  // Update the mongodb
  console.log('fetchUserInfo')
  // Mock data
  // const user = {id: 1, username: 'test', password: 'test'}

  // Get data from mongodb
  const user = await User.findOne({username})
  return user
}
// Create the Local strategy configuration method
exports.createLocalStrategy = (passport) => {
  // Use the Passport's Local strategy
  console.log('register local strategy')
  passport.use(new LocalStrategy((username, password, done) => {
    console.log(`invoke local strategy,username:${username}, password:${password}`)
    fetchUserInfo(username)
    .then(user => {
      console.log('user from db:', user)
      console.log('start auth')
      // If a user was not found, continue to the next middleware with an error message
			if (!user) {
        console.log('Unknown user')
				return done(null, false, {
					message: 'Unknown user'
				});
      }
      // If the passport is incorrect, continue to the next middleware with an error message
			if (!user.authenticate(password)) {
        console.log('Invalid password')
				return done(null, false, {
					message: 'Invalid password'
				});
      }
      // Otherwise, continue to the next middleware with the user object
			return done(null, user);
    })
    .catch((err) => ( done(err) ))
  }))
}

module.exports = () => {
  passport.use(new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',
    session: false
  },
  async (username, password, done) => {
    console.log('local strategy')
    console.log(username, password)
    const user = await fetchUserInfo(username)
    if (!user) {
      return done(null, false, {
        message: 'Unknown user'
      })
    }
    // call User model method authenticate to verify password
    if (!user.authenticate(password)) {
      return done(null, false, {
        message: 'Invalid password'
      })
    }
    // auth success
    return done(null, user)
  }
))
}
