gpt4 book ai didi

node.js - 使用 Passport js 登录 google

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:21 31 4
gpt4 key购买 nike

https://github.com/harshitKyal/loginWithGoogle

这是我的 github 存储库链接。我正在使用 Passport js 登录 google 。我在谷歌开发者控制台创建了客户端 key 和 secret key 。运行代码时,它会将我重定向到成功页面,但 req.user 对象未定义。请引导我完成这个过程。我从https://github.com/mstade/passport-google-oauth2/tree/master/example获取代码

app.get( '/auth/google/callback', 
passport.authenticate( 'google', {
successRedirect: '/',
failureRedirect: '/login'
}));

app.get('/', function(req, res){
res.render('index', { user: req.user });
});

此代码将我重定向到索引页面。但在索引页我已经写了

<% if (!user) { %>
<h2>Welcome! Please log in.</h2>
<% } else { %>
<h2>Hello, <%= user.displayName %>.</h2>
<% } %>

应该显示您好,请输入用户名。

但它没有显示,而是显示欢迎!请登录。

最佳答案

代码库看起来不错。注意到您正在使用与 Redis 服务器的连接来存储 session 信息:

store:  new RedisStore({
host: 'localhost',
port: 6379
}),

你开始了吗?要检查您的 Redis 服务器是否正在运行以及您的应用程序是否可以连接到它,您可以查看 debug output :

export DEBUG=*
//and then run your app
node example/app.js

如果您会看到很多错误,例如:

    connect:redis Redis returned err { Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:1020:11)
at exports._exceptionWithHostPort (util.js:1043:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',

然后,您的 Redis 连接丢失。

要解决此问题,您可以:

a) 安装并运行 redis本地

b) 使用docker image 。这需要docker待安装。我确实在 MAC OS Sierra 上尝试过,它与命令配合得很好:

docker run --name some-redis -p 6379:6379 -d redis

看起来这就是你错过的事情 - 启动 redis 服务器。如果你设置了它 - 一切都应该运行良好。

关于node.js - 使用 Passport js 登录 google,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49002183/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com