javascriptauthenticationember.jsember-app-kit

Login after successful signup Ember-Simple-Auth


I have set up a auto-authenticate for user after signup:

app/libs/auto-authenticate.js

export default Ember.SimpleAuth.Authenticators.OAuth2.extend({                  
   authenticate: function(credentials) {                                                        
     if(!Ember.isEmpty(credentials.access_token)) {                               
       return Ember.RSVP.resolve(credentials);                                                  
     } else {                                                                                   
       return this._super(credentials);                                                         
     }                                                                                          
   }                                                                                            
}); 

app/app.js

import AutoAuthenticate from 'appkit/libs/auto-authenticate';
App.initializer({                                                                              
    name: 'auto',                                                                                
    initialize: function(container, application) {                                               
      container.register('app:authenticators:custom', AutoAuthenticator);         
      Ember.SimpleAuth.setup(container, application);                                            
    }                                                                             
 });

app/controllers/register.js

  export default Ember.ObjectController.extend({
    firstname: '',                                                                
    lastname: '',                                                                                
    username: '',                                                                                
    password: '',                                                                                                                                                                             
    actions: {                                                                                   
      registerUser: function(){                                                                  
        var self = this;                                                                         
        var user = this.store.createRecord('user', {                                             
          first_name: this.get('firstname'),                                       
          last_name: this.get('lastname'),                                                       
          username: this.get('username')                                                         
        });
        user.set('typedPass', this.get('password'));                                             
        user.save().then(function(){                                                             
          //How can I login this user using ember-simple-auth ?? 
        });                                                                       
      }                                                                                          
    }                                                                                            
 });

I have separate Login for users that will provide their username and password.

What I want to do is , when a new user signs up in the website I want that user to logged in directly without going to the login page and providing its username/password ? As I am getting the username and password from the registration process, I dont want the user to go to another route and then login . How to call the custom authenticator to authenticate the current signed up user with its login credentials??


Solution

  • The best solution would probably to just reuse the username and password properties you already have in the registration controller:

    export default Ember.ObjectController.extend({
      firstname: '',                                                                
      lastname: '',                                                                                
      username: '',                                                                                
      password: '',                                                                                                                                                                             
      actions: {                                                                                   
        registerUser: function(){                                                                  
          var self = this;                                                                         
          var user = this.store.createRecord('user', {                                             
            first_name: this.get('firstname'),                                       
            last_name: this.get('lastname'),                                                       
            username: this.get('username')                                                         
          });
          user.set('typedPass', this.get('password'));                                             
          user.save().then(function() {                                                             
            //this is basically what happens when you trigger the LoginControllerMixin's "authenticate" action
            this.get('session').authenticate('app:authenticators:custom', {
              identification: this.get('username'),
              password: this.get('password')
            });
          });                                                                       
        }                                                                                          
      }                                                                                            
    

    });