如何在同一个项目中为不同的用户创建两个本地策略?
How can create two local strategy in same porject for different user'?
我正在做一个项目,我必须创建两个登录名或不同的用户员工或客户如何使用 sequelize 在节点 js 中创建两个本地护照
无法在文档中找到它,但 passport.use()
可以接收 name
作为第一个参数。
passport.use('local-employee', new LocalStrategy(...))
passport.use('local-customer', new LocalStrategy(...))
完整示例:
const express = require('express')
const passport = require('passport');
const LocalStrategy = require('passport-local');
const app = express();
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({
secret: 'BAH',
resave: true,
saveUninitialized: true
}));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use('local-employee', new LocalStrategy(
function (username, password, done) {
console.log(username, password);
// get user from employee source
user = {username, password, type: 'employee'};
return done(null, user);
}
));
passport.use('local-customer', new LocalStrategy(
function (username, password, done) {
console.log(username, password);
// get user from customer source
user = {username, password, type: 'customer'};
return done(null, user);
}
));
app.use(passport.initialize());
app.use(passport.session());
app.post('/employee/login',
passport.authenticate('local-employee'),
function (req, res) {
res.json(req.user)
});
app.post('/customer/login',
passport.authenticate('local-customer'),
function (req, res) {
res.json(req.user)
});
app.listen(3000)
{
"name": "passport-local",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
"express": "^4.17.1",
"express-session": "^1.17.2",
"passport": "^0.4.1",
"passport-local": "^1.0.0"
}
}
我正在做一个项目,我必须创建两个登录名或不同的用户员工或客户如何使用 sequelize 在节点 js 中创建两个本地护照
无法在文档中找到它,但 passport.use()
可以接收 name
作为第一个参数。
passport.use('local-employee', new LocalStrategy(...))
passport.use('local-customer', new LocalStrategy(...))
完整示例:
const express = require('express')
const passport = require('passport');
const LocalStrategy = require('passport-local');
const app = express();
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({
secret: 'BAH',
resave: true,
saveUninitialized: true
}));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use('local-employee', new LocalStrategy(
function (username, password, done) {
console.log(username, password);
// get user from employee source
user = {username, password, type: 'employee'};
return done(null, user);
}
));
passport.use('local-customer', new LocalStrategy(
function (username, password, done) {
console.log(username, password);
// get user from customer source
user = {username, password, type: 'customer'};
return done(null, user);
}
));
app.use(passport.initialize());
app.use(passport.session());
app.post('/employee/login',
passport.authenticate('local-employee'),
function (req, res) {
res.json(req.user)
});
app.post('/customer/login',
passport.authenticate('local-customer'),
function (req, res) {
res.json(req.user)
});
app.listen(3000)
{
"name": "passport-local",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
"express": "^4.17.1",
"express-session": "^1.17.2",
"passport": "^0.4.1",
"passport-local": "^1.0.0"
}
}