Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MaarchCourrier
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Redmine
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Harbor Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maarch
MaarchCourrier
Commits
1c6d2421
Commit
1c6d2421
authored
4 years ago
by
Alex ORLUC
Browse files
Options
Downloads
Patches
Plain Diff
FEAT #16148 TIME 1:30 fix handle 401 error
parent
bdd0f59b
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/service/auth-interceptor.service.ts
+31
-42
31 additions, 42 deletions
src/frontend/service/auth-interceptor.service.ts
src/frontend/service/auth.service.ts
+19
-0
19 additions, 0 deletions
src/frontend/service/auth.service.ts
with
50 additions
and
42 deletions
src/frontend/service/auth-interceptor.service.ts
+
31
−
42
View file @
1c6d2421
import
{
Injectable
}
from
'
@angular/core
'
;
import
{
Injectable
}
from
'
@angular/core
'
;
Alex ORLUC
@Alex
·
Jan 29, 2021
Author
Owner
c'est un FIX pas un FEAT
c'est un FIX pas un FEAT
Please
register
or
sign in
to reply
import
{
HttpHandler
,
HttpInterceptor
,
HttpRequest
,
HttpClient
,
HttpErrorResponse
}
from
'
@angular/common/http
'
;
import
{
HttpHandler
,
HttpInterceptor
,
HttpRequest
,
HttpClient
,
HttpErrorResponse
}
from
'
@angular/common/http
'
;
import
{
TranslateService
}
from
'
@ngx-translate/core
'
;
import
{
TranslateService
}
from
'
@ngx-translate/core
'
;
import
{
catchError
,
switchMap
}
from
'
rxjs/operators
'
;
import
{
catchError
,
filter
,
switchMap
,
take
}
from
'
rxjs/operators
'
;
import
{
NotificationService
}
from
'
./notification/notification.service
'
;
import
{
NotificationService
}
from
'
./notification/notification.service
'
;
import
{
AuthService
}
from
'
./auth.service
'
;
import
{
AuthService
}
from
'
./auth.service
'
;
import
{
Router
}
from
'
@angular/router
'
;
import
{
Router
}
from
'
@angular/router
'
;
import
{
Observable
,
of
}
from
'
rxjs
'
;
import
{
BehaviorSubject
,
Observable
}
from
'
rxjs
'
;
@
Injectable
()
@
Injectable
()
export
class
AuthInterceptor
implements
HttpInterceptor
{
export
class
AuthInterceptor
implements
HttpInterceptor
{
...
@@ -41,6 +41,11 @@ export class AuthInterceptor implements HttpInterceptor {
...
@@ -41,6 +41,11 @@ export class AuthInterceptor implements HttpInterceptor {
method
:
[
'
PUT
'
]
method
:
[
'
PUT
'
]
}
}
];
];
private
isRefreshing
=
false
;
private
refreshTokenSubject
:
BehaviorSubject
<
any
>
=
new
BehaviorSubject
<
any
>
(
null
);
constructor
(
constructor
(
public
translate
:
TranslateService
,
public
translate
:
TranslateService
,
public
http
:
HttpClient
,
public
http
:
HttpClient
,
...
@@ -60,9 +65,29 @@ export class AuthInterceptor implements HttpInterceptor {
...
@@ -60,9 +65,29 @@ export class AuthInterceptor implements HttpInterceptor {
});
});
}
}
logout
()
{
private
handle401Error
(
request
:
HttpRequest
<
any
>
,
next
:
HttpHandler
)
{
this
.
authService
.
logout
(
false
,
true
);
if
(
!
this
.
isRefreshing
)
{
this
.
notificationService
.
error
(
this
.
translate
.
instant
(
'
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
>
{
...
@@ -84,43 +109,7 @@ export class AuthInterceptor implements HttpInterceptor {
...
@@ -84,43 +109,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
)
{
console
.
debug
(
'
Auth error
'
,
request
.
url
);
return
this
.
handle401Error
(
request
,
next
);
return
this
.
http
.
get
(
'
../rest/authenticate/token
'
,
{
params
:
{
refreshToken
:
this
.
authService
.
getRefreshToken
()
}
}).
pipe
(
switchMap
((
data
:
any
)
=>
{
console
.
debug
(
'
Attempt get token ... !
'
,
request
.
url
);
// 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
of
(
false
);
}
})
);
}
),
catchError
(
err
=>
{
// Disconnect user if bad token process
if
(
err
.
status
===
401
)
{
console
.
debug
(
'
Refresh token failed !
'
,
request
.
url
,
this
.
router
.
url
);
this
.
logout
();
}
return
of
(
false
);
})
);
}
else
if
(
error
.
error
.
errors
===
'
User must change his password
'
)
{
}
else
if
(
error
.
error
.
errors
===
'
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/service/auth.service.ts
+
19
−
0
View file @
1c6d2421
...
@@ -102,6 +102,25 @@ export class AuthService {
...
@@ -102,6 +102,25 @@ export class AuthService {
this
.
localStorage
.
remove
(
'
MaarchCourrierRefreshToken
'
);
this
.
localStorage
.
remove
(
'
MaarchCourrierRefreshToken
'
);
}
}
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
(
false
,
true
);
this
.
notify
.
error
(
this
.
translate
.
instant
(
'
lang.sessionExpired
'
));
return
of
(
false
);
})
);
}
async
logout
(
cleanUrl
:
boolean
=
true
,
forcePageLogin
:
boolean
=
false
)
{
async
logout
(
cleanUrl
:
boolean
=
true
,
forcePageLogin
:
boolean
=
false
)
{
if
([
'
cas
'
,
'
keycloak
'
,
'
azure_saml
'
].
indexOf
(
this
.
authMode
)
>
-
1
&&
!
forcePageLogin
)
{
if
([
'
cas
'
,
'
keycloak
'
,
'
azure_saml
'
].
indexOf
(
this
.
authMode
)
>
-
1
&&
!
forcePageLogin
)
{
this
.
SsoLogout
(
cleanUrl
);
this
.
SsoLogout
(
cleanUrl
);
...
...
This diff is collapsed.
Click to expand it.
Alex ORLUC
@Alex
mentioned in commit
4846ef97
·
4 years ago
mentioned in commit
4846ef97
mentioned in commit 4846ef97ece11b343d4b574e22a0229326e0f8b7
Toggle commit list
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