Passport 整合入門

**** 必須使用 passport.initialize() 中介軟體初始化 Passport 。要使用登入會話,需要 passport.session() 中介軟體。

請注意,必須定義 passport.serialize()passport.deserializeUser() 方法。 Passport 將對會話進行序列化和反序列化使用者例項

const express = require('express');
const session = require('express-session');
const passport = require('passport');
const cookieParser = require('cookie-parser');
const app = express();

// Required to read cookies
app.use(cookieParser());

passport.serializeUser(function(user, next) {
    // Serialize the user in the session
    next(null, user);
});

passport.deserializeUser(function(user, next) {
    // Use the previously serialized user
        next(null, user);
});

// Configuring express-session middleware
app.use(session({
    secret: 'The cake is a lie',
    resave: true,
    saveUninitialized: true
}));

// Initializing passport
app.use(passport.initialize());
app.use(passport.session());

// Starting express server on port 3000
app.listen(3000);