This commit is contained in:
Sébastien André
2020-03-14 23:10:21 +01:00
parent 0e3fdd8f54
commit 8427691db4
4 changed files with 55 additions and 33 deletions

View File

@@ -5,6 +5,7 @@ import { FormBuilder, FormGroup, Validators } from "@angular/forms";
import { first } from "rxjs/operators";
import { AuthenticationService } from "../../services/authentication.service";
import { User } from "../../models/user";
@Component({
selector: "app-login",
@@ -50,7 +51,7 @@ export class LoginComponent implements OnInit {
}
get createCtrls() {
return this.loginForm.controls;
return this.createForm.controls;
}
onLoginSubmit() {
@@ -85,8 +86,12 @@ export class LoginComponent implements OnInit {
}
this.loading = true;
let createUser = new User();
createUser.login = this.createCtrls.username.value;
createUser.password = this.createCtrls.password.value;
this.authenticationService
.login(this.createCtrls.username.value, this.createCtrls.password.value)
.create(createUser)
.pipe(first())
.subscribe(
data => {

View File

@@ -1,8 +1,9 @@
export class User {
id: number;
username: string;
password: string;
firstName: string;
lastName: string;
authdata?: string;
}
id: number;
login: string;
password: string;
firstName: string;
lastName: string;
authdata?: string;
expired?: number;
}

View File

@@ -1,16 +1,15 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { User } from '../models/user';
import { BaseService } from './base.service';
import { DateService } from './date.service';
import { BehaviorSubject, Observable } from "rxjs";
import { map } from "rxjs/operators";
import { User } from "../models/user";
import { BaseService } from "./base.service";
import { DateService } from "./date.service";
@Injectable({
providedIn: 'root'
providedIn: "root"
})
export class AuthenticationService extends BaseService {
private currentUserSubject: BehaviorSubject<User>;
@@ -19,16 +18,18 @@ export class AuthenticationService extends BaseService {
constructor(private http: HttpClient, private dateService: DateService) {
super();
this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('currentUser')));
this.currentUserSubject = new BehaviorSubject<User>(
JSON.parse(localStorage.getItem("currentUser"))
);
this.currentUser = this.currentUserSubject.asObservable();
}
public get currentUserValue(): User {
const tmp = localStorage.getItem('currentUser');
const tmp = localStorage.getItem("currentUser");
if (tmp) {
const storedUser = JSON.parse(tmp);
if (new Date().getTime() > storedUser.expired) {
localStorage.removeItem('currentUser');
localStorage.removeItem("currentUser");
}
}
@@ -41,23 +42,38 @@ export class AuthenticationService extends BaseService {
password: password
};
return this.http.post<any>(`${this.apiUrl}/User/Authenticate`, bodyLogin, {
headers: this.headers
})
.pipe(map(user => {
// store user details and basic auth credentials in local storage to keep user logged in between page refreshes
user.authdata = window.btoa(username + ':' + password);
user.expired = this.dateService.AddDays(new Date(), 1).getTime();
return this.http
.post<any>(`${this.apiUrl}/User/Authenticate`, bodyLogin, {
headers: this.headers
})
.pipe(
map(user => {
// store user details and basic auth credentials in local storage to keep user logged in between page refreshes
user.authdata = window.btoa(username + ":" + password);
user.expired = this.dateService.AddDays(new Date(), 1).getTime();
localStorage.setItem('currentUser', JSON.stringify(user));
this.currentUserSubject.next(user);
return user;
}));
localStorage.setItem("currentUser", JSON.stringify(user));
this.currentUserSubject.next(user);
return user;
})
);
}
create(newUser: User) {
return this.http
.post<any>(`${this.apiUrl}/User`, newUser, {
headers: this.headers
})
.pipe(
map(result => {
this.login(newUser.login, newUser.password);
})
);
}
logout() {
// remove user from local storage to log user out
localStorage.removeItem('currentUser');
localStorage.removeItem("currentUser");
this.currentUserSubject.next(null);
}
}