Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 2c33d03

Browse files
authored
Merge pull request #26 from VOMATEC-Innovations/feature/end-session-on-logout
Added possibility to end user session on "Single Application Logout".
2 parents 26d3e59 + dffa811 commit 2c33d03

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export default App
3939

4040
### Custom Provider/Endpoint
4141

42-
After https://github.com/gardner/react-oauth2-pkce/pull/16 it is possible to pass in just `provider` or `authorizeEndpoint` and `tokenEndpoint`. These two parameters were added to maintain backwards compatibility while enabling callers to customize the endpoint.
42+
After https://github.com/gardner/react-oauth2-pkce/pull/16 it is possible to pass in just `provider` or `authorizeEndpoint`, `tokenEndpoint` and `logoutEndpoint`. These two parameters were added to maintain backwards compatibility while enabling callers to customize the endpoint.
43+
44+
### End User Session on "Single Application Logout"
45+
You can end user session when calling `logout(true)`. A custom endpoint can configured by passing `logoutEndpoint` as props. The user will be redirected to the `redirectUri`.
4346

4447
## License
4548

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-oauth2-pkce",
3-
"version": "2.0.6",
3+
"version": "2.0.7",
44
"description": "Authenticate against generic OAuth2 using PKCE",
55
"author": "Gardner Bickford <[email protected]>",
66
"license": "MIT",

src/AuthService.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface AuthServiceProps {
1212
provider: string
1313
authorizeEndpoint?: string
1414
tokenEndpoint?: string
15+
logoutEndpoint?: string
1516
audience?: string
1617
redirectUri?: string
1718
scopes: string[]
@@ -146,11 +147,22 @@ export class AuthService<TIDToken = JWTIDToken> {
146147
return window.localStorage.getItem('auth') !== null
147148
}
148149

149-
async logout(): Promise<boolean> {
150+
async logout(shouldEndSession: boolean = false): Promise<boolean> {
150151
this.removeItem('pkce')
151152
this.removeItem('auth')
152-
window.location.reload()
153-
return true
153+
if (shouldEndSession) {
154+
const { clientId, provider, logoutEndpoint, redirectUri } = this.props;
155+
const query = {
156+
client_id: clientId,
157+
post_logout_redirect_uri: redirectUri
158+
}
159+
const url = `${logoutEndpoint || `${provider}/logout`}?${toUrlEncoded(query)}`
160+
window.location.replace(url)
161+
return true;
162+
} else {
163+
window.location.reload()
164+
return true
165+
}
154166
}
155167

156168
async login(): Promise<void> {

0 commit comments

Comments
 (0)