added local storage var
This commit is contained in:
parent
56eaec8920
commit
9e2720ae88
@ -1,6 +1,8 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { FormGroup, FormControl, Validators } from '@angular/forms';
|
||||
import { UserService } from '../../services/user.service';
|
||||
import { Auth } from '../../interfaces/auth.interface';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
@ -11,7 +13,8 @@ export class LoginComponent implements OnInit {
|
||||
|
||||
loginForm: FormGroup;
|
||||
|
||||
constructor(private userSerice: UserService) { }
|
||||
constructor(private userSerice: UserService,
|
||||
private router: Router) { }
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loginForm = this.createLoginForm();
|
||||
@ -28,8 +31,9 @@ export class LoginComponent implements OnInit {
|
||||
}
|
||||
|
||||
onRegisterUser(): void {
|
||||
this.userSerice.login(this.loginForm.value).subscribe(() => {
|
||||
|
||||
this.userSerice.login(this.loginForm.value).subscribe((auth: Auth) => {
|
||||
localStorage.setItem('token', auth.token);
|
||||
this.router.navigateByUrl('/subjects');
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { UserService } from './../../services/user.service';
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { FormGroup, FormControl, Validators } from '@angular/forms';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-register',
|
||||
@ -11,7 +12,8 @@ export class RegisterComponent implements OnInit {
|
||||
|
||||
registerForm: FormGroup;
|
||||
|
||||
constructor(private userSerice: UserService) { }
|
||||
constructor(private userSerice: UserService,
|
||||
private router: Router) { }
|
||||
|
||||
ngOnInit(): void {
|
||||
this.registerForm = this.createRegisterForm();
|
||||
@ -29,7 +31,7 @@ export class RegisterComponent implements OnInit {
|
||||
|
||||
onRegisterUser(): void {
|
||||
this.userSerice.register(this.registerForm.value).subscribe(() => {
|
||||
|
||||
this.router.navigateByUrl('/subjects');
|
||||
});
|
||||
}
|
||||
|
||||
|
3
src/app/user/interfaces/auth.interface.ts
Normal file
3
src/app/user/interfaces/auth.interface.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export interface Auth {
|
||||
token: string;
|
||||
}
|
@ -3,6 +3,7 @@ import { User } from './../interfaces/user.interface';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Auth } from '../interfaces/auth.interface';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@ -15,8 +16,8 @@ export class UserService {
|
||||
return this.http.post<void>(environment.host + environment.apiEndpoints.users.register, user);
|
||||
}
|
||||
|
||||
login(user: User): Observable<void> {
|
||||
return this.http.post<void>(environment.host + environment.apiEndpoints.users.login, user);
|
||||
login(user: User): Observable<Auth> {
|
||||
return this.http.post<Auth>(environment.host + environment.apiEndpoints.users.login, user);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user