Skip to content

Commit

Permalink
#28: bug with the login page was fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
vlavrynovych committed Aug 16, 2017
1 parent 72e3c1b commit e044d16
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/angular-spa-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@
}

function goTo(route) {
$location.path(route);
info('Redirected to the ' + route);
if(route == config.uiRoutes.login && service.isAuthenticated()) {
$location.path(getHome());
} else {
$location.path(route);
info('Redirected to the ' + route);
}
}

function isAuthenticated() {
Expand Down
43 changes: 43 additions & 0 deletions test/test.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('angular-spa-auth', function () {
it('User is not logged in on start', function () {
//given:
expect($rootScope.currentUser).toBeUndefined();
events = [];

$rootScope.$on("$routeChangeStart", function(e) {
e.path = $location.path();
Expand Down Expand Up @@ -118,6 +119,48 @@ describe('angular-spa-auth', function () {
checkEvent(true);
});


it('Refresh when you are on the login page and login', function () {
//given:
expect($rootScope.currentUser).toBeUndefined();
events = [];
AuthService.config.uiRoutes.target = AuthService.config.uiRoutes.login;

$rootScope.$on("$routeChangeStart", function(e) {
e.path = $location.path();
e.i = events.length;
events.push(e);
});

//when: isAuthenticated method is triggered but we still do not know status of user
changeRoute(AuthService.config.uiRoutes.login);

//then: login page is available
expect($location.path()).toEqual(AuthService.config.uiRoutes.login);
expect($rootScope.currentUser).toBeUndefined();
checkEvent(false);

//when: trigger after init
$httpBackend.flush();

//then: we are on login page and user is set to false, because we checked his authentication status
expect($location.path()).toEqual(AuthService.config.uiRoutes.login);
expect($rootScope.currentUser).toEqual(false);

//when: try to login using some credentials
AuthService.login(credentials);
$httpBackend.flush();

//then: successfully authenticated and redirected to the home page EVEN if the target is login
expect($location.path()).toEqual(AuthService.config.uiRoutes.home);

//when: try again to go to the login page
changeRoute(AuthService.config.uiRoutes.login);

//then: successfully authenticated and redirected to the home page EVEN if the target is login
expect($location.path()).toEqual(AuthService.config.uiRoutes.home);
});

function changeRoute(path) {
$location.path(path);
$rootScope.$broadcast("$routeChangeStart", {
Expand Down

0 comments on commit e044d16

Please sign in to comment.