[英]Ember app with server-side Google OAuth 2 Passport authentication (Node)
My Ember app needs to authenticate users with Google OAuth 2. I want to be able to store the authentication tokens in a database, so I decided to put the authentication process on the server side, using Passport for Node. 我的Ember应用程序需要使用Google OAuth 2对用户进行身份验证。我希望能够将身份验证令牌存储在数据库中,因此我决定使用Passport for Node将身份验证过程放在服务器端。
When the authentication is over on the server, how do you make your Ember app aware of the Passport "session"? 当服务器上的身份验证结束时,如何让您的Ember应用程序知道Passport“会话”?
Once authenticated thanks to the passport process, the client, in all its communications with the server, sends the user session along with its requests. 通过护照过程进行身份验证后,客户端在与服务器的所有通信中发送用户会话及其请求。 If you want your Handlebars template to condition on the presence of a user, my approach was to set up the following request handler on the server: 如果您希望Handlebars模板在用户在场的情况下进行调整,我的方法是在服务器上设置以下请求处理程序:
app.get("/user", function (req,res) {
if (req.isAuthenticated()) {
res.json({
authenticated: true,
user: req.user
})
} else {
res.json({
authenticated: false,
user: null
})
}
})
And in my Ember route I do the following request: 在我的Ember路线中,我做了以下要求:
App.ApplicationRoute = Ember.Route.extend({
model: function () {
return $.get("/user").then(function (response) {
return {user: response.user};
})
}
});
So that in my Handlebars template I can do the following: 所以在我的Handlebars模板中我可以执行以下操作:
{{#if user}}
<p>Hello, {{user.name}}</p>
{{else}}
<p>You must authenticate</p>
{{/if}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.