Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Feature/sup 2663 #606

Merged
merged 3 commits into from
Dec 14, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions app/services/tcAuth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

angular.module('tc.services').factory('TcAuthService', TcAuthService);

TcAuthService.$inject = ['CONSTANTS', 'auth', 'AuthTokenService', '$rootScope', '$q', '$log', '$timeout', 'UserService', 'Helpers', 'ApiService', 'store'];
TcAuthService.$inject = ['CONSTANTS', 'auth', 'AuthTokenService', '$rootScope', '$q', '$log', '$timeout', 'UserService', 'Helpers', 'ApiService', 'store', '$http'];

function TcAuthService(CONSTANTS, auth, AuthTokenService, $rootScope, $q, $log, $timeout, UserService, Helpers, ApiService, store) {
function TcAuthService(CONSTANTS, auth, AuthTokenService, $rootScope, $q, $log, $timeout, UserService, Helpers, ApiService, store, $http) {
$log = $log.getInstance("TcAuthServicetcAuth");
var auth0 = auth;
var apiUrl = CONSTANTS.API_URL_V3;
var service = {
login: login,
socialLogin: socialLogin,
Expand Down Expand Up @@ -162,6 +163,18 @@
}

function logout() {
$http({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will quite work. $http call is a promise so even before it's resolved AuthTokenService would be called. I think the solution is to chain these promises

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work?

return $http({
    ...
}).then(function(res) {
    AuthTokenService.removeTokens();
    $rootScope.$broadcast(CONSTANTS.EVENT_USER_LOGGED_OUT);
}).catch(function(err) {
   // handle error
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so :). As long we log exceptions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should move more toward naming variables and passing them in. To me, this would be preferable:

function logout() {
      var logoutOptions = {
        url: apiUrl + '/authorizations/1',
        method: 'DELETE',
        headers: {
          Authorization: 'Bearer ' + AuthTokenService.getV3Token()
        }
      };

      var removeTokens = function() {
            $log.log('Logout successful.');

            AuthTokenService.removeTokens();
            $rootScope.$broadcast(CONSTANTS.EVENT_USER_LOGGED_OUT);
      };

      // Name below according to what it does
      var describesWhatHappensOnError = function(error) {
            $log.error(error);
      };

      $http(logoutOptions)
      .then(removeTokens)
      .catch(describesWhatHappensOnError);
}

What do you guys think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parthshah @nlitwin -- the way I'm doing it now works. These don't need to be chained, since it's fine if the tokens get removed before the DELETE request goes through. I can do the logoutOptions thing to make this cleaner, but I think it's fine to have these calls be independent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is indeed ok to remove the tokens before the DELETE request is finished, do we need $q? I think we might be able to get rid of $q either way since there is nothing asynchronous to wait for with AuthTokenService.removeTokens(); and $rootScope.$broadcast(CONSTANTS.EVENT_USER_LOGGED_OUT); Maybe I'm just not seeing something

url: apiUrl + '/authorizations/1',
method: 'DELETE',
headers: {
'Authorization': "Bearer " + AuthTokenService.getV3Token()
}
}).then(function(res) {
$log.log('logout successful');
}).catch(function(resp) {
$log.error('logout error');
});

return $q(function(resolve, reject) {
AuthTokenService.removeTokens();
$rootScope.$broadcast(CONSTANTS.EVENT_USER_LOGGED_OUT);
Expand Down