Commit d67ca397 authored by David Burke's avatar David Burke

Route to verify-mfa when needed

parent 37a0fc7d
Pipeline #63160297 failed with stages
in 6 minutes and 43 seconds
......@@ -14,12 +14,13 @@ import {
UserMustConfirmEmailAction
} from "../account/account.actions";
import { getToken, IState } from "../app.reducers";
import { Router } from "@angular/router";
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
token: string | null;
constructor(private store: Store<IState>) {
constructor(private store: Store<IState>, private router: Router) {
this.store.pipe(select(getToken)).subscribe(token => (this.token = token));
}
......@@ -48,6 +49,8 @@ export class AuthInterceptor implements HttpInterceptor {
this.store.dispatch(new LogoutSuccessAction());
} else if (detail === "User's email is not confirmed.") {
this.store.dispatch(new UserMustConfirmEmailAction());
} else if (detail === "This user requires MFA verification.") {
this.router.navigate(["/account/login/verify-mfa"]);
}
}
}
......
......@@ -5,11 +5,18 @@ import {
VerifyMfaSuccess,
VerifyMfaFailure
} from "./verify-mfa.actions";
import { withLatestFrom, map, exhaustMap, catchError } from "rxjs/operators";
import {
withLatestFrom,
map,
exhaustMap,
catchError,
tap
} from "rxjs/operators";
import { Store, select } from "@ngrx/store";
import { IState, selectVerifyMfaForm } from "../login.reducer";
import { UserService } from "../../account/user";
import { of } from "rxjs";
import { Router } from "@angular/router";
@Injectable()
export class VerifyMFAEffects {
......@@ -17,18 +24,28 @@ export class VerifyMFAEffects {
verifyMfa$ = this.actions$.pipe(
ofType(VerifyMfaActionTypes.VERIFY_MFA),
withLatestFrom(this.store.pipe(select(selectVerifyMfaForm))),
map(([action, form]) => form.value),
exhaustMap(formValue => {
return this.service.verifyMfa(formValue.otp).pipe(
map(resp => new VerifyMfaSuccess()),
catchError(err => of(new VerifyMfaFailure()))
);
map(([action, form]) => form),
exhaustMap(form => {
if (form.isValid) {
return this.service.verifyMfa(form.value.otp).pipe(
map(resp => new VerifyMfaSuccess()),
catchError(err => of(new VerifyMfaFailure()))
);
}
return of(new VerifyMfaFailure());
})
);
@Effect({ dispatch: false })
verifyMfaSuccess$ = this.actions$.pipe(
ofType(VerifyMfaActionTypes.VERIFY_MFA_SUCCESS),
tap(() => this.router.navigate(["/"]))
);
constructor(
private actions$: Actions,
private store: Store<IState>,
private service: UserService
private service: UserService,
private router: Router
) {}
}
......@@ -66,7 +66,7 @@ export function reducer(
case VerifyMfaActionTypes.VERIFY_MFA_FAILURE:
return {
...state,
errorMessage: "An error occured",
errorMessage: "Unable to verify code.",
hasFinished: false,
hasStarted: false
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment