Fin section 17 : authentification

This commit is contained in:
Lucien Cartier-Tilet 2023-02-27 15:58:23 +01:00
parent f4a6d2dfeb
commit b5d062ebca
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
7 changed files with 152 additions and 6 deletions

View File

@ -1,9 +1,11 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component'; import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
const routes: Routes = [ const routes: Routes = [
{ path: '', redirectTo: 'pokemons', pathMatch: 'full' }, { path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: '**', component: PageNotFoundComponent }, { path: '**', component: PageNotFoundComponent },
]; ];

View File

@ -9,9 +9,10 @@ import { AppComponent } from './app.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component'; import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { PokemonModule } from './pokemon/pokemon.module'; import { PokemonModule } from './pokemon/pokemon.module';
import { InMemoryDataService } from './in-memory-data.service'; import { InMemoryDataService } from './in-memory-data.service';
import { LoginComponent } from './login/login.component';
@NgModule({ @NgModule({
declarations: [AppComponent, PageNotFoundComponent], declarations: [AppComponent, PageNotFoundComponent, LoginComponent],
imports: [ imports: [
BrowserModule, BrowserModule,
FormsModule, FormsModule,

17
src/app/auth.guard.ts Normal file
View File

@ -0,0 +1,17 @@
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate() {
if (this.authService.isLoggedIn) {
return true;
}
this.router.navigate(['/login']);
return false;
}
}

22
src/app/auth.service.ts Normal file
View File

@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import { delay, Observable, of, tap } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class AuthService {
isLoggedIn: boolean = false;
redirectUrl: string;
login(name: string, password: string): Observable<boolean> {
const isLoggedIn = name === 'pikachu' && password === 'pikachu';
return of(isLoggedIn).pipe(
delay(1000),
tap((isLoggedIn) => (this.isLoggedIn = isLoggedIn))
);
}
logout() {
this.isLoggedIn = false;
}
}

View File

@ -0,0 +1,43 @@
<div class="row">
<div class="col s12 m4 offset-m4">
<div class="card hoverable">
<div class="card-content center">
<span class="card-title">Page de connexion</span>
<p>
<em>{{ message }}</em>
</p>
</div>
<form #loginForm="ngForm">
<div>
<label for="name">Name</label>
<input
type="text"
id="name"
[(ngModel)]="name"
name="name"
required
/>
</div>
<div>
<label for="password">Password</label>
<input
type="password"
id="password"
[(ngModel)]="password"
name="password"
required
/>
</div>
</form>
<div class="card-action center">
<a
(click)="login()"
class="waves-effect waves-light btn"
*ngIf="!auth.isLoggedIn"
>Se connecter</a
>
<a (click)="logout()" *ngIf="auth.isLoggedIn">Se déconnecter</a>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,44 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
})
export class LoginComponent implements OnInit {
message: string = 'Vous êtes déconnecté. (pikachu/pikachu)';
name: string;
password: string;
auth: AuthService;
constructor(private authService: AuthService, private router: Router) {}
ngOnInit() {
this.auth = this.authService;
}
setMessage() {
if (this.auth.isLoggedIn) {
this.message = 'Vous êtes connecté.';
} else {
this.message = 'Identifiant ou mot de passe incorrect.';
}
}
login() {
this.message = 'Tentative de connexion en cours.';
this.auth.login(this.name, this.password).subscribe((isLoggedIn) => {
this.setMessage();
if (isLoggedIn) {
this.password = '';
this.router.navigate(['/pokemons']);
}
});
}
logout() {
this.auth.logout();
this.message = 'Vous êtes déconnecté.';
}
}

View File

@ -13,12 +13,29 @@ import { EditPokemonComponent } from './edit-pokemon/edit-pokemon.component';
import { AddPokemonComponent } from './add-pokemon/add-pokemon.component'; import { AddPokemonComponent } from './add-pokemon/add-pokemon.component';
import { SearchPokemonComponent } from './search-pokemon/search-pokemon.component'; import { SearchPokemonComponent } from './search-pokemon/search-pokemon.component';
import { LoaderComponent } from './loader/loader.component'; import { LoaderComponent } from './loader/loader.component';
import { AuthGuard } from '../auth.guard';
const pokemonRoutes: Routes = [ const pokemonRoutes: Routes = [
{ path: 'edit/pokemon/:id', component: EditPokemonComponent }, {
{ path: 'pokemon/add', component: AddPokemonComponent }, path: 'edit/pokemon/:id',
{ path: 'pokemon/:id', component: DetailPokemonComponent }, component: EditPokemonComponent,
{ path: 'pokemons', component: ListPokemonComponent }, canActivate: [AuthGuard],
},
{
path: 'pokemon/add',
component: AddPokemonComponent,
canActivate: [AuthGuard],
},
{
path: 'pokemon/:id',
component: DetailPokemonComponent,
canActivate: [AuthGuard],
},
{
path: 'pokemons',
component: ListPokemonComponent,
canActivate: [AuthGuard],
},
]; ];
@NgModule({ @NgModule({