如何解决为什么不调用deserializeUser?角度和快速
我已经阅读了与我的有关堆栈溢出以及我能找到的其他地方的所有问题...
我不能解决我的问题...
我正在使用Angular和Express ...
我使用withCredentials
这是我的 app.ts 文件/ app.js编译后
import express = require('express');
import dotenv = require('dotenv');
import router from './routes';
import cors from 'cors';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import session = require('express-session');
import passport from 'passport';
dotenv.config();
const secret_key: string = (process.env.SECRET_KEY as string);
const app: express.Application = express();
// Add headers
const options: cors.CorsOptions = {
allowedHeaders: [
'Origin','X-Requested-With','Content-Type','Accept','X-Access-Token',],credentials: true,methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',origin: 'http://localhost:4200',preflightContinue: false,};
app.use(cors(options));
app.use(express.static(__dirname + 'public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.options('*',cors(options));
app.use(session({
secret: 'test-secret',resave: true,saveUninitialized: true,cookie: { maxAge: 2 * 60 * 60 * 1000/* 2 hours */,secure: false }
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.initialize(),router);
app.listen(process.env.PORT,() => console.log(`Hello world app listening on port ${process.env.PORT}!`));
passport.js文件,其中反序列化在其中。
import passport from 'passport';
import { knex } from './knex/knex';
export default () => {
passport.serializeUser((user: any,done: any) => {
console.log('inside ser');
done(null,user.userId);
});
passport.deserializeUser((userId: any,done: any) => {
console.log('inside des');
knex('accounts').where({userId: userId}).first()
.then((user) => { done(null,user); console.log(user)})
.catch((err) => { done(err,null); console.log(`deserialoize Error ${err}`) });
});
}
local.js初始化用于序列化和反序列化
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
import init = require('../passport');
import { knex } from '../knex/knex';
import * as bcrypt from 'bcrypt';
const options = {
usernameField: 'email',passwordField: 'password'
};
init.default();
export default passport.use(new LocalStrategy(options,(username,password,done) => {
knex('accounts').where({userEmail: username}).first()
.then((user) => {
if (!user) return done(null,false)
if(!bcrypt.compareSync(password,user.userPassword)) {
return done(null,false);
}
else {
return done(null,user);
}
})
.catch((err) => { return done(err); });
}));
这是我的用户控制器
export const userLogIn = async (req: Request,res: Response) => {
passport.default.authenticate('local',(err,user,info) => {
if (err) { res.status(500).send('error'); console.log(err)}
if (!user) { res.status(404).send('User not found!'); }
if (user)
{
req.logIn(user,function (err) {
if (err) { res.status(500).send('error'); console.log(err)}
res.status(200);
});
}
})(req,res);
}
这就是我从angular那里进行请求的方式
logInUser(value: any): void{
this.http.post<any>('http://localhost:3000/login',value,{ withCredentials: true }).subscribe(data => {
//let user: User = data;
//console.warn(data);
});
}
我已经挣扎了将近2天...
我知道这不是最好的打字稿代码,我在学习时会不断对其进行改进,但是现在我想解决此问题,并且我认为它与打字稿无关。
如果有帮助,我检查了sessionID并保持不变,会话也与我设置的maxAge等一起存储在请求中
但是没有对来自角度的请求的request.user
调用和 serializeUser ,但不调用 deserializeUser
解决方法
您的方法似乎有错字。请注意,护照添加了req.login
方法不是 req.logIn
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。