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

Binary file not shown.

View File

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

View File

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

View File

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