Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
HIAST-Clinics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
HIAST-Clinics
Commits
0669d39a
Commit
0669d39a
authored
Aug 22, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(F) Add authentication interceptor
parent
ba98801b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
9 deletions
+37
-9
app.module.ts
Clinics.Frontend/src/app/app.module.ts
+4
-1
authentication.service.ts
...src/app/services/authentication/authentication.service.ts
+1
-8
authentication.interceptor.ts
.../authentication/interceptor/authentication.interceptor.ts
+32
-0
No files found.
Clinics.Frontend/src/app/app.module.ts
View file @
0669d39a
...
@@ -13,8 +13,10 @@ import { FooterComponent } from './components/template/footer/footer.component';
...
@@ -13,8 +13,10 @@ import { FooterComponent } from './components/template/footer/footer.component';
import
{
HomeComponent
}
from
'./components/home/home.component'
;
import
{
HomeComponent
}
from
'./components/home/home.component'
;
import
{
LoginFormComponent
}
from
'./components/Authentication/login-form/login-form.component'
;
import
{
LoginFormComponent
}
from
'./components/Authentication/login-form/login-form.component'
;
import
{
FormsModule
}
from
'@angular/forms'
;
import
{
FormsModule
}
from
'@angular/forms'
;
import
{
HttpClientModule
}
from
'@angular/common/http'
;
import
{
H
TTP_INTERCEPTORS
,
H
ttpClientModule
}
from
'@angular/common/http'
;
import
{
AuthenticationService
}
from
'./services/authentication/authentication.service'
;
import
{
AuthenticationService
}
from
'./services/authentication/authentication.service'
;
import
{
AuthenticationInterceptor
}
from
'./services/authentication/interceptor/authentication.interceptor'
;
@
NgModule
({
@
NgModule
({
imports
:
[
imports
:
[
...
@@ -32,6 +34,7 @@ import { AuthenticationService } from './services/authentication/authentication.
...
@@ -32,6 +34,7 @@ import { AuthenticationService } from './services/authentication/authentication.
// all parts of the app
// all parts of the app
providers
:
[
providers
:
[
AuthenticationService
,
AuthenticationService
,
{
provide
:
HTTP_INTERCEPTORS
,
useClass
:
AuthenticationInterceptor
,
multi
:
true
},
],
],
// components and directives that belong to this module
// components and directives that belong to this module
...
...
Clinics.Frontend/src/app/services/authentication/authentication.service.ts
View file @
0669d39a
...
@@ -23,19 +23,12 @@ export class AuthenticationService {
...
@@ -23,19 +23,12 @@ export class AuthenticationService {
private
readonly
USERS_ENDPOINT
:
string
=
`
${
config
.
apiUrl
}
/Users`
private
readonly
USERS_ENDPOINT
:
string
=
`
${
config
.
apiUrl
}
/Users`
//#region HTTP headers
private
readonly
HTTP_HEADERS
:
HttpHeaders
=
this
.
getHeaders
();
getHeaders
():
HttpHeaders
{
return
new
HttpHeaders
().
set
(
'Content-Type'
,
'application/json'
);
}
//#endregion
//#endregion
//#endregion
//#region Login
//#region Login
private
postLogin
(
loginCommand
:
LoginCommand
):
Observable
<
LoginResponse
>
{
private
postLogin
(
loginCommand
:
LoginCommand
):
Observable
<
LoginResponse
>
{
return
this
.
http
.
post
<
LoginResponse
>
(
return
this
.
http
.
post
<
LoginResponse
>
(
this
.
USERS_ENDPOINT
,
loginCommand
,
{
headers
:
this
.
HTTP_HEADERS
}
);
this
.
USERS_ENDPOINT
,
loginCommand
);
}
}
login
(
loginCommand
:
LoginCommand
):
Observable
<
LoginResult
>
{
login
(
loginCommand
:
LoginCommand
):
Observable
<
LoginResult
>
{
...
...
Clinics.Frontend/src/app/services/authentication/interceptor/authentication.interceptor.ts
0 → 100644
View file @
0669d39a
import
{
Injectable
}
from
'@angular/core'
;
import
{
HttpInterceptor
,
HttpRequest
,
HttpHandler
,
HttpEvent
,
HttpHeaders
}
from
'@angular/common/http'
;
import
{
JWTHandler
}
from
'../jwtHandler'
;
import
{
Observable
}
from
'rxjs'
;
@
Injectable
()
export
class
AuthenticationInterceptor
implements
HttpInterceptor
{
//#region HTTP headers
private
readonly
HTTP_HEADERS
:
HttpHeaders
=
this
.
getHeaders
();
getHeaders
():
HttpHeaders
{
return
new
HttpHeaders
().
set
(
'Content-Type'
,
'application/json'
);
}
//#endregion
intercept
(
req
:
HttpRequest
<
any
>
,
next
:
HttpHandler
):
Observable
<
HttpEvent
<
any
>>
{
const
jwt
=
JWTHandler
.
getJwtFromCookie
();
console
.
log
(
req
);
req
=
req
.
clone
({
headers
:
this
.
HTTP_HEADERS
});
if
(
jwt
!==
null
)
{
req
=
req
.
clone
({
setHeaders
:
{
Authorization
:
`Bearer
${
jwt
}
`
}
});
}
console
.
log
(
req
);
return
next
.
handle
(
req
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment