From 203de712d9f05f1edda5608851f78c3eadda745c Mon Sep 17 00:00:00 2001
From: Phan Huy Tran
Date: Thu, 13 Feb 2025 10:29:13 +0100
Subject: [PATCH] refactor: Redirect to orginal route after login, restructure
project files
---
frontend/src/app/app.routes.ts | 8 ++++----
frontend/src/app/auth.guard.ts | 14 +++++++++++---
.../home/home.component.html} | 0
.../home/home.component.ts} | 4 ++--
.../landing/landing.component.html} | 0
.../landing/landing.component.ts} | 6 +++---
6 files changed, 20 insertions(+), 12 deletions(-)
rename frontend/src/app/{homepage/homepage/homepage.component.html => feature/home/home.component.html} (100%)
rename frontend/src/app/{homepage/homepage/homepage.component.ts => feature/home/home.component.ts} (85%)
rename frontend/src/app/{landing-page/landing-page.component.html => feature/landing/landing.component.html} (100%)
rename frontend/src/app/{landing-page/landing-page.component.ts => feature/landing/landing.component.ts} (80%)
diff --git a/frontend/src/app/app.routes.ts b/frontend/src/app/app.routes.ts
index 02b958c..73ed20c 100644
--- a/frontend/src/app/app.routes.ts
+++ b/frontend/src/app/app.routes.ts
@@ -1,16 +1,16 @@
import { Routes } from '@angular/router';
-import { LandingPageComponent } from './landing-page/landing-page.component';
-import { HomepageComponent } from './homepage/homepage/homepage.component';
+import { LandingComponent } from './feature/landing/landing.component';
+import { HomeComponent } from './feature/home/home.component';
import { authGuard } from './auth.guard';
export const routes: Routes = [
{
path: '',
- component: LandingPageComponent,
+ component: LandingComponent,
},
{
path: 'home',
- component: HomepageComponent,
+ component: HomeComponent,
canActivate: [authGuard],
},
];
diff --git a/frontend/src/app/auth.guard.ts b/frontend/src/app/auth.guard.ts
index cb2cbe9..0743ff6 100644
--- a/frontend/src/app/auth.guard.ts
+++ b/frontend/src/app/auth.guard.ts
@@ -1,8 +1,11 @@
-import { CanActivateFn } from '@angular/router';
+import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from '@angular/router';
import { inject } from '@angular/core';
import { KeycloakService } from 'keycloak-angular';
-export const authGuard: CanActivateFn = async () => {
+export const authGuard: CanActivateFn = async (
+ route: ActivatedRouteSnapshot,
+ state: RouterStateSnapshot
+) => {
const keycloakService = inject(KeycloakService);
const isLoggedIn = keycloakService.isLoggedIn();
@@ -10,6 +13,11 @@ export const authGuard: CanActivateFn = async () => {
return true;
}
- keycloakService.login();
+ const baseurl = window.location.origin;
+
+ keycloakService.login({
+ redirectUri: `${baseurl}${state.url}`,
+ });
+
return false;
};
diff --git a/frontend/src/app/homepage/homepage/homepage.component.html b/frontend/src/app/feature/home/home.component.html
similarity index 100%
rename from frontend/src/app/homepage/homepage/homepage.component.html
rename to frontend/src/app/feature/home/home.component.html
diff --git a/frontend/src/app/homepage/homepage/homepage.component.ts b/frontend/src/app/feature/home/home.component.ts
similarity index 85%
rename from frontend/src/app/homepage/homepage/homepage.component.ts
rename to frontend/src/app/feature/home/home.component.ts
index ff894dc..06aa423 100644
--- a/frontend/src/app/homepage/homepage/homepage.component.ts
+++ b/frontend/src/app/feature/home/home.component.ts
@@ -5,10 +5,10 @@ import { KeycloakService } from 'keycloak-angular';
selector: 'app-homepage',
standalone: true,
imports: [],
- templateUrl: './homepage.component.html',
+ templateUrl: './home.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
-export class HomepageComponent {
+export class HomeComponent {
private keycloakService: KeycloakService = inject(KeycloakService);
logout() {
diff --git a/frontend/src/app/landing-page/landing-page.component.html b/frontend/src/app/feature/landing/landing.component.html
similarity index 100%
rename from frontend/src/app/landing-page/landing-page.component.html
rename to frontend/src/app/feature/landing/landing.component.html
diff --git a/frontend/src/app/landing-page/landing-page.component.ts b/frontend/src/app/feature/landing/landing.component.ts
similarity index 80%
rename from frontend/src/app/landing-page/landing-page.component.ts
rename to frontend/src/app/feature/landing/landing.component.ts
index 1291434..d2e28c0 100644
--- a/frontend/src/app/landing-page/landing-page.component.ts
+++ b/frontend/src/app/feature/landing/landing.component.ts
@@ -3,12 +3,12 @@ import { KeycloakService } from 'keycloak-angular';
import { RouterLink } from '@angular/router';
@Component({
- selector: 'app-landing-page',
+ selector: 'app-landing',
standalone: true,
imports: [RouterLink],
- templateUrl: './landing-page.component.html',
+ templateUrl: './landing.component.html',
})
-export class LandingPageComponent {
+export class LandingComponent {
private keycloakService: KeycloakService = inject(KeycloakService);
public isLoggedIn = this.keycloakService.isLoggedIn();