Skip to content

Commit f23b4fd

Browse files
committed
Temp commit
Trying to figure out how to get status code from HttpClient with HttpResponse.
1 parent dad4d75 commit f23b4fd

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

frontend/src/_services/authentication.service.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Modified from https://jasonwatmore.com/post/2022/11/15/angular-14-jwt-authentication-example-tutorial#login-component-ts
22
import { Injectable } from '@angular/core';
33
import { Router } from '@angular/router';
4-
import { HttpClient } from '@angular/common/http';
4+
import { HttpClient, HttpResponse } from '@angular/common/http';
55
import { BehaviorSubject, Observable } from 'rxjs';
6-
import { map, switchMap } from 'rxjs/operators';
6+
import { map, switchMap, tap } from 'rxjs/operators';
77
import { environment } from '../_environments/environment';
88
import { User } from '../_models/user.model';
99
import { UServRes } from '../_models/user.service.response.interface';
@@ -27,19 +27,29 @@ export class AuthenticationService {
2727

2828
login(username: string, password: string) {
2929
return this.http
30-
.post<UServRes>(`${environment.UserServiceApiUrl}/auth/login`, { username: username, password: password })
30+
.post<UServRes>(`${environment.UserServiceApiUrl}/auth/login`,
31+
{ username: username, password: password },
32+
{ observe: 'response' })
3133
.pipe(
34+
tap(response => {
35+
console.log(response.status);
36+
console.dir(response.body);
37+
}),
3238
map(response => {
3339
// store user details and jwt token in local storage to keep user logged in between page refreshes
34-
const data = response.data;
35-
const user: User = {
36-
id: data.id,
37-
username: data.username,
38-
email: data.email,
39-
accessToken: data.accessToken,
40-
isAdmin: data.isAdmin,
41-
createdAt: data.createdAt,
42-
};
40+
let user: User = {};
41+
if (response.body) {
42+
const body: UServRes = response.body;
43+
const data = body.data;
44+
user = {
45+
id: data.id,
46+
username: data.username,
47+
email: data.email,
48+
accessToken: data.accessToken,
49+
isAdmin: data.isAdmin,
50+
createdAt: data.createdAt,
51+
};
52+
}
4353
localStorage.setItem('user', JSON.stringify(user));
4454
this.userSubject.next(user);
4555
return user;
@@ -49,11 +59,9 @@ export class AuthenticationService {
4959

5060
createAccount(username: string, email: string, password: string) {
5161
return this.http
52-
.post<UServRes>(`${environment.UserServiceApiUrl}/users`, {
53-
username: username,
54-
email: email,
55-
password: password,
56-
})
62+
.post<UServRes>(`${environment.UserServiceApiUrl}/users`,
63+
{ username: username, email: email, password: password},
64+
{ observe: 'response' })
5765
.pipe(switchMap(() => this.login(username, password))); // auto login after registration
5866
}
5967

frontend/src/app/account/login.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,20 @@ export class LoginComponent {
5252
this.router.navigate([returnUrl]);
5353
},
5454
error: error => {
55-
console.error(error);
55+
console.dir(error);
5656
this.isProcessingLogin = false;
5757
let errorMessage = 'An unknown error occurred';
5858
if (error.status === 400) {
5959
errorMessage = 'Missing Fields';
60-
} else if (error.status === 401) {
60+
}
61+
else if (error.status === 401) {
6162
errorMessage = 'Invalid username or password';
62-
} else if (error.status === 500) {
63+
}
64+
else if (error.status === 500) {
6365
errorMessage = 'Database Server Error';
6466
}
6567
this.messageService.add({ severity: 'error', summary: 'Log In Error', detail: errorMessage });
66-
},
68+
}
6769
});
6870
} else {
6971
console.log('Invalid form');

frontend/src/app/account/register.component.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ export class RegisterComponent {
105105
},
106106
// error handling for registration because we assume there will be no errors with auto login
107107
error: error => {
108-
console.error(error);
108+
console.log('A registration error occured');
109+
console.log(error, error.error);
109110
this.isProcessingRegistration = false;
110111
let errorMessage = 'An unknown error occurred';
111-
if (error.status === 400) {
112-
errorMessage = 'Missing Fields';
113-
} else if (error.status === 401) {
114-
errorMessage = 'There is already an account with that username or email';
115-
} else if (error.status === 500) {
116-
errorMessage = 'Database Server Error';
112+
// Check if error has a message property
113+
if (error && error.error && error.error.message) {
114+
errorMessage = error.error.message;
115+
} else if (error && error.message) {
116+
errorMessage = error.message;
117117
}
118118
this.messageService.add({ severity: 'error', summary: 'Log In Error', detail: errorMessage });
119119
},

0 commit comments

Comments
 (0)