Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MaarchParapheur
Manage
Activity
Members
Plan
Redmine
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maarch
MaarchParapheur
Commits
92404352
Commit
92404352
authored
4 years ago
by
Alex ORLUC
Browse files
Options
Downloads
Patches
Plain Diff
FIX #16148 TIME 0:20 improve handle 401
parent
16da4a1a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/frontend/app/service/auth-interceptor.service.ts
+38
-43
38 additions, 43 deletions
src/frontend/app/service/auth-interceptor.service.ts
src/frontend/app/service/auth.service.ts
+19
-0
19 additions, 0 deletions
src/frontend/app/service/auth.service.ts
with
57 additions
and
43 deletions
src/frontend/app/service/auth-interceptor.service.ts
+
38
−
43
View file @
92404352
import
{
Injectable
}
from
'
@angular/core
'
;
import
{
Injectable
}
from
'
@angular/core
'
;
import
{
HttpHandler
,
HttpInterceptor
,
HttpRequest
,
HttpClient
,
HttpErrorResponse
}
from
'
@angular/common/http
'
;
import
{
HttpHandler
,
HttpInterceptor
,
HttpRequest
,
HttpClient
,
HttpErrorResponse
}
from
'
@angular/common/http
'
;
import
{
Observable
}
from
'
rxjs
'
;
import
{
BehaviorSubject
,
Observable
}
from
'
rxjs
'
;
import
{
Router
}
from
'
@angular/router
'
;
import
{
Router
}
from
'
@angular/router
'
;
import
{
catchError
,
switchMap
}
from
'
rxjs/operators
'
;
import
{
catchError
,
filter
,
switchMap
,
take
}
from
'
rxjs/operators
'
;
import
{
NotificationService
}
from
'
./notification.service
'
;
import
{
NotificationService
}
from
'
./notification.service
'
;
import
{
EMPTY
}
from
'
rxjs
'
;
import
{
SignaturesContentService
}
from
'
./signatures.service
'
;
import
{
SignaturesContentService
}
from
'
./signatures.service
'
;
import
{
AuthService
}
from
'
./auth.service
'
;
import
{
AuthService
}
from
'
./auth.service
'
;
...
@@ -24,7 +23,18 @@ export class AuthInterceptor implements HttpInterceptor {
...
@@ -24,7 +23,18 @@ export class AuthInterceptor implements HttpInterceptor {
}
}
];
];
constructor
(
public
http
:
HttpClient
,
private
router
:
Router
,
public
notificationService
:
NotificationService
,
public
signaturesService
:
SignaturesContentService
,
public
authService
:
AuthService
)
{
}
private
isRefreshing
=
false
;
private
refreshTokenSubject
:
BehaviorSubject
<
any
>
=
new
BehaviorSubject
<
any
>
(
null
);
constructor
(
public
http
:
HttpClient
,
private
router
:
Router
,
public
notificationService
:
NotificationService
,
public
signaturesService
:
SignaturesContentService
,
public
authService
:
AuthService
)
{
}
addAuthHeader
(
request
:
HttpRequest
<
any
>
)
{
addAuthHeader
(
request
:
HttpRequest
<
any
>
)
{
...
@@ -37,9 +47,29 @@ export class AuthInterceptor implements HttpInterceptor {
...
@@ -37,9 +47,29 @@ export class AuthInterceptor implements HttpInterceptor {
});
});
}
}
logout
()
{
private
handle401Error
(
request
:
HttpRequest
<
any
>
,
next
:
HttpHandler
)
{
this
.
authService
.
logout
();
if
(
!
this
.
isRefreshing
)
{
this
.
notificationService
.
error
(
'
lang.sessionExpired
'
);
this
.
isRefreshing
=
true
;
this
.
refreshTokenSubject
.
next
(
null
);
return
this
.
authService
.
refreshToken
().
pipe
(
switchMap
((
data
:
any
)
=>
{
this
.
isRefreshing
=
false
;
this
.
refreshTokenSubject
.
next
(
data
.
token
);
request
=
this
.
addAuthHeader
(
request
);
return
next
.
handle
(
request
);
})
);
}
else
{
return
this
.
refreshTokenSubject
.
pipe
(
filter
((
token
)
=>
token
!=
null
),
take
(
1
),
switchMap
(()
=>
{
request
=
this
.
addAuthHeader
(
request
);
return
next
.
handle
(
request
);
})
);
}
}
}
intercept
(
request
:
HttpRequest
<
any
>
,
next
:
HttpHandler
):
Observable
<
any
>
{
intercept
(
request
:
HttpRequest
<
any
>
,
next
:
HttpHandler
):
Observable
<
any
>
{
...
@@ -62,42 +92,7 @@ export class AuthInterceptor implements HttpInterceptor {
...
@@ -62,42 +92,7 @@ export class AuthInterceptor implements HttpInterceptor {
if
(
this
.
byPassHandleErrors
.
filter
(
url
=>
request
.
url
.
indexOf
(
url
.
route
)
>
-
1
&&
url
.
method
.
indexOf
(
request
.
method
)
>
-
1
).
length
>
0
)
{
if
(
this
.
byPassHandleErrors
.
filter
(
url
=>
request
.
url
.
indexOf
(
url
.
route
)
>
-
1
&&
url
.
method
.
indexOf
(
request
.
method
)
>
-
1
).
length
>
0
)
{
return
next
.
handle
(
request
);
return
next
.
handle
(
request
);
}
else
if
(
error
.
status
===
401
)
{
}
else
if
(
error
.
status
===
401
)
{
return
this
.
http
.
get
(
'
../rest/authenticate/token
'
,
{
return
this
.
handle401Error
(
request
,
next
);
params
:
{
refreshToken
:
this
.
authService
.
getRefreshToken
()
}
}).
pipe
(
switchMap
((
data
:
any
)
=>
{
// Update stored token
this
.
authService
.
setToken
(
data
.
token
);
// Update user info
this
.
authService
.
updateUserInfo
(
data
.
token
);
// Clone our request with token updated ant try to resend it
request
=
this
.
addAuthHeader
(
request
);
return
next
.
handle
(
request
).
pipe
(
catchError
(
err
=>
{
// Disconnect user if bad token process
if
(
err
.
status
===
401
)
{
this
.
logout
();
return
EMPTY
;
}
})
);
}
),
catchError
(
err
=>
{
// Disconnect user if bad token process
if
(
err
.
status
===
401
)
{
this
.
logout
();
}
return
EMPTY
;
})
);
}
else
if
(
error
.
error
.
errors
===
'
Password expired : User must change his password
'
)
{
}
else
if
(
error
.
error
.
errors
===
'
Password expired : User must change his password
'
)
{
return
this
.
router
.
navigate
([
'
/password-modification
'
]);
return
this
.
router
.
navigate
([
'
/password-modification
'
]);
}
else
{
}
else
{
...
...
This diff is collapsed.
Click to expand it.
src/frontend/app/service/auth.service.ts
+
19
−
0
View file @
92404352
...
@@ -47,6 +47,25 @@ export class AuthService {
...
@@ -47,6 +47,25 @@ export class AuthService {
this
.
localStorage
.
remove
(
'
MaarchParapheurRefreshToken
'
);
this
.
localStorage
.
remove
(
'
MaarchParapheurRefreshToken
'
);
}
}
refreshToken
()
{
return
this
.
http
.
get
<
any
>
(
`../rest/authenticate/token`
,
{
params
:
{
refreshToken
:
this
.
getRefreshToken
()
}
})
.
pipe
(
tap
((
data
)
=>
{
// Update stored token
this
.
setToken
(
data
.
token
);
// Update user info
this
.
updateUserInfo
(
data
.
token
);
}),
catchError
((
error
)
=>
{
this
.
logout
();
this
.
notificationService
.
error
(
'
lang.sessionExpired
'
);
return
of
(
false
);
})
);
}
logout
()
{
logout
()
{
const
refreshToken
=
this
.
getRefreshToken
();
const
refreshToken
=
this
.
getRefreshToken
();
if
(
refreshToken
===
null
)
{
if
(
refreshToken
===
null
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment