@@ -20,14 +20,12 @@ export class FakeBackendInterceptor implements HttpInterceptor {
2020
2121 function handleRoute ( ) {
2222 switch ( true ) {
23- case url . endsWith ( '/users/register' ) && method === 'POST' :
24- return register ( ) ;
2523 case url . endsWith ( '/users/authenticate' ) && method === 'POST' :
2624 return authenticate ( ) ;
25+ case url . endsWith ( '/users/register' ) && method === 'POST' :
26+ return register ( ) ;
2727 case url . endsWith ( '/users' ) && method === 'GET' :
2828 return getUsers ( ) ;
29- case url . match ( / \/ u s e r s \/ \d + $ / ) && method === 'GET' :
30- return getUserById ( ) ;
3129 case url . match ( / \/ u s e r s \/ \d + $ / ) && method === 'DELETE' :
3230 return deleteUser ( ) ;
3331 default :
@@ -38,6 +36,19 @@ export class FakeBackendInterceptor implements HttpInterceptor {
3836
3937 // route functions
4038
39+ function authenticate ( ) {
40+ const { username, password } = body ;
41+ const user = users . find ( x => x . username === username && x . password === password ) ;
42+ if ( ! user ) return error ( 'Username or password is incorrect' ) ;
43+ return ok ( {
44+ id : user . id ,
45+ username : user . username ,
46+ firstName : user . firstName ,
47+ lastName : user . lastName ,
48+ token : 'fake-jwt-token'
49+ } )
50+ }
51+
4152 function register ( ) {
4253 const user = body
4354
@@ -52,31 +63,11 @@ export class FakeBackendInterceptor implements HttpInterceptor {
5263 return ok ( ) ;
5364 }
5465
55- function authenticate ( ) {
56- const { username, password } = body ;
57- const user = users . find ( x => x . username === username && x . password === password ) ;
58- if ( ! user ) return error ( 'Username or password is incorrect' ) ;
59- return ok ( {
60- id : user . id ,
61- username : user . username ,
62- firstName : user . firstName ,
63- lastName : user . lastName ,
64- token : 'fake-jwt-token'
65- } )
66- }
67-
6866 function getUsers ( ) {
6967 if ( ! isLoggedIn ( ) ) return unauthorized ( ) ;
7068 return ok ( users ) ;
7169 }
7270
73- function getUserById ( ) {
74- if ( ! isLoggedIn ( ) ) return unauthorized ( ) ;
75-
76- const user = users . find ( x => x . id == idFromUrl ( ) ) ;
77- return ok ( user ) ;
78- }
79-
8071 function deleteUser ( ) {
8172 if ( ! isLoggedIn ( ) ) return unauthorized ( ) ;
8273
@@ -91,14 +82,14 @@ export class FakeBackendInterceptor implements HttpInterceptor {
9182 return of ( new HttpResponse ( { status : 200 , body } ) )
9283 }
9384
94- function unauthorized ( ) {
95- return throwError ( { status : 401 , error : { message : 'Unauthorised' } } ) ;
96- }
97-
9885 function error ( message ) {
9986 return throwError ( { error : { message } } ) ;
10087 }
10188
89+ function unauthorized ( ) {
90+ return throwError ( { status : 401 , error : { message : 'Unauthorised' } } ) ;
91+ }
92+
10293 function isLoggedIn ( ) {
10394 return headers . get ( 'Authorization' ) === 'Bearer fake-jwt-token' ;
10495 }
@@ -115,4 +106,4 @@ export const fakeBackendProvider = {
115106 provide : HTTP_INTERCEPTORS ,
116107 useClass : FakeBackendInterceptor ,
117108 multi : true
118- } ;
109+ } ;
0 commit comments