Skip to content

Commit

Permalink
fix(ng-client): handle better case of invalid Ory session
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Dec 15, 2023
1 parent 42d6321 commit fb59a67
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
13 changes: 11 additions & 2 deletions apps/ng-client/src/app/services/ory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class OryClientService {
baseOptions: {
withCredentials: true,
},
})
}),
);
}

Expand Down Expand Up @@ -72,7 +72,16 @@ export class OryClientService {
window.location.replace(`${this.basePath}/ui/login`);
console.error(error);
return throwError(() => error);
})
}),
);
}

disableSession(sessionId: string): Observable<boolean> {
return new Observable((subscriber) => {
this.client
.disableMySession({ id: sessionId })
.then(() => subscriber.next(true))
.catch((error) => subscriber.error(error));
});
}
}
68 changes: 39 additions & 29 deletions apps/ng-client/src/app/store/user-store/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -62,12 +62,12 @@ export class UserStoreEffects {
of(
new featureActions.SignUpFailureAction({
error: transformError(error),
})
)
)
)
)
)
}),
),
),
),
),
),
);

/**
Expand All @@ -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);
Expand All @@ -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.SignInSuccessAction>(
featureActions.ActionTypes.SIGN_IN_SUCCESS
featureActions.ActionTypes.SIGN_IN_SUCCESS,
),
map(() => {
return new featureActions.LoadCurrentUserAction();
})
)
}),
),
);

signOutEffect$ = createEffect(() =>
Expand Down Expand Up @@ -148,18 +158,18 @@ export class UserStoreEffects {
return of(
new featureActions.SignOutFailureAction({
error: transformError(error),
})
}),
);
})
}),
);
})
)
}),
),
);

loadCurrentUserEffect$ = createEffect(() =>
this.actions$.pipe(
ofType<featureActions.LoadCurrentUserAction>(
featureActions.ActionTypes.LOAD_CURRENT_USER
featureActions.ActionTypes.LOAD_CURRENT_USER,
),
exhaustMap(() => {
return new Observable<UserDto>((observer) => {
Expand Down Expand Up @@ -192,13 +202,13 @@ export class UserStoreEffects {
of(
new featureActions.LoadCurrentUserFailureAction({
error: transformError(err),
})
)
)
}),
),
),
);
})
}),
);
})
)
}),
),
);
}

0 comments on commit fb59a67

Please sign in to comment.