Skip to content

Commit

Permalink
Fixed password reset redirect logic, added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Aug 4, 2015
1 parent 1ac99a4 commit a98c8bd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/src/app/user/profile/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module app.user.profile {
constructor(private stateHelperServiceProvider){

let state:global.IState = {
url: '/profile?passwordResetToken',
url: '/profile',
views: {
"main@app.user": {
controller: namespace+'.controller',
Expand Down
23 changes: 18 additions & 5 deletions app/src/common/services/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ module common.services.auth {

}

/**
* Initialise the NgJwtAuthService
* @returns {ng.IPromise<any>}
*/
private initialiseJwtAuthService() {

let jwtAuthServiceInitialisedPromise = this.ngJwtAuthService
return this.ngJwtAuthService
.registerUserFactory((subClaim: string, tokenData: global.JwtAuthClaims): ng.IPromise<common.models.User> => {
return this.$q.when(new common.models.User(tokenData._user));
})
Expand All @@ -56,10 +60,12 @@ module common.services.auth {
})
.init(); //initialise the auth service (kicks off the timers etc)

return jwtAuthServiceInitialisedPromise;

}

/**
* Check the address bar for a new jwt token to process
* @returns {any}
*/
private processQueryToken():ng.IPromise<any> {

this.removeFacebookHash();
Expand Down Expand Up @@ -88,15 +94,22 @@ module common.services.auth {

}

/**
* Check the url for password reset token and process it
* @returns {any}
*/
private processPasswordResetToken():ng.IPromise<any> {

let queryParams = this.$location.search();
if (_.isEmpty(queryParams.passwordResetToken)) {
return this.$q.when(true); //immediately resolve
}

return this.ngJwtAuthService.exchangeToken(queryParams.passwordResetToken)
.then(null, (err) => {
let token = queryParams.passwordResetToken;
this.$location.search('passwordResetToken', null);

return this.ngJwtAuthService.exchangeToken(token)
.catch((err) => {
this.$mdToast.show(
this.$mdToast.simple()
.hideDelay(2000)
Expand Down
45 changes: 33 additions & 12 deletions app/src/config/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,57 @@ module config.stateManager {
this.registerStatePermissions();
}

private registerStatePermissions = () => {

this.$rootScope.$on('$stateChangeStart', (event, toState:global.IState, toParams, fromState:global.IState, fromParams) => {
/**
* Register the state change start transition
* 1) Verify if the user must be logged in to state
* 2) If so and user is not logged in, stop the transition and prompt the user to log in after waiting for
* the auth service to initialise
* 3) Once the user has authenticated redirect them to the state they were trying to access
* 4) If they fail to authenticate, return them to the home state.
*/
private registerStatePermissions = ():void => {

this.$rootScope.$on('$stateChangeStart', (event, toState:global.IState, toParams, fromState:global.IState, fromParams):ng.IPromise<any> => {

if (this.userMustBeLoggedIn(toState) && !this.ngJwtAuthService.loggedIn) {

event.preventDefault();

//defer prompting for login until the auth service has completed all checks
this.authService.initialisedPromise.finally(() => {
this.showLoginAndRedirectTo(toState, toParams, fromState);
return this.authService.initialisedPromise.finally(() => {

if(this.ngJwtAuthService.loggedIn){ //user is still not logged in after authentication initialisation
return this.$state.go(toState.name, toParams);
}

return this.showLoginAndRedirectTo(toState, toParams, fromState);

});

}

})
});

};

private showLoginAndRedirectTo = (toState:global.IState, toParams, fromState:global.IState) => {
this.ngJwtAuthService.requireCredentialsAndAuthenticate()
.then(() => {
this.$state.go(toState.name, toParams);
}, (err) => {
/**
* Show the login to the user then redirect to their intended state or the home state if login fails.
* @param toState
* @param toParams
* @param fromState
* @returns {IPromise<TResult>}
*/
private showLoginAndRedirectTo = (toState:global.IState, toParams, fromState:global.IState):ng.IPromise<any> => {

return this.ngJwtAuthService.requireCredentialsAndAuthenticate()
.then(() => this.$state.go(toState.name, toParams),
(err) => {

let returnTo = fromState.name ? fromState.name : 'app.guest.home';

let attemptedStateName = this.$state.href(toState, toParams);

this.$state.go(returnTo).then(() => {
return this.$state.go(returnTo).then(() => {

this.$mdToast.show(
this.$mdToast.simple()
Expand Down

0 comments on commit a98c8bd

Please sign in to comment.