Add login & logout

pull/1/head
Max Ehrlicher-Schmidt 4 years ago
parent cc193b6ae0
commit 0327597cd6

@ -20,6 +20,10 @@
>Dark Mode</mat-slide-toggle >Dark Mode</mat-slide-toggle
> >
</div> </div>
<button *ngIf="loggedIn" mat-menu-item (click)="logout()">
<mat-icon>exit_to_app</mat-icon>
<span>Ausloggen</span>
</button>
</mat-menu> </mat-menu>
</mat-toolbar> </mat-toolbar>

@ -1,6 +1,7 @@
import { Component, Renderer2 } from '@angular/core'; import { Component, Renderer2 } from '@angular/core';
import { ColorThemeService } from './services/colorTheme.service'; import { ColorThemeService } from './services/colorTheme.service';
import { AuthService } from './services/auth.service'; import { AuthService } from './services/auth.service';
import { Router } from '@angular/router';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@ -15,7 +16,8 @@ export class AppComponent {
constructor( constructor(
private renderer: Renderer2, private renderer: Renderer2,
private themeService: ColorThemeService, private themeService: ColorThemeService,
private authService: AuthService private authService: AuthService,
private router: Router,
) { ) {
this.renderer.addClass(document.body, 'mat-app-background'); //so the background color changes dependent on current theme this.renderer.addClass(document.body, 'mat-app-background'); //so the background color changes dependent on current theme
this.themeService.load(); this.themeService.load();
@ -26,4 +28,8 @@ export class AppComponent {
changeTheme(event) { changeTheme(event) {
this.themeService.update(event.checked ? 'dark-theme' : 'light-theme'); this.themeService.update(event.checked ? 'dark-theme' : 'light-theme');
} }
logout() {
this.authService.logout().subscribe().add(() => this.router.navigate(['login']));
}
} }

@ -35,4 +35,7 @@
<button mat-stroked-button color="primary" (click)="login()"> <button mat-stroked-button color="primary" (click)="login()">
Login Login
</button> </button>
<mat-error class="login-error-message" *ngIf="errorOccurred">
{{errorMessage}}
</mat-error>
</div> </div>

@ -13,4 +13,7 @@
#loading-bar { #loading-bar {
margin-bottom: 1em; margin-bottom: 1em;
} }
.login-error-message {
margin-top: 0.5em;
}
} }

@ -1,27 +1,44 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import {FormControl, Validators} from '@angular/forms'; import { FormControl, Validators } from '@angular/forms';
import {AuthService} from '../../services/auth.service'; import { Router } from '@angular/router';
import { AuthService } from '../../services/auth.service';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
templateUrl: './login.component.html', templateUrl: './login.component.html',
styleUrls: ['./login.component.scss'] styleUrls: ['./login.component.scss'],
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
email = new FormControl('', [Validators.required, Validators.email]); email = new FormControl('', [Validators.required, Validators.email]);
password = new FormControl('', [Validators.required]); password = new FormControl('', [Validators.required]);
hide = true; hide = true;
loading = false; loading = false;
constructor(private authService: AuthService) { } errorOccurred = false;
errorMessage = '';
constructor(private authService: AuthService, private router: Router) {}
ngOnInit(): void { ngOnInit(): void {}
}
login() { login() {
this.errorMessage = '';
this.errorOccurred = false;
if (this.email.invalid || this.password.invalid) { if (this.email.invalid || this.password.invalid) {
return; return;
} }
this.authService.login(this.email.value, this.password.value).subscribe(); this.loading = true;
this.authService
.login(this.email.value, this.password.value)
.subscribe(
() => this.router.navigate(['bikes']),
(error) => {
this.errorOccurred = true;
this.errorMessage =
error.error.message ||
'Ein Fehler bei Einloggen ist aufgetreten. Überprüfen Sie Ihre Internetverbindung oder versuchen Sie es später erneut.';
}
)
.add(() => {
this.loading = false;
});
} }
} }

@ -1,6 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators'; import { map, tap } from 'rxjs/operators';
import { environment } from '../../environments/environment'; import { environment } from '../../environments/environment';
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject } from 'rxjs';
@ -42,8 +42,12 @@ export class AuthService {
logout() { logout() {
// remove token from local storage to log user out // remove token from local storage to log user out
return this.http
.post<any>(`${environment.authUrl}/logout`, { request_token: this.requestToken }).pipe(tap(() => {
localStorage.removeItem('requestToken'); localStorage.removeItem('requestToken');
localStorage.removeItem('refreshToken'); localStorage.removeItem('refreshToken');
this.checkIfUserIsLoggedIn(); this.checkIfUserIsLoggedIn();
} }
));
}
} }

Loading…
Cancel
Save