added user register

This commit is contained in:
Chneemann 2024-08-04 20:44:26 +02:00
parent b8a6c95910
commit 1d1e6e58d9
7 changed files with 56 additions and 29 deletions

View file

@ -40,7 +40,7 @@ export class LoginComponent {
password: this.authData.password, password: this.authData.password,
}; };
try { try {
await this.authService.login(body); await this.authService.login(body, this.authData.checkbox);
ngForm.resetForm(); ngForm.resetForm();
this.router.navigate(['/browse/']); this.router.navigate(['/browse/']);
this.errorService.clearError(); this.errorService.clearError();

View file

@ -37,7 +37,9 @@
} @else { @if (mail.touched && authData.mail.length > 0 && } @else { @if (mail.touched && authData.mail.length > 0 &&
!isUserEmailValid(authData.mail.toLowerCase())) { !isUserEmailValid(authData.mail.toLowerCase())) {
<p>This is not a valid email format</p> <p>This is not a valid email format</p>
}} }} @if ((errorService.error$ | async)) {
<p>{{ errorService.error$ | async }}</p>
}
</div> </div>
<input <input
type="password" type="password"

View file

@ -1,8 +1,11 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { BtnLargeComponent } from '../../../shared/components/btn-large/btn-large.component'; import { BtnLargeComponent } from '../../../shared/components/btn-large/btn-large.component';
import { FormsModule, NgForm } from '@angular/forms'; import { FormsModule, NgForm } from '@angular/forms';
import { ActivatedRoute, RouterLink } from '@angular/router'; import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { AuthService } from '../../../services/auth.service';
import { ErrorService } from '../../../services/error.service';
import { HttpErrorResponse } from '@angular/common/http';
@Component({ @Component({
selector: 'app-register', selector: 'app-register',
@ -21,7 +24,12 @@ export class RegisterComponent {
registrationSuccess: boolean = false; registrationSuccess: boolean = false;
constructor(private route: ActivatedRoute) {} constructor(
private route: ActivatedRoute,
private authService: AuthService,
public errorService: ErrorService,
private router: Router
) {}
ngOnInit(): void { ngOnInit(): void {
this.route.queryParams.subscribe((params) => { this.route.queryParams.subscribe((params) => {
@ -34,11 +42,25 @@ export class RegisterComponent {
return emailRegex.test(emailValue); return emailRegex.test(emailValue);
} }
onSubmit(ngForm: NgForm) { async onSubmit(ngForm: NgForm) {
if (ngForm.submitted && ngForm.form.valid) { if (ngForm.submitted && ngForm.form.valid) {
console.log(this.authData); const body = {
ngForm.form.reset(); 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.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.');
}
}
} }
} }
} }

View file

@ -4,6 +4,7 @@ section {
height: 100vh; height: 100vh;
width: 100vw; width: 100vw;
background-color: $black; background-color: $black;
overflow: hidden;
} }
.video-overlay { .video-overlay {

View file

@ -1,12 +1,7 @@
import { import { HttpClient } from '@angular/common/http';
HttpClient,
HttpErrorResponse,
HttpHeaders,
} from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { lastValueFrom, Observable } from 'rxjs'; import { lastValueFrom } from 'rxjs';
import { environment } from '../environments/environment.development'; import { environment } from '../environments/environment.development';
import { Router } from '@angular/router';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -14,25 +9,24 @@ import { Router } from '@angular/router';
export class AuthService { export class AuthService {
errorMsg: string | null = null; errorMsg: string | null = null;
constructor(private http: HttpClient, private router: Router) {} constructor(private http: HttpClient) {}
async login(body: any): Promise<void> { async login(body: any, storage: boolean): Promise<void> {
const data = await lastValueFrom( const data = await lastValueFrom(
this.http.post<any>(`${environment.baseUrl}/auth/login/`, body) this.http.post<any>(`${environment.baseUrl}/auth/login/`, body)
); );
const authToken = data.toString(); this.storeAuthToken(data, storage);
localStorage.setItem('authToken', authToken);
} }
getAllUsers(): Observable<any> { storeAuthToken(data: any, storage: boolean) {
const headers = this.getAuthHeaders(); storage
return this.http.get('http://127.0.0.1:8000/users/', { headers }); ? localStorage.setItem('authToken', data.toString())
: sessionStorage.setItem('authToken', data.toString());
} }
private getAuthHeaders(): HttpHeaders { async register(body: any): Promise<void> {
const authToken = localStorage.getItem('authToken'); await lastValueFrom(
return new HttpHeaders({ this.http.post<any>(`${environment.baseUrl}/auth/register/`, body)
Authorization: `token ${authToken}`, );
});
} }
} }

View file

@ -7,7 +7,7 @@
<app-btn-large <app-btn-large
[value]="'Log Out'" [value]="'Log Out'"
[imgPath]="'logout'" [imgPath]="'logout'"
routerLink="/logout" (click)="logout()"
></app-btn-large> ></app-btn-large>
</div> </div>
} @else { } @else {

View file

@ -1,6 +1,6 @@
import { Component, Input, input } from '@angular/core'; import { Component, Input, input } from '@angular/core';
import { BtnLargeComponent } from '../btn-large/btn-large.component'; import { BtnLargeComponent } from '../btn-large/btn-large.component';
import { RouterLink } from '@angular/router'; import { Router, RouterLink } from '@angular/router';
@Component({ @Component({
selector: 'app-header', selector: 'app-header',
@ -11,4 +11,12 @@ import { RouterLink } from '@angular/router';
}) })
export class HeaderComponent { export class HeaderComponent {
@Input() browse: boolean = false; @Input() browse: boolean = false;
constructor(private router: Router) {}
logout() {
localStorage.clear();
sessionStorage.clear();
this.router.navigate(['/']);
}
} }