简体   繁体   English

带有服务器端Google OAuth 2 Passport身份验证的Ember应用程序(节点)

[英]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.

相关问题 具有服务器端验证处理的Ember示例应用程序 - Ember sample app with server-side validation handling 使用Oauth服务器/客户端进行Ember身份验证 - Ember authentication with Oauth server/client 服务器端使用ember数据需要什么? - What is needed server-side to use ember-data? 使用Ember和Node编译服务器端模板 - Server side template compiling with Ember and Node Ember用户身份验证(客户端和服务器端完全分开) - Ember user authentication (Client side and Server Side completely separated) 带有节点服务器和社交登录名的Ember Cli身份验证 - Ember Cli Authentication with node server and social logins ember.js registerBoundHelper:创建类似if的助手,该助手通过json执行服务器端授权 - ember.js registerBoundHelper: creating an if-like helper that performs server-side authorization via json 向服务器端搜索/ REST API实现ember请求的最佳方法 - Best way to implement ember request to server-side search / rest api 在开始Ember.js时,Rails或服务器端开发人员应该采用什么范式转变? - What paradigm shifts in thinking should a Rails or server-side developer make when beginning Ember.js? Ember oauth2身份验证问题 - Ember oauth2 Authentication issues
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM