[英]How to redirect to profile page after social auth login with Passport ? (Angular, express, passport, jwt)
I'm using passport to achieve login with FB or Google on my app.我正在使用护照在我的应用程序上使用 FB 或 Google 登录。
I succeed to create a profile if it doesn't exist but now I don't understand how to send those information to the front when I want to login.如果配置文件不存在,我会成功创建配置文件,但现在我不明白当我想登录时如何将这些信息发送到前面。 I tried many thing like use
res.cookie(blabla)
, but couldn't use it in front.我尝试了很多类似 use
res.cookie(blabla)
的东西,但不能在前面使用它。 res.send(blabla)
just open a json file. res.send(blabla)
只需打开一个 json 文件。
I certainly missed something but I don't know what.我当然错过了一些东西,但我不知道是什么。
Here is my passportjs file:这是我的passportjs文件:
import passport from 'passport';
import { UsersService } from './../services/users.service';
import { AuthService } from './../services/auth.service';
import * as jwt from 'jsonwebtoken';
let FacebookStrategy = require('passport-facebook').Strategy;
const usersService = UsersService.getInstance();
module.exports = function () {
passport.use('facebook', new FacebookStrategy({
clientID : CLIENTID,
clientSecret : CLIENTSECRET,
callbackURL : 'http://localhost:3000/auth/facebook/callback',
profileFields: ['id', 'emails', 'name']
},
function(access_token:any, refresh_token:any, profile:any, done:any) {
process.nextTick(function() {
usersService.getByEmail(profile.emails[0].value).then((result) => {
if(result.email == profile.emails[0].value) {
console.log('EXISTE');
const userToken = {
email: result.email,
id: result.id
};
const secret: any = process.env.JWT_SECRET;
const token = jwt.sign(userToken, secret);
let newUser : any = result;
newUser.jwtoken = token;
console.log(newUser)
return done(null, newUser)
} else{
console.log('EXISTE PAS')
let user: any = {
email : profile.emails[0].value,
username : profile.username
}
usersService.create(user).then(result => {
return done(null, user)
})
}
})
});
}
))
}
Here is my route file:这是我的路线文件:
router.get('/facebook', passport.authenticate('facebook', { scope: 'email' }))
router.get('/facebook/callback',
passport.authenticate('facebook', {
session: false,
failureRedirect : 'http://localhost:4200/auth/signin'
}), (req: any, res: Response) => {
let token: any = req.user.jwtoken;
res.send(token).redirect('http://localhost:4200/')
}
);
So I tried an other thing for the redirection:所以我尝试了另一件事进行重定向:
router.get('/facebook', passport.authenticate('facebook', { scope: 'email' }))
router.get('/facebook/callback',
passport.authenticate('facebook', {
session: false,
failureRedirect : 'http://localhost:4200/auth/signin'
}), (req: any, res: Response) => {
let user: any = req.user;
res.redirect('http://localhost:3000/users/me', user);
}
);
And now I have an error:现在我有一个错误:
FacebookTokenError: This authorization code has been used.
at Strategy.parseErrorResponse (/home/alex/eygdev/EYG_post-WCS-internship/node_modules/passport-facebook/lib/strategy.js:198:12)
at Strategy.OAuth2Strategy._createOAuthError (/home/alex/eygdev/EYG_post-WCS-internship/node_modules/passport-oauth2/lib/strategy.js:405:16)
at /home/alex/eygdev/EYG_post-WCS-internship/node_modules/passport-oauth2/lib/strategy.js:175:45
at /home/alex/eygdev/EYG_post-WCS-internship/node_modules/oauth/lib/oauth2.js:191:18
at passBackControl (/home/alex/eygdev/EYG_post-WCS-internship/node_modules/oauth/lib/oauth2.js:132:9)
at IncomingMessage.<anonymous> (/home/alex/eygdev/EYG_post-WCS-internship/node_modules/oauth/lib/oauth2.js:157:7)
at IncomingMessage.emit (events.js:203:15)
at IncomingMessage.EventEmitter.emit (domain.js:448:20)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
I don't really understand why I got this error since nobody is logged on my website.我真的不明白为什么会出现此错误,因为没有人登录我的网站。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.