WIP login
parent
4438d166e4
commit
cc193b6ae0
@ -0,0 +1,5 @@
|
||||
export class User {
|
||||
email: string;
|
||||
requestToken: string;
|
||||
refreshToken: string;
|
||||
}
|
@ -1 +1,38 @@
|
||||
<p>login works!</p>
|
||||
<div id="login-form">
|
||||
<h1>fLotte Login</h1>
|
||||
<mat-form-field>
|
||||
<mat-label>E-Mail-Adresse eingeben</mat-label>
|
||||
<input matInput placeholder="fLotte@beispiel.de" [formControl]="email" />
|
||||
<mat-error *ngIf="email.hasError('required')">
|
||||
Bitte geben Sie eine E-Mail-Adresse ein.
|
||||
</mat-error>
|
||||
<mat-error *ngIf="email.hasError('email')">
|
||||
Bitte geben Sie eine valide E-Mail-Adresse ein.
|
||||
</mat-error>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field>
|
||||
<mat-label>Passwort eingeben</mat-label>
|
||||
<input
|
||||
matInput
|
||||
[type]="hide ? 'password' : 'text'"
|
||||
[formControl]="password"
|
||||
/>
|
||||
<mat-error *ngIf="password.hasError('required')">
|
||||
Bitte geben Sie Ihr Passwort ein.
|
||||
</mat-error>
|
||||
<button
|
||||
mat-icon-button
|
||||
matSuffix
|
||||
(click)="hide = !hide"
|
||||
[attr.aria-label]="'Hide password'"
|
||||
[attr.aria-pressed]="hide"
|
||||
>
|
||||
<mat-icon>{{ hide ? "visibility_off" : "visibility" }}</mat-icon>
|
||||
</button>
|
||||
</mat-form-field>
|
||||
<mat-progress-bar mode="indeterminate" id="loading-bar" *ngIf="loading"></mat-progress-bar>
|
||||
<button mat-stroked-button color="primary" (click)="login()">
|
||||
Login
|
||||
</button>
|
||||
</div>
|
||||
|
@ -0,0 +1,16 @@
|
||||
#login-form {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
max-width: 32em;
|
||||
min-width: 5em;
|
||||
margin: auto;
|
||||
margin-top: 3em;
|
||||
padding: 1em;
|
||||
.mat-form-field {
|
||||
margin: 0.5em 0;
|
||||
}
|
||||
#loading-bar {
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { environment } from '../../environments/environment';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthService {
|
||||
public loggedIn: BehaviorSubject<boolean>;
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
this.loggedIn = new BehaviorSubject<boolean>(false);
|
||||
this.checkIfUserIsLoggedIn();
|
||||
}
|
||||
|
||||
private checkIfUserIsLoggedIn(): void {
|
||||
this.loggedIn.next(!!this.requestToken);
|
||||
}
|
||||
|
||||
public get requestToken(): string {
|
||||
return localStorage.getItem('requestToken');
|
||||
}
|
||||
|
||||
public get refreshToken(): string {
|
||||
return localStorage.getItem('refreshToken');
|
||||
}
|
||||
|
||||
login(email: string, password: string) {
|
||||
return this.http
|
||||
.post<any>(`${environment.authUrl}/login`, { email, password })
|
||||
.pipe(
|
||||
map((response) => {
|
||||
// store request and refresh token in local storage to keep user logged in between page refreshes
|
||||
localStorage.setItem('requestToken', response.request_token);
|
||||
localStorage.setItem('refreshToken', response.refresh_token);
|
||||
this.checkIfUserIsLoggedIn();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
logout() {
|
||||
// remove token from local storage to log user out
|
||||
localStorage.removeItem('requestToken');
|
||||
localStorage.removeItem('refreshToken');
|
||||
this.checkIfUserIsLoggedIn();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue