-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(front): Save and restore path when logging in
- Loading branch information
Showing
4 changed files
with
24 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,16 @@ | ||
import { inject } from '@angular/core'; | ||
import { CanActivateFn, Router } from '@angular/router'; | ||
import { CanActivateFn } from '@angular/router'; | ||
import { LocalUserService } from '../services/data/local-user.service'; | ||
|
||
const POST_AUTH_REDIRECT_KEY = 'postAuthLocation'; | ||
|
||
export const AuthGuard: CanActivateFn = () => { | ||
const router = inject(Router); | ||
const userService = inject(LocalUserService); | ||
|
||
// Check whether redirect URL exists in session storage. If this is the case, | ||
// this is the second time this function has been called: the user failed auth | ||
// once, the URL was stored, then they were redirected back to the dashboard. | ||
// So, now we redirect remove the token (so no infinite loop), and redirect | ||
// back to their original requested URL on this site. | ||
const redirect = sessionStorage.getItem(POST_AUTH_REDIRECT_KEY); | ||
if (redirect) { | ||
sessionStorage.removeItem(POST_AUTH_REDIRECT_KEY); | ||
return router.parseUrl('/' + redirect); | ||
} | ||
|
||
// This guard passes iff the client is logged in (essentially, if they | ||
// This guard passes if the client is logged in (essentially, if they | ||
// have an access token in local storage). | ||
if (userService.isLoggedIn) { | ||
return true; | ||
} | ||
|
||
// They're not logged in, so store current path in session storage and send | ||
// them over to Steam | ||
if (window.location.pathname !== '/') | ||
sessionStorage.setItem(POST_AUTH_REDIRECT_KEY, window.location.pathname); | ||
|
||
userService.login(); | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { inject } from '@angular/core'; | ||
import { CanActivateFn, Router } from '@angular/router'; | ||
|
||
export const POST_AUTH_REDIRECT_KEY = 'postAuthLocation'; | ||
|
||
export const RedirectGuard: CanActivateFn = () => { | ||
const router = inject(Router); | ||
|
||
// Check whether redirect URL exists in session storage. If this is the case, | ||
// we redirect to this path and remove it from storage (to prevent infinite loop) | ||
const redirect = sessionStorage.getItem(POST_AUTH_REDIRECT_KEY); | ||
if (redirect) { | ||
sessionStorage.removeItem(POST_AUTH_REDIRECT_KEY); | ||
return router.parseUrl('/' + redirect); | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters