From 0327597cd6a4f2b81b3155a1b2e96a2b4f03ecb7 Mon Sep 17 00:00:00 2001 From: Max Ehrlicher-Schmidt Date: Fri, 18 Sep 2020 09:34:02 +0200 Subject: [PATCH] Add login & logout --- src/app/app.component.html | 4 +++ src/app/app.component.ts | 8 +++++- src/app/pages/login/login.component.html | 3 +++ src/app/pages/login/login.component.scss | 3 +++ src/app/pages/login/login.component.ts | 33 ++++++++++++++++++------ src/app/services/auth.service.ts | 12 ++++++--- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index 7490ec0..1d6ad0a 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -20,6 +20,10 @@ >Dark Mode + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index ab69dbd..1f6af50 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,6 +1,7 @@ import { Component, Renderer2 } from '@angular/core'; import { ColorThemeService } from './services/colorTheme.service'; import { AuthService } from './services/auth.service'; +import { Router } from '@angular/router'; @Component({ selector: 'app-root', @@ -15,7 +16,8 @@ export class AppComponent { constructor( private renderer: Renderer2, 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.themeService.load(); @@ -26,4 +28,8 @@ export class AppComponent { changeTheme(event) { this.themeService.update(event.checked ? 'dark-theme' : 'light-theme'); } + + logout() { + this.authService.logout().subscribe().add(() => this.router.navigate(['login'])); + } } diff --git a/src/app/pages/login/login.component.html b/src/app/pages/login/login.component.html index 290b9a0..e05ab4a 100644 --- a/src/app/pages/login/login.component.html +++ b/src/app/pages/login/login.component.html @@ -35,4 +35,7 @@ + + {{errorMessage}} + diff --git a/src/app/pages/login/login.component.scss b/src/app/pages/login/login.component.scss index 542b480..e228d12 100644 --- a/src/app/pages/login/login.component.scss +++ b/src/app/pages/login/login.component.scss @@ -13,4 +13,7 @@ #loading-bar { margin-bottom: 1em; } + .login-error-message { + margin-top: 0.5em; + } } \ No newline at end of file diff --git a/src/app/pages/login/login.component.ts b/src/app/pages/login/login.component.ts index 653e5bb..68903e2 100644 --- a/src/app/pages/login/login.component.ts +++ b/src/app/pages/login/login.component.ts @@ -1,27 +1,44 @@ import { Component, OnInit } from '@angular/core'; -import {FormControl, Validators} from '@angular/forms'; -import {AuthService} from '../../services/auth.service'; +import { FormControl, Validators } from '@angular/forms'; +import { Router } from '@angular/router'; +import { AuthService } from '../../services/auth.service'; @Component({ selector: 'app-login', templateUrl: './login.component.html', - styleUrls: ['./login.component.scss'] + styleUrls: ['./login.component.scss'], }) export class LoginComponent implements OnInit { email = new FormControl('', [Validators.required, Validators.email]); password = new FormControl('', [Validators.required]); hide = true; loading = false; - constructor(private authService: AuthService) { } + errorOccurred = false; + errorMessage = ''; + constructor(private authService: AuthService, private router: Router) {} - ngOnInit(): void { - } + ngOnInit(): void {} login() { + this.errorMessage = ''; + this.errorOccurred = false; if (this.email.invalid || this.password.invalid) { 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; + }); } - } diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 85bf050..d0c029f 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { map } from 'rxjs/operators'; +import { map, tap } from 'rxjs/operators'; import { environment } from '../../environments/environment'; import { BehaviorSubject } from 'rxjs'; @@ -42,8 +42,12 @@ export class AuthService { logout() { // remove token from local storage to log user out - localStorage.removeItem('requestToken'); - localStorage.removeItem('refreshToken'); - this.checkIfUserIsLoggedIn(); + return this.http + .post(`${environment.authUrl}/logout`, { request_token: this.requestToken }).pipe(tap(() => { + localStorage.removeItem('requestToken'); + localStorage.removeItem('refreshToken'); + this.checkIfUserIsLoggedIn(); + } + )); } }