1
1
// Modified from https://jasonwatmore.com/post/2022/11/15/angular-14-jwt-authentication-example-tutorial#login-component-ts
2
2
import { Injectable } from '@angular/core' ;
3
3
import { Router } from '@angular/router' ;
4
- import { HttpClient , HttpResponse } from '@angular/common/http' ;
4
+ import { HttpClient } from '@angular/common/http' ;
5
5
import { BehaviorSubject , Observable } from 'rxjs' ;
6
- import { map , switchMap , tap } from 'rxjs/operators' ;
6
+ import { map , switchMap } from 'rxjs/operators' ;
7
7
import { environment } from '../_environments/environment' ;
8
8
import { User } from '../_models/user.model' ;
9
9
import { UServRes } from '../_models/user.service.response.interface' ;
@@ -28,14 +28,12 @@ export class AuthenticationService {
28
28
login ( username : string , password : string ) {
29
29
console . log ( 'login' , `${ environment . UserServiceApiUrl } /auth/login` ) ;
30
30
return this . http
31
- . post < UServRes > ( `${ environment . UserServiceApiUrl } /auth/login` ,
31
+ . post < UServRes > (
32
+ `${ environment . UserServiceApiUrl } /auth/login` ,
32
33
{ username : username , password : password } ,
33
- { observe : 'response' } )
34
+ { observe : 'response' } ,
35
+ )
34
36
. pipe (
35
- tap ( response => {
36
- console . log ( response . status ) ;
37
- console . dir ( response . body ) ;
38
- } ) ,
39
37
map ( response => {
40
38
// store user details and jwt token in local storage to keep user logged in between page refreshes
41
39
let user : User = { } ;
@@ -60,9 +58,11 @@ export class AuthenticationService {
60
58
61
59
createAccount ( username : string , email : string , password : string ) {
62
60
return this . http
63
- . post < UServRes > ( `${ environment . UserServiceApiUrl } /users` ,
64
- { username : username , email : email , password : password } ,
65
- { observe : 'response' } )
61
+ . post < UServRes > (
62
+ `${ environment . UserServiceApiUrl } /users` ,
63
+ { username : username , email : email , password : password } ,
64
+ { observe : 'response' } ,
65
+ )
66
66
. pipe ( switchMap ( ( ) => this . login ( username , password ) ) ) ; // auto login after registration
67
67
}
68
68
0 commit comments