0

oauth2 authorize で正しいユーザー名/パスワードで認証すると、正常に認証されます。ただし、認証を使用してページを更新すると、ユーザー オブジェクトは失われます。私はember-cliを使用しています。

これに関連するすべてのコードを次に示します。

ember-simple-auth の初期化子

//initializers/authentication.js

import OAuth2 from 'simple-auth-oauth2/authenticators/oauth2';
import Session from 'simple-auth/session';
import UserModel from 'client/models/user';

export default {
  name: 'authentication',
  before: 'simple-auth',

  initialize: function(container) {
    window.ENV = window.ENV || {};
    window.ENV['simple-auth'] = {
      authenticationRoute: 'login',
      routeAfterAuthentication: 'me.index',
      authorizer: 'simple-auth-authorizer:oauth2-bearer',
      crossOriginWhitelist: [ClientENV.apiEndpoint]
    };

    window.ENV['simple-auth-oauth2'] = {
      serverTokenEndpoint: ClientENV.apiEndpoint + '/auth',
      refreshAccessTokens: true,
    };

    // This doesn't work. This gets called but never gets saved in the session object.
    Session.reopen({
      user: function() {
        return UserModel.currentUser().then(function(user) {
          return user;
        });
      }.property()
    });

    OAuth2.reopen({
      makeRequest: function (url, data) {
        data.client_id = 'web-client';
        return this._super(url, data);
      }
    });
  }
};

応募ルート:

//routes/application.js

import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
import UserModel from 'client/models/user';
import Configuration from 'simple-auth/configuration';

export default Ember.Route.extend(ApplicationRouteMixin, {
  actions: {
    sessionAuthenticationSucceeded: function() {
      var self = this;
      UserModel.currentUser().then(function(user) {
        self.get('session').set('user', user);
        var attemptedTransition = self.get('session').get('attemptedTransition');
        if (attemptedTransition) {
          attemptedTransition.retry();
          self.get('session').set('attemptedTransition', null);
        } else {
          self.transitionTo(Configuration.routeAfterAuthentication);
        }
      })
    }
  }
});

アプリのユーザー モデル

//models/user.js
import Ember from 'ember';
import { request } from 'ic-ajax';

var UserModel = Ember.Object.extend({
  username: null,
  email: null,
  firstname: null,
  lastname: null,
  role: null,

  fullname: function() {
    return this.get('firstname') + ' ' + this.get('lastname');
  }.property('firstname', 'lastname')

});

UserModel.reopenClass({
  currentUser: function() {
    return request({
      url: ClientENV.apiEndpoint + '/user/me',
      type: 'GET'
    });
  }
});

export default UserModel;

コード全体もここにあります: Gist for the code

4

0 に答える 0