Skip to content

Commit

Permalink
Memorize location before login in session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Dec 10, 2024
1 parent c4b8080 commit 9b37e8e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export class AuthService {
#csrf = inject(CsrfService);
#userSignal = signal<User | null | undefined>(undefined);

#redirectAfterLogin = '/';

#oidcUserManager: OidcUserManager;

#authUrl = this.#config.authUrl;
Expand Down Expand Up @@ -156,7 +154,7 @@ export class AuthService {
* to the authorization endpoint of the OIDC provider.
*/
async login(): Promise<void> {
this.#redirectAfterLogin = location.pathname;
sessionStorage.setItem('afterLogin', location.pathname);
this.#oidcUserManager.signinRedirect();
}

Expand Down Expand Up @@ -215,7 +213,7 @@ export class AuthService {
await this.#oidcUserManager.removeUser();
this.#userSignal.set(null);
this.#csrf.token = null;
this.#redirectAfterLogin = '/';
sessionStorage.removeItem('afterLogin');
this.#router.navigate(['/']);
});
}
Expand Down Expand Up @@ -454,6 +452,16 @@ export class AuthService {
* Redirect back to the original page after login
*/
redirectAfterLogin() {
this.#router.navigate([this.#redirectAfterLogin]);
let afterLogin = sessionStorage.getItem('afterLogin');
sessionStorage.removeItem('afterLogin');
if (
!afterLogin ||
['/oauth/callback', '/register', '/setup-totp', '/confirm-totp'].some((path) =>
afterLogin!.startsWith(path),
)
) {
afterLogin = '/';
}
this.#router.navigate([afterLogin]);
}
}

0 comments on commit 9b37e8e

Please sign in to comment.