Skip to content

Instantly share code, notes, and snippets.

@badmonster0
Created May 8, 2015 05:23
Show Gist options
  • Save badmonster0/9293b234ea92d63b73d8 to your computer and use it in GitHub Desktop.
Save badmonster0/9293b234ea92d63b73d8 to your computer and use it in GitHub Desktop.
How to create a simple passport strategy using es7 + babel + express
let app = express()
app.use(passport.initialize())

let userConst = {
        email: '[email protected]',
        password: bcrypt.hashSync('asdf', SALT)
}
    
passport.use('local-simple', new LocalStrategy({
    // Use "email" field instead of "username"
    usernameField: 'email',
    // We'll need this later
    failureFlash: true
}, (email, password, callback) => {
  

    nodeify(async() => {
        if (email !== userConst.email) {
            return [false, {
                message: 'Invalid username'
            }]
        }

        if (!await bcrypt.promise.compare(password, userConst.password)) {
            return [false, {
                message: 'Invalid password'
            }]
        }
        //after you return
        //it becomes req.user
        //otherwise you won't have anything
        return userConst

        // Use spread option when returning multiple values
        // so a callback gets convert from [1,2] => callback(null, 1, 2)
        // without spread:true, it becomes   [1, 2] => callback(null, [1, 2])
        // https://gist.github.com/vanessachem/3ba92e73ff5d21d696b9
    }(), callback, {
        spread: true
    })
}))

app.post('/login', getLoginInfo, passport.authenticate('local-simple', {
    successRedirect: '/profile',
    failureRedirect: '/',
    failureFlash: true
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment