From a393bd91aba2ed9c576527e775cd41612012e553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BENEDEK=20L=C3=A1szl=C3=B3?= Date: Sat, 7 Jun 2025 15:51:32 +0200 Subject: [PATCH] user module init --- src/app/app.routes.ts | 7 ++++++- src/app/auth/auth-routing.module.ts | 3 ++- src/app/chat/chat-routing.module.ts | 1 + src/app/services/auth.service.ts | 27 ++++++++++++++++++++++++++- src/app/user/user-routing.module.ts | 19 +++++++++++++++++++ src/app/user/user.module.ts | 17 +++++++++++++++++ src/app/user/user/user.component.html | 1 + src/app/user/user/user.component.scss | 0 src/app/user/user/user.component.ts | 18 ++++++++++++++++++ 9 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 src/app/user/user-routing.module.ts create mode 100644 src/app/user/user.module.ts create mode 100644 src/app/user/user/user.component.html create mode 100644 src/app/user/user/user.component.scss create mode 100644 src/app/user/user/user.component.ts diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 3cbf43d..f5240f4 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -11,6 +11,11 @@ export const routes: Routes = [ loadChildren: () => import('./chat/chat.module').then(m => m.ChatModule), canActivate: [IsLoggedInCanActivate] }, + { + path: 'user', + loadChildren: () => import('./user/user.module').then(m => m.UserModule), + canActivate: [IsLoggedInCanActivate] + }, { path: '', redirectTo: 'chat', pathMatch: 'full' }, - { path: '**/*', redirectTo: '', pathMatch: 'full' } + { path: '**', redirectTo: 'chat', pathMatch: 'full' } ]; diff --git a/src/app/auth/auth-routing.module.ts b/src/app/auth/auth-routing.module.ts index df7206b..98119f6 100644 --- a/src/app/auth/auth-routing.module.ts +++ b/src/app/auth/auth-routing.module.ts @@ -6,7 +6,8 @@ import { RegisterComponent } from './register/register.component'; const routes: Routes = [ { path: 'login', component: LoginComponent }, { path: 'register', component: RegisterComponent }, - { path: '', redirectTo: 'login', pathMatch: 'full' } + { path: '', redirectTo: 'login', pathMatch: 'full' }, + { path: '**', redirectTo: 'login', pathMatch: "full" } ]; @NgModule({ diff --git a/src/app/chat/chat-routing.module.ts b/src/app/chat/chat-routing.module.ts index 6c7e949..29b4b30 100644 --- a/src/app/chat/chat-routing.module.ts +++ b/src/app/chat/chat-routing.module.ts @@ -4,6 +4,7 @@ import { ChatComponent } from './chat/chat.component'; const routes: Routes = [ { path: '', component: ChatComponent }, + { path: '**', redirectTo: '', pathMatch: "full" } ]; @NgModule({ diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index d6a4b6c..1c3c588 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -13,6 +13,7 @@ export class AuthService { private readonly USERNAME_FIELD: string = "username"; private readonly PASSWORD_FIELD: string = "password"; private readonly REPEAT_PASSWORD_FIELD: string = "repeatPassword"; + private readonly LOCAL_USERNAME: string = "last_username"; private loggedIn: boolean = false; @@ -25,7 +26,9 @@ export class AuthService { formData.append(this.USERNAME_FIELD, username); formData.append(this.PASSWORD_FIELD, password); - return this.http.post(url, formData, { withCredentials: true }) + localStorage.setItem(this.LOCAL_USERNAME, username); + + return this.http.post(url, formData, { withCredentials: true }); } public Register(username: string, password: string, repeatPassword: string): Observable { @@ -42,6 +45,8 @@ export class AuthService { public Logout(): Observable { let url = `${environment.apiBase}/auth/logout`; + localStorage.removeItem(this.LOCAL_USERNAME); + return this.http.get(url, { withCredentials: true }).pipe( map(() => true), catchError(() => of(false)) @@ -70,6 +75,11 @@ export class AuthService { } public IsLoggedIn(): boolean { return this.loggedIn; } + public GetUsername(): string { + let username = localStorage.getItem(this.LOCAL_USERNAME) + if (username) return username; + else throw new Error("not logged in"); + } } export const IsLoggedInCanActivate: CanActivateFn = ( @@ -89,4 +99,19 @@ export const IsLoggedInCanActivate: CanActivateFn = ( } }) ); +}; + +export const UsernameResolver: CanActivateFn = ( + route: ActivatedRouteSnapshot, + __: RouterStateSnapshot +): boolean => { + const authService = inject(AuthService); + const router = inject(Router); + try { + router.navigateByUrl(`user/${authService.GetUsername()}`); + } catch (err) { + console.log(err); + router.navigateByUrl("auth/login"); + } + return false; }; \ No newline at end of file diff --git a/src/app/user/user-routing.module.ts b/src/app/user/user-routing.module.ts new file mode 100644 index 0000000..631dfa2 --- /dev/null +++ b/src/app/user/user-routing.module.ts @@ -0,0 +1,19 @@ +import { NgModule } from '@angular/core'; +import { RouterModule, Routes } from '@angular/router'; +import { UserComponent } from './user/user.component'; +import { UsernameResolver } from '../services/auth.service'; + +const routes: Routes = [ + { + path: ':username', component: UserComponent + }, + { + path: '', canActivate: [UsernameResolver], component: UserComponent, pathMatch: "full" + }, +]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule] +}) +export class UserRoutingModule { } diff --git a/src/app/user/user.module.ts b/src/app/user/user.module.ts new file mode 100644 index 0000000..50fdd5a --- /dev/null +++ b/src/app/user/user.module.ts @@ -0,0 +1,17 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; + +import { UserRoutingModule } from './user-routing.module'; +import { UserComponent } from './user/user.component'; + + +@NgModule({ + declarations: [ + UserComponent + ], + imports: [ + CommonModule, + UserRoutingModule + ] +}) +export class UserModule { } diff --git a/src/app/user/user/user.component.html b/src/app/user/user/user.component.html new file mode 100644 index 0000000..6b2f937 --- /dev/null +++ b/src/app/user/user/user.component.html @@ -0,0 +1 @@ +

{{username}}

diff --git a/src/app/user/user/user.component.scss b/src/app/user/user/user.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/user/user/user.component.ts b/src/app/user/user/user.component.ts new file mode 100644 index 0000000..0f0b0f2 --- /dev/null +++ b/src/app/user/user/user.component.ts @@ -0,0 +1,18 @@ +import { Component } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; + +@Component({ + selector: 'app-user', + standalone: false, + templateUrl: './user.component.html', + styleUrl: './user.component.scss' +}) +export class UserComponent { + public username!: string; + + constructor(private route: ActivatedRoute) { + this.route.paramMap.subscribe(params => { + this.username = params.get('username')!; + }); + } +}