Merge pull request 'refactor: Redirect to orginal route after login, restructure project files' (!24) from refactor/auth-guard into main
Reviewed-on: https://git.simonis.lol/projects/casino/pulls/24 Reviewed-by: jank1619 <jan@kjan.email> Reviewed-by: Constantin Simonis <constantin@simonis.lol>
This commit is contained in:
commit
19ab3933d6
6 changed files with 20 additions and 12 deletions
|
@ -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],
|
||||
},
|
||||
];
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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() {
|
|
@ -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();
|
Reference in a new issue