From 1d1e6e58d9bf9351b04636cf588b00b29af58b15 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Sun, 4 Aug 2024 20:44:26 +0200 Subject: [PATCH] added user register --- .../components/auth/login/login.component.ts | 2 +- .../auth/register/register.component.html | 4 ++- .../auth/register/register.component.ts | 34 +++++++++++++++---- .../home/browse/browse.component.scss | 1 + frontend/src/app/services/auth.service.ts | 32 +++++++---------- .../components/header/header.component.html | 2 +- .../components/header/header.component.ts | 10 +++++- 7 files changed, 56 insertions(+), 29 deletions(-) diff --git a/frontend/src/app/components/auth/login/login.component.ts b/frontend/src/app/components/auth/login/login.component.ts index 044cff9..8dc5deb 100644 --- a/frontend/src/app/components/auth/login/login.component.ts +++ b/frontend/src/app/components/auth/login/login.component.ts @@ -40,7 +40,7 @@ export class LoginComponent { password: this.authData.password, }; try { - await this.authService.login(body); + await this.authService.login(body, this.authData.checkbox); ngForm.resetForm(); this.router.navigate(['/browse/']); this.errorService.clearError(); diff --git a/frontend/src/app/components/auth/register/register.component.html b/frontend/src/app/components/auth/register/register.component.html index b1b5c16..55366a1 100644 --- a/frontend/src/app/components/auth/register/register.component.html +++ b/frontend/src/app/components/auth/register/register.component.html @@ -37,7 +37,9 @@ } @else { @if (mail.touched && authData.mail.length > 0 && !isUserEmailValid(authData.mail.toLowerCase())) {

This is not a valid email format

- }} + }} @if ((errorService.error$ | async)) { +

{{ errorService.error$ | async }}

+ } { @@ -34,11 +42,25 @@ export class RegisterComponent { return emailRegex.test(emailValue); } - onSubmit(ngForm: NgForm) { + async onSubmit(ngForm: NgForm) { if (ngForm.submitted && ngForm.form.valid) { - console.log(this.authData); - ngForm.form.reset(); - this.registrationSuccess = true; + const body = { + email: this.authData.mail, + username: this.authData.mail.split('@')[0], + password: this.authData.password, + }; + try { + await this.authService.register(body); + ngForm.resetForm(); + this.registrationSuccess = true; + this.errorService.clearError(); + } catch (error) { + if (error instanceof HttpErrorResponse) { + this.errorService.setError(error.error.detail); + } else { + this.errorService.setError('An unknown error has occurred.'); + } + } } } } diff --git a/frontend/src/app/components/home/browse/browse.component.scss b/frontend/src/app/components/home/browse/browse.component.scss index e2a2fc4..c9f5d1d 100644 --- a/frontend/src/app/components/home/browse/browse.component.scss +++ b/frontend/src/app/components/home/browse/browse.component.scss @@ -4,6 +4,7 @@ section { height: 100vh; width: 100vw; background-color: $black; + overflow: hidden; } .video-overlay { diff --git a/frontend/src/app/services/auth.service.ts b/frontend/src/app/services/auth.service.ts index 18ba4dc..669facb 100644 --- a/frontend/src/app/services/auth.service.ts +++ b/frontend/src/app/services/auth.service.ts @@ -1,12 +1,7 @@ -import { - HttpClient, - HttpErrorResponse, - HttpHeaders, -} from '@angular/common/http'; +import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { lastValueFrom, Observable } from 'rxjs'; +import { lastValueFrom } from 'rxjs'; import { environment } from '../environments/environment.development'; -import { Router } from '@angular/router'; @Injectable({ providedIn: 'root', @@ -14,25 +9,24 @@ import { Router } from '@angular/router'; export class AuthService { errorMsg: string | null = null; - constructor(private http: HttpClient, private router: Router) {} + constructor(private http: HttpClient) {} - async login(body: any): Promise { + async login(body: any, storage: boolean): Promise { const data = await lastValueFrom( this.http.post(`${environment.baseUrl}/auth/login/`, body) ); - const authToken = data.toString(); - localStorage.setItem('authToken', authToken); + this.storeAuthToken(data, storage); } - getAllUsers(): Observable { - const headers = this.getAuthHeaders(); - return this.http.get('http://127.0.0.1:8000/users/', { headers }); + storeAuthToken(data: any, storage: boolean) { + storage + ? localStorage.setItem('authToken', data.toString()) + : sessionStorage.setItem('authToken', data.toString()); } - private getAuthHeaders(): HttpHeaders { - const authToken = localStorage.getItem('authToken'); - return new HttpHeaders({ - Authorization: `token ${authToken}`, - }); + async register(body: any): Promise { + await lastValueFrom( + this.http.post(`${environment.baseUrl}/auth/register/`, body) + ); } } diff --git a/frontend/src/app/shared/components/header/header.component.html b/frontend/src/app/shared/components/header/header.component.html index 366d714..074a09d 100644 --- a/frontend/src/app/shared/components/header/header.component.html +++ b/frontend/src/app/shared/components/header/header.component.html @@ -7,7 +7,7 @@ } @else { diff --git a/frontend/src/app/shared/components/header/header.component.ts b/frontend/src/app/shared/components/header/header.component.ts index 89ab706..fa1f50e 100644 --- a/frontend/src/app/shared/components/header/header.component.ts +++ b/frontend/src/app/shared/components/header/header.component.ts @@ -1,6 +1,6 @@ import { Component, Input, input } from '@angular/core'; import { BtnLargeComponent } from '../btn-large/btn-large.component'; -import { RouterLink } from '@angular/router'; +import { Router, RouterLink } from '@angular/router'; @Component({ selector: 'app-header', @@ -11,4 +11,12 @@ import { RouterLink } from '@angular/router'; }) export class HeaderComponent { @Input() browse: boolean = false; + + constructor(private router: Router) {} + + logout() { + localStorage.clear(); + sessionStorage.clear(); + this.router.navigate(['/']); + } }