[英]Implement passportjs and angular4
Look, i've made a mean stack app with register/login using passportjs, and it was fine, when i change to angular2(the other was on angularjs) i've made the backend side and i think it works but i dont know how to test it on the fronted side, i know it has to be something with the url in the <a href="">
but when i put the route that passport gives me, example: /auth/facebook
, my aplication does nothing... well it does something, send me to the homepage :c here is my code so far on the backend side 你看,我已经用passwordjs制作了一个带有注册/登录的普通堆栈应用程序,很好,当我更改为angular2(另一个在angularjs上)时,我做了后端,我认为它可以用,但是我不知道如何在正面测试它,我知道它必须是<a href="">
带有url的东西,但是当我放置护照给我的路线时,例如: /auth/facebook
,我的应用程序什么都不做...很好,它可以将我发送到主页:c这是到目前为止我在后端的代码
const FacebookStrategy = require('passport-facebook').Strategy;
const session = require('express-session');
const secret = require('../config/database')
const user = require('../models/user')
module.exports = function(app, passport){
app.use(passport.initialize());
app.use(passport.session());
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
passport.serializeUser(function(user, done) {
token = jwt.sign({email: user.email}, secret, {expiresIn : '24h'});
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use(new FacebookStrategy({
clientID: '350645212059140',
clientSecret: '8a990aec6db37cc3535f4223c235c427',
callbackURL: "http://localhost:4200/auth/facebook/callback",
profileFields: ['id','displayName','email']
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
// User.findOrCreate(..., function(err, user) {
// if (err) { return done(err); }
// done(null, user);
// });
done(null, profile)
}
));
app.get('/auth/facebook',passport.authenticate('facebook', { scope: 'email' }));
app.get('/auth/facebook/callback' , passport.authenticate('facebook', {failureRedirect: '/login'}), function(res, res){
if (err) {
console.log(err)
}else{
res.send('wi')
}
})
return passport;
}
my question is, how can i implement this on the fronted side, remember angular 4 :) 我的问题是,我怎样才能在正面实现这一点,请记住角度4 :)
Here is pseudo code for this. 这是伪代码。
Step 1: When you are submitting your form using ngSubmit call a function for instance passportLogin(). 第1步:使用ngSubmit提交表单时,请调用一个函数,例如passportLogin()。
Step 2: Now in your component use this function and do an HTTP post request to your node js(express js) URL, for example, auth/login. 步骤2:现在在您的组件中使用此功能,并向节点js(express js)URL发出HTTP post请求,例如auth / login。
Step 3: In the server side write a routing 步骤3:在服务器端编写路由
var express = require('express'),
router = express.Router();
module.exports = function(passport){
router.post('/login', passport.authenticate('local-login', {
successRedirect: '/auth/success',
failureRedirect: '/auth/failure'
}));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.