Skip to content

Commit

Permalink
Use API_CONFIG instead of environment.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelim01 committed Oct 17, 2024
1 parent f096a0e commit 5936fdd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
4 changes: 0 additions & 4 deletions frontend/src/_environments/environment.ts

This file was deleted.

10 changes: 5 additions & 5 deletions frontend/src/_interceptors/jwt.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HttpTestingController, provideHttpClientTesting } from '@angular/common
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
import { JwtInterceptor } from './jwt.interceptor';
import { AuthenticationService } from '../_services/authentication.service';
import { environment } from '../_environments/environment';
import { API_CONFIG } from '../app/api.config';

describe('JwtInterceptor', () => {
let httpMock: HttpTestingController;
Expand Down Expand Up @@ -34,9 +34,9 @@ describe('JwtInterceptor', () => {
});

it('should add an Authorization header', () => {
httpClient.get(`${environment.UserServiceApiUrl}/test`).subscribe();
httpClient.get(`${API_CONFIG.baseUrl}/test`).subscribe();

const httpRequest = httpMock.expectOne(`${environment.UserServiceApiUrl}/test`);
const httpRequest = httpMock.expectOne(`${API_CONFIG.baseUrl}/test`);

expect(httpRequest.request.headers.has('Authorization')).toBeTruthy();
expect(httpRequest.request.headers.get('Authorization')).toBe('Bearer fake-jwt-token');
Expand All @@ -47,9 +47,9 @@ describe('JwtInterceptor', () => {
userValue: {},
});

httpClient.get(`${environment.UserServiceApiUrl}/test`).subscribe();
httpClient.get(`${API_CONFIG.baseUrl}/test`).subscribe();

const httpRequest = httpMock.expectOne(`${environment.UserServiceApiUrl}/test`);
const httpRequest = httpMock.expectOne(`${API_CONFIG.baseUrl}/test`);

expect(httpRequest.request.headers.has('Authorization')).toBeFalsy();
});
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/_interceptors/jwt.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../_environments/environment';
import { AuthenticationService } from '../_services/authentication.service';
import { User } from '../_models/user.model';
import { API_CONFIG } from '../app/api.config';

@Injectable()
export class JwtInterceptor implements HttpInterceptor {
Expand All @@ -13,7 +13,7 @@ export class JwtInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
// add auth header with jwt if user is logged in and request is to the api url
const isLoggedIn = this.authenticationService.userValue?.accessToken;
const isApiUrl = request.url.startsWith(environment.UserServiceApiUrl);
const isApiUrl = request.url.startsWith(API_CONFIG.baseUrl);
if (isLoggedIn && isApiUrl) {
request = request.clone({
headers: request.headers.set('Authorization', `Bearer ${this.currentUser!.accessToken}`),
Expand Down
20 changes: 9 additions & 11 deletions frontend/src/_services/auth.guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { CanActivate, Router } from '@angular/router';
import { AuthenticationService } from './authentication.service';
import { filter, map, Observable, of, switchMap } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { environment } from '../_environments/environment';
import { UServRes } from '../_models/user.service.model';
import { API_CONFIG } from '../app/api.config';

@Injectable()
export class AuthGuardService implements CanActivate {
Expand All @@ -25,16 +25,14 @@ export class AuthGuardService implements CanActivate {
return of(false); // of() to return an observable to be flattened
}
// call to user service endpoint '/users/{user_id}' to check user is still valid
return this.http
.get<UServRes>(`${environment.UserServiceApiUrl}/users/${user.id}`, { observe: 'response' })
.pipe(
map(response => {
if (response.status === 200) {
return true;
}
return false;
}),
);
return this.http.get<UServRes>(`${API_CONFIG.baseUrl}/users/${user.id}`, { observe: 'response' }).pipe(
map(response => {
if (response.status === 200) {
return true;
}
return false;
}),
);
}),
);
}
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/_services/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { environment } from '../_environments/environment';
import { UServRes } from '../_models/user.service.model';
import { User } from '../_models/user.model';
import { API_CONFIG } from '../app/api.config';

@Injectable({ providedIn: 'root' })
export class AuthenticationService {
Expand All @@ -27,10 +27,10 @@ export class AuthenticationService {
}

login(username: string, password: string) {
console.log('login', `${environment.UserServiceApiUrl}/auth/login`);
console.log('login', `${API_CONFIG.baseUrl}/auth/login`);
return this.http
.post<UServRes>(
`${environment.UserServiceApiUrl}/auth/login`,
`${API_CONFIG.baseUrl}/auth/login`,
{ username: username, password: password },
{ observe: 'response' },
)
Expand All @@ -52,7 +52,7 @@ export class AuthenticationService {
createAccount(username: string, email: string, password: string) {
return this.http
.post<UServRes>(
`${environment.UserServiceApiUrl}/users`,
`${API_CONFIG.baseUrl}/users`,
{ username: username, email: email, password: password },
{ observe: 'response' },
)
Expand Down

0 comments on commit 5936fdd

Please sign in to comment.