lynndylanhurley / ng-token-auth

Token based authentication module for angular.js.
Do What The F*ck You Want To Public License
1.8k stars 233 forks source link

signup and signout promises not successfully resolving #294

Open sachinkaria opened 8 years ago

sachinkaria commented 8 years ago

I have managed to set up ng-token-auth to work with rails devise-token-auth. However when I try to sign up and then login the promise doesn't resolve even though the user is successfully created in the rails database. If the sign up promise resolves the user should automatically be logged in however it never executes this.

Here is my controller:

comeDineApp.controller('userController', ['ipCookie','$scope', '$state', '$auth',
  function(ipCookie, $scope, $state, $auth){

    $scope.handleRegBtnClick = function() {
      $auth.submitRegistration($scope.registrationForm)
        .then(function() {
          $auth.submitLogin({
            email: $scope.registrationForm.email,
            password: $scope.registrationForm.password
          });
        });
    };
}]);

In this case submitLogin function is never executed even though a user is successfully created. Here is the rails feedback I get when creating a user:

Started POST "/auth" for ::1 at 2016-05-31 10:44:35 +0100
Processing by DeviseTokenAuth::RegistrationsController#create as HTML
  Parameters: {"email"=>"cool@cool.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "confirm_success_url"=>"http://localhost:8100/#/user/sign_up", "config_name"=>"default", "registration"=>{"email"=>"cool@cool.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "confirm_success_url"=>"http://localhost:8100/#/user/sign_up", "config_name"=>"default"}}
Unpermitted parameters: confirm_success_url, config_name, registration
Unpermitted parameters: confirm_success_url, config_name, registration
Unpermitted parameters: confirm_success_url, config_name, registration
   (0.2ms)  BEGIN
   (0.3ms)  SELECT COUNT(*) FROM "users" WHERE "users"."provider" = $1 AND "users"."email" = $2  [["provider", "email"], ["email", "cool@cool.com"]]
  SQL (0.4ms)  INSERT INTO "users" ("email", "encrypted_password", "tokens", "uid", "confirmed_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["email", "cool@cool.com"], ["encrypted_password", "$2a$10$SwIccp70rj7Fkhr/mmbzzO0iWZ25zoWmZHhd.Ss.U7EiHY3HDKg5a"], ["tokens", "{}"], ["uid", "cool@cool.com"], ["confirmed_at", "2016-05-31 09:44:35.290653"], ["created_at", "2016-05-31 09:44:35.290790"], ["updated_at", "2016-05-31 09:44:35.290790"]]
   (0.6ms)  COMMIT
   (0.2ms)  BEGIN
  SQL (0.4ms)  UPDATE "users" SET "tokens" = $1, "uid" = $2, "confirmed_at" = $3, "updated_at" = $4 WHERE "users"."id" = $5  [["tokens", "{\"l8gjteNazg85SmNVb98GDg\":{\"token\":\"$2a$10$ZFFMheopZltbe3aLZKEQy./GKqoDzXuWrLlVxZaNCdLd0z78hAZqG\",\"expiry\":1465897475}}"], ["uid", "e85f6997-4499-49c5-b4b3-16a6d182f1ee"], ["confirmed_at", "2016-05-31 09:44:35.378804"], ["updated_at", "2016-05-31 09:44:35.379174"], ["id", 12]]
   (0.5ms)  COMMIT
Completed 200 OK in 330ms (Views: 0.3ms | ActiveRecord: 2.5ms)

If I manually redirect to the login page I can successfully login. This problem is also happening when I try signOut. The user successfully signs out of the rails API however the promise is not resolved in Angular and the page doesn't redirect.

sachinkaria commented 8 years ago

This works perfectly with signing IN.

abhayastudios commented 8 years ago

Not sure if this is related but I had something similar, where after registration the login event was not emitted. Therefore I broadcast this event myself:

$rootScope.$on('auth:registration-email-success', function(event, user) {
  $log.debug('event auth:registration-email-success: ' + user.email);
  $rootScope.$broadcast('auth:login-success',user);
});

Then I use events to do the state change:

$rootScope.$on('auth:login-success', function(event, user) {
  $log.debug('event auth:login-success');
  // ensure $rootScope contains user object
  $auth.validateUser().then(function(resp) {
    $state.go('some_state');
  });
});
poc7667 commented 8 years ago

+1 I have this issue with gem 'devise_token_auth' on my backend server

pierrettemugisha commented 8 years ago

@abhayastudios In which part of your code did you do this? I think I have the same issue as my use is logged in but I can't go to another state. Although my app is in HTML and Java. But that is not important, I guess. I tried using those events but I think I am putting them in the wrong place in my code as they do not resolve my issue...

sachinkaria commented 8 years ago

I haven't found a solution to this issue yet, the promises still don't resolve for sign-up and sign-out