0

When authenticating with correct username/password with oauth2 authorize it successfully authenticate. But when refreshing the page with authenticate is true the user object is lost. I am using ember-cli.

Here are all the related code for this:

Initializer for 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);
      }
    });
  }
};

Application route :

//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);
        }
      })
    }
  }
});

User model for the app

//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;

The whole code is also here: Gist for the code

Pranaya Behera
  • 545
  • 1
  • 9
  • 24
  • It's better to use a custom session class as shown in e.g. this example https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html than to reopen the default `Session` class. You'd want to check whether doing that already helps you. – marcoow Aug 13 '14 at 09:46
  • So in place of Session.reopen I should do `Session.extend(user: function() { return UserModel.currentUser(); }.property());` ? – Pranaya Behera Aug 13 '14 at 10:10
  • yes, and then register that class in Ember's container and configure that to be used for Ember Simple Auth's session like in this example: https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html – marcoow Aug 13 '14 at 12:43
  • Marcoow: You are talking about removing the `Session.reopen` bit and In another file e.g. `current-user.js` , insert the `Session.extend();` bit and then as the example that you have linked to register it as `container.register('session:custom', App.CustomSession);` https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html#L107 – Pranaya Behera Aug 13 '14 at 12:57
  • Yeah, that's what I'm talking about – marcoow Aug 14 '14 at 20:46

0 Answers0