Fin section 17 : authentification
parent
f4a6d2dfeb
commit
b5d062ebca
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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>
|
@ -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é.';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue