-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from Kleostro/feat/tu-01-06/user-authentification
feat(tu-01-06): user authentification
- Loading branch information
Showing
13 changed files
with
196 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { HttpClient } from '@angular/common/http'; | ||
import { inject, Injectable } from '@angular/core'; | ||
|
||
import { Observable } from 'rxjs'; | ||
|
||
import { LogoutResponse } from '../models/logoutResponse'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class LogoutService { | ||
private httpClient = inject(HttpClient); | ||
|
||
public logout(): Observable<LogoutResponse> { | ||
return this.httpClient.delete<LogoutResponse>('logout'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export interface LogoutResponse {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!-- @if (!isLoggedIn) { --> | ||
<p-button text title="Log Out" (click)="logout()"> | ||
<span class="pi pi-sign-out"></span> | ||
</p-button> | ||
<!-- } --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import 'variables'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
import { Router, RouterLink } from '@angular/router'; | ||
|
||
import { ButtonModule } from 'primeng/button'; | ||
import { firstValueFrom } from 'rxjs'; | ||
|
||
import { LogoutService } from '@/app/api/logoutService/logout.service'; | ||
|
||
import { NavigationComponent } from '../../../core/components/navigation/navigation.component'; | ||
import { LocalStorageService } from '../../../core/services/local-storage/local-storage.service'; | ||
|
||
@Component({ | ||
selector: 'app-logout', | ||
standalone: true, | ||
imports: [ButtonModule, NavigationComponent, RouterLink], | ||
|
||
templateUrl: './logout.component.html', | ||
styleUrl: './logout.component.scss', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class LogoutComponent { | ||
public localStorageService = inject(LocalStorageService); | ||
private logoutService: LogoutService = inject(LogoutService); | ||
private router = inject(Router); | ||
|
||
public logout(): void { | ||
firstValueFrom(this.logoutService.logout()) | ||
.then(() => { | ||
this.localStorageService.clear(); | ||
this.router.navigate(['/sign-in']); | ||
}) | ||
.catch(() => { | ||
// TBD: handle error | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
<p>login works!</p> | ||
<section class="login-container"> | ||
<form class="login-form" [formGroup]="loginForm" (ngSubmit)="submitForm()"> | ||
<h2 class="login-title">Sign In</h2> | ||
|
||
<div class="login-form-item"> | ||
<label for="email" class="login-label">Email</label> | ||
<div class="login-field"> | ||
<input | ||
id="email" | ||
type="email" | ||
pInputText | ||
formControlName="email" | ||
placeholder="Enter your email" | ||
class="login-input p-inputtext" | ||
/> | ||
@if (loginForm.get('email')?.invalid && loginForm.get('email')?.touched) { | ||
@if (loginForm.get('email')?.errors?.['required']) { | ||
<small class="p-error">Please input your email!</small> | ||
} | ||
} | ||
</div> | ||
</div> | ||
<div class="login-form-item"> | ||
<label for="password" class="login-label">Password</label> | ||
<div class="login-field"> | ||
<input | ||
id="password" | ||
type="password" | ||
pInputText | ||
formControlName="password" | ||
placeholder="Enter your password" | ||
class="login-input p-inputtext" | ||
/> | ||
@if (loginForm.get('password')?.invalid && loginForm.get('password')?.touched) { | ||
@if (loginForm.get('password')?.errors?.['required']) { | ||
<small class="p-error">Please input your password!</small> | ||
} | ||
} | ||
</div> | ||
</div> | ||
<div class="login-button-container"> | ||
<p-button type="submit" label="Sign in" [disabled]="!loginForm.valid" class="p-mt-2"></p-button> | ||
<p-link pButton routerLink="/sign-up" label="Sign Up" class="p-button-text p-mt-2"></p-link> | ||
</div> | ||
</form> | ||
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.login-container { | ||
display: flex; | ||
justify-content: center; | ||
height: 100%; | ||
} | ||
|
||
.login-form { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
|
||
width: 30rem; | ||
height: auto; | ||
margin: auto; | ||
padding: 2rem; | ||
|
||
background-color: rgb(199 199 199 / 14%); | ||
box-shadow: 0 0 10px 0 rgb(0 0 0 / 30%); | ||
|
||
.login-title { | ||
margin-bottom: 1rem; | ||
font-weight: 700; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,65 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; | ||
import { Router, RouterLink } from '@angular/router'; | ||
|
||
import { ButtonModule } from 'primeng/button'; | ||
import { InputTextModule } from 'primeng/inputtext'; | ||
import { PasswordModule } from 'primeng/password'; | ||
import { firstValueFrom } from 'rxjs'; | ||
|
||
import { OverriddenHttpErrorResponse } from '@/app/api/models/errorResponse'; | ||
import { SignInResponse } from '@/app/api/models/signInResponse'; | ||
import { User } from '@/app/api/models/user'; | ||
import { SignInService } from '@/app/api/signInService/sign-in.service'; | ||
import { LocalStorageService } from '@/app/core/services/local-storage/local-storage.service'; | ||
|
||
@Component({ | ||
selector: 'app-login', | ||
standalone: true, | ||
imports: [], | ||
imports: [CommonModule, ReactiveFormsModule, InputTextModule, ButtonModule, PasswordModule, RouterLink], | ||
templateUrl: './login.component.html', | ||
styleUrl: './login.component.scss', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class LoginComponent {} | ||
export class LoginComponent { | ||
private signInService = inject(SignInService); | ||
private localStorageService = inject(LocalStorageService); | ||
private router = inject(Router); | ||
private fb = inject(FormBuilder); | ||
|
||
public loginForm = this.fb.group({ | ||
email: this.fb.control<string>('', [Validators.required.bind(this), Validators.email.bind(this)]), | ||
password: this.fb.control<string>('', [Validators.required.bind(this)]), | ||
}); | ||
|
||
public submitForm(): void { | ||
if (this.loginForm.valid) { | ||
firstValueFrom(this.signInService.signIn(this.userData)) | ||
.then((data: SignInResponse) => { | ||
this.localStorageService.addValueByKey('email', this.userData.email); | ||
this.localStorageService.addValueByKey('token', data.token); | ||
this.router.navigate(['/']); | ||
}) | ||
.catch((err: OverriddenHttpErrorResponse) => { | ||
this.loginForm.setErrors({ [err.error.reason]: true }); | ||
}); | ||
} else { | ||
Object.values(this.loginForm.controls).forEach((control) => { | ||
if (control.invalid) { | ||
control.markAsTouched(); | ||
control.updateValueAndValidity({ | ||
onlySelf: true, | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private get userData(): User { | ||
return { | ||
email: this.loginForm.controls.email.value || '', | ||
password: this.loginForm.controls.password.value || '', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters