4

私の Ember アプリは、Google OAuth 2 でユーザーを認証する必要があります。認証トークンをデータベースに保存できるようにしたいので、Passport for Node.js を使用して認証プロセスをサーバー側に配置することにしました。

サーバーでの認証が終了したら、Ember アプリに Passport の「セッション」を認識させるにはどうすればよいでしょうか?

4

1 に答える 1

4

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:

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:

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:

{{#if user}}
    <p>Hello, {{user.name}}</p>
{{else}}
    <p>You must authenticate</p>
{{/if}}
于 2013-10-04T14:26:30.557 に答える