我是新节点js.I我正在开发简单照片共享网络应用程序。我正在使用护照js进行社交认证,我设置了从客户端登录或退出的cookie天气,这是反应js / redux。 我很确定我确实为cookie设置了任何到期日期。但我不知道当我关闭浏览器时我的cookie会被自动删除...这是我犯的一个愚蠢的错误
server.js
import express from 'express';
import path from 'path';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware'
import webpackHotMidleware from 'webpack-hot-middleware';
import bodyParser from 'body-parser';
import webpackConfig from '../../webpack.config.dev';
//Login Stuff
import passport from 'passport';
import config from './Auth';
import r from 'rethinkdb';
import uuid from 'node-uuid'
const JWTPASS = 'NONOE';
var cookieParser = require('cookie-parser');
import jwt from 'jsonwebtoken'
const FacebookStrategy = require('passport-facebook').Strategy;
const TwitterStrategy = require('passport-twitter').Strategy;
var session = require('express-session');
import Cookies from 'cookies'
let app = express();
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(cookieParser('keyboardcat'));
app.use(session({
secret: 'keyboardcat',
resave: true,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
app.use((req,res,next) => {
let token = req.cookies['peace'];
if(token){
jwt.verify(token,JWTPASS,((err,decoded) => {
if(err){
res.cookie('check',false);
res.clearCookie("peace");
next();
}else if(decoded.verify !== 'falseAlarm'){
res.cookie('check',false);
res.clearCookie("peace");
next();
}else{
res.cookie('check',true);
next();
}
}))
}else{
res.cookie('check',false);
next();
}
});
const compiler = webpack(webpackConfig);
app.use(webpackMiddleware(compiler, {
hot: true,
publicPath: webpackConfig.output.publicPath,
noInfo: true
}));
app.use(webpackHotMidleware(compiler));
//FacebookConfigStarts
passport.use(new FacebookStrategy({
clientID: config.facebookAuth.clientID,
clientSecret: config.facebookAuth.clientSecret,
callbackURL: config.facebookAuth.callbackURL,
profileFields: ['id', 'displayName', 'link', 'email','photos'],
passReqToCallback: true,
},
function(req,accessToken, refreshToken, profile, done) {
let email = profile._json.email;
let name = profile._json.name;
let facebookUrl = profile._json.link;
let facebook_id = profile._json.id;
let facebook_pic = profile._json.picture.data.url;
r.connect({db:'image'}).then((conn) => {
r.table('users').filter({facebookId:facebook_id}).run(conn,(error,cursor) => {
cursor.toArray().then((users) => {
if(users.length > 0){
console.log(users[0],"from Alredy users")
done(null,users[0])
}else{
r.table('users').insert({email:email,facebookId:facebook_id,name:name,propic:facebook_pic,username:uuid.v1(),facebookUrl:facebookUrl}).run(conn).then((response) => {
console.log("from NEw User",response);
return r.table('users').get(response.generated_keys[0]).run(conn)
}).then((newUser) => {
//console.log(newUser)
done(null,newUser)
})
}
})
})
});
// // done(null,profile)
}
));
app.get('/auth/facebook', passport.authenticate('facebook',{ scope: ['email','user_location'],session:false }));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {failureRedirect: '/',session:false}),(req,res) => {
let payload = {
verify:'falseAlarm',
user:req.user
};
let token = jwt.sign(payload,JWTPASS);
res.cookie('peace',token)//this cookie automatically deleted
res.redirect('/feed')
});
//Twitter Strategy
passport.serializeUser(function(user, done) {
done(null, user.id);
});
//
// // used to deserialize the user
passport.deserializeUser(function(id, done) {
r.connect({db:'image'}).then((conn) => {
r.table('users').get(id).run(conn).then((user) => {
done(null,user)
})
})
});
passport.use(new TwitterStrategy({
consumerKey: config.twitterAuth.consumerKey,
consumerSecret: config.twitterAuth.consumerSecret,
callbackURL: config.twitterAuth.callbackURL,
},
function(token, tokenSecret, profile, done) {
r.connect({db:'image'}).then((conn) => {
r.table('users').filter({twitterId:profile.id}).run(conn,(error,cursor) =>{
cursor.toArray().then((users) => {
if(users.length > 0){
return done(null,users[0])
}else{
//console.log('Not there So iam Insertin')
r.table('users').insert({name:profile.username,twitterId:profile.id,propic:profile.photos[0].value,username:uuid.v1(),twitterUsername:profile.username}).run(conn).then((response) => {
return r.table('users').get(response.generated_keys[0]).run(conn)
}).then((newUser) => {
//console.log('New User',newUser)
done(null,newUser)
})
}
})
})
})
}
));
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/' }),
function(req, res) {
// console.log(req.user)
let payload = {
verify:'falseAlarm',
user:req.user
};
let token = jwt.sign(payload,JWTPASS);
res.cookie('peace',token)//this cookie autoMatically deleted
res.redirect('/feed');
});
app.get('/logout',(req,res) => {
// console.log(req.user)
req.session.destroy()
req.logout()
res.clearCookie("peace");
res.clearCookie("connect.sid")
res.redirect('/')
});
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '../../index.html'))
});
app.listen(3000, () => {
console.log('Listening')
});