我的Ember应用需要使用Google OAuth 2对用户进行身份验证。我希望能够将身份验证令牌存储在数据库中,因此我决定将身份验证过程放在服务器端,使用Passport进行节点
当服务器上的身份验证结束时,如何让您的Ember应用程序知道Passport“会话”?
答案 0 :(得分:4)
通过护照流程进行身份验证后,客户端会在与服务器的所有通信中发送用户会话及其请求。如果您希望Handlebars模板在用户在场时进行调整,我的方法是在服务器上设置以下请求处理程序:
app.get("/user", function (req,res) {
if (req.isAuthenticated()) {
res.json({
authenticated: true,
user: req.user
})
} else {
res.json({
authenticated: false,
user: null
})
}
})
在我的Ember路线中,我提出以下要求:
App.ApplicationRoute = Ember.Route.extend({
model: function () {
return $.get("/user").then(function (response) {
return {user: response.user};
})
}
});
因此,在我的Handlebars模板中,我可以执行以下操作:
{{#if user}}
<p>Hello, {{user.name}}</p>
{{else}}
<p>You must authenticate</p>
{{/if}}