diff --git a/apps/ng-client/src/app/services/ory.service.ts b/apps/ng-client/src/app/services/ory.service.ts index e4d4bf46..d1f57446 100644 --- a/apps/ng-client/src/app/services/ory.service.ts +++ b/apps/ng-client/src/app/services/ory.service.ts @@ -20,7 +20,7 @@ export class OryClientService { baseOptions: { withCredentials: true, }, - }) + }), ); } @@ -72,7 +72,16 @@ export class OryClientService { window.location.replace(`${this.basePath}/ui/login`); console.error(error); return throwError(() => error); - }) + }), ); } + + disableSession(sessionId: string): Observable { + return new Observable((subscriber) => { + this.client + .disableMySession({ id: sessionId }) + .then(() => subscriber.next(true)) + .catch((error) => subscriber.error(error)); + }); + } } diff --git a/apps/ng-client/src/app/store/user-store/effects.ts b/apps/ng-client/src/app/store/user-store/effects.ts index 8b49e3f1..1ac52637 100644 --- a/apps/ng-client/src/app/store/user-store/effects.ts +++ b/apps/ng-client/src/app/store/user-store/effects.ts @@ -24,7 +24,7 @@ export class UserStoreEffects { constructor( private actions$: Actions, private userService: UsersService, - private oryClientService: OryClientService + private oryClientService: OryClientService, ) {} private isValidSession(x: Session): x is ValidSession { @@ -62,12 +62,12 @@ export class UserStoreEffects { of( new featureActions.SignUpFailureAction({ error: transformError(error), - }) - ) - ) - ) - ) - ) + }), + ), + ), + ), + ), + ), ); /** @@ -81,11 +81,20 @@ export class UserStoreEffects { withLatestFrom(this.oryClientService.createBrowserLogoutFlow()), switchMap(([session, logoutFlow]) => { if (!this.isValidSession(session)) { - throw new Error('Session is invalid'); + return this.oryClientService.disableSession(session.id).pipe( + // eslint-disable-next-line max-nested-callbacks + switchMap(() => { + return of( + new featureActions.SignInFailureAction({ + error: transformError(new Error('Session is invalid')), + }), + ); + }), + ); } LocalStorageService.setObject( 'user', - this.getUserFromSession(session) + this.getUserFromSession(session), ); LocalStorageService.set('logoutUrl', logoutFlow.logout_url); LocalStorageService.setObject('session', session); @@ -94,33 +103,34 @@ export class UserStoreEffects { token: '', logoutUrl: logoutFlow.logout_url, session, - }) + }), ); }), catchError((error) => { + console.error(error); window.location.replace( - `${this.oryClientService.basePath}/ui/login` + `${this.oryClientService.basePath}/ui/login`, ); return of( new featureActions.SignInFailureAction({ error: transformError(error), - }) + }), ); - }) + }), ); - }) - ) + }), + ), ); signInSuccessEffect$ = createEffect(() => this.actions$.pipe( ofType( - featureActions.ActionTypes.SIGN_IN_SUCCESS + featureActions.ActionTypes.SIGN_IN_SUCCESS, ), map(() => { return new featureActions.LoadCurrentUserAction(); - }) - ) + }), + ), ); signOutEffect$ = createEffect(() => @@ -148,18 +158,18 @@ export class UserStoreEffects { return of( new featureActions.SignOutFailureAction({ error: transformError(error), - }) + }), ); - }) + }), ); - }) - ) + }), + ), ); loadCurrentUserEffect$ = createEffect(() => this.actions$.pipe( ofType( - featureActions.ActionTypes.LOAD_CURRENT_USER + featureActions.ActionTypes.LOAD_CURRENT_USER, ), exhaustMap(() => { return new Observable((observer) => { @@ -192,13 +202,13 @@ export class UserStoreEffects { of( new featureActions.LoadCurrentUserFailureAction({ error: transformError(err), - }) - ) - ) + }), + ), + ), ); - }) + }), ); - }) - ) + }), + ), ); }