diff --git a/Back/skydiveLogs-api/Data/JumpsDb-log.db b/Back/skydiveLogs-api/Data/JumpsDb-log.db new file mode 100644 index 0000000..f849812 Binary files /dev/null and b/Back/skydiveLogs-api/Data/JumpsDb-log.db differ diff --git a/Front/skydivelogs-app/src/app/login/login.component.ts b/Front/skydivelogs-app/src/app/login/login.component.ts index 5ac25b3..81d9e8c 100644 --- a/Front/skydivelogs-app/src/app/login/login.component.ts +++ b/Front/skydivelogs-app/src/app/login/login.component.ts @@ -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 => { diff --git a/Front/skydivelogs-app/src/models/user.ts b/Front/skydivelogs-app/src/models/user.ts index 2a232f8..8937564 100644 --- a/Front/skydivelogs-app/src/models/user.ts +++ b/Front/skydivelogs-app/src/models/user.ts @@ -1,8 +1,9 @@ export class User { - id: number; - username: string; - password: string; - firstName: string; - lastName: string; - authdata?: string; -} \ No newline at end of file + id: number; + login: string; + password: string; + firstName: string; + lastName: string; + authdata?: string; + expired?: number; +} diff --git a/Front/skydivelogs-app/src/services/authentication.service.ts b/Front/skydivelogs-app/src/services/authentication.service.ts index 03e59c0..698af7e 100644 --- a/Front/skydivelogs-app/src/services/authentication.service.ts +++ b/Front/skydivelogs-app/src/services/authentication.service.ts @@ -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; @@ -19,16 +18,18 @@ export class AuthenticationService extends BaseService { constructor(private http: HttpClient, private dateService: DateService) { super(); - this.currentUserSubject = new BehaviorSubject(JSON.parse(localStorage.getItem('currentUser'))); + this.currentUserSubject = new BehaviorSubject( + 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(`${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(`${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(`${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); } }