Skip to content

Commit

Permalink
feat(front): Save and restore path when logging in
Browse files Browse the repository at this point in the history
  • Loading branch information
GordiNoki authored and tsa96 committed Nov 28, 2024
1 parent b2a832a commit 07c59f6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
2 changes: 2 additions & 0 deletions apps/frontend/src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { NotFoundComponent } from './pages/not-found/not-found.component';
import { HomeComponent } from './pages/home/home.component';
import { AuthGuard } from './guards/auth.guard';
import { RoleGuard } from './guards/role.guard';
import { RedirectGuard } from './guards/redirect.guard';

export const APP_ROUTES: Route[] = [
{
path: '',
component: HomeComponent,
canActivate: [RedirectGuard],
title: 'Dashboard'
},
{
Expand Down
23 changes: 2 additions & 21 deletions apps/frontend/src/app/guards/auth.guard.ts
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;
};
18 changes: 18 additions & 0 deletions apps/frontend/src/app/guards/redirect.guard.ts
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;
};
2 changes: 2 additions & 0 deletions apps/frontend/src/app/services/data/local-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as Bitflags from '@momentum/bitflags';
import { AuthService } from './auth.service';
import { HttpService } from './http.service';
import { env } from '../../env/environment';
import { POST_AUTH_REDIRECT_KEY } from '../../guards/redirect.guard';

export type FullUser = User & { profile?: Profile; userStats?: UserStats };

Expand Down Expand Up @@ -62,6 +63,7 @@ export class LocalUserService {
}

public login() {
sessionStorage.setItem(POST_AUTH_REDIRECT_KEY, window.location.pathname);
window.location.href = env.auth + '/web';
}

Expand Down

0 comments on commit 07c59f6

Please sign in to comment.