Skip to content

Commit c836231

Browse files
authored
Merge pull request #5 from ArteGEIE/fix-login-loop
fix loop in redirect
2 parents f2bfe85 + 059e651 commit c836231

File tree

4 files changed

+62
-71
lines changed

4 files changed

+62
-71
lines changed

.prettierrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
rangeEnd: Infinity,
1010
semi: true,
1111
singleQuote: true,
12-
tabWidth: 4,
12+
tabWidth: 2,
1313
trailingComma: 'es5',
1414
useTabs: false,
1515
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
{
2-
"name": "@arte/ra-apisix-oidc",
3-
"version": "1.2.0",
4-
"repository": {
5-
"type": "git",
6-
"url": "git+ssh://git@github.com/ArteGEIE/ra-apisix-oidc.git"
7-
},
8-
"author": "Guillaume BILLEY <guillaume@marmelab.com>",
9-
"license": "MIT",
10-
"files": [
11-
"*.md",
12-
"lib",
13-
"esm",
14-
"src"
15-
],
16-
"main": "lib/index",
17-
"module": "esm/index.js",
18-
"types": "esm/index.d.ts",
19-
"sideEffects": false,
20-
"peerDependencies": {
21-
"ra-core": "*"
22-
},
23-
"scripts": {
24-
"build": "yarn run build-cjs && yarn run build-esm",
25-
"build-cjs": "rimraf ./lib && tsc",
26-
"build-esm": "rimraf ./esm && tsc --outDir esm --module es2015",
27-
"watch": "tsc --outDir esm --module es2015 --watch",
28-
"lint": "eslint --fix ./src",
29-
"test-unit": "jest"
30-
},
31-
"devDependencies": {
32-
"rimraf": "^6.0.1",
33-
"typescript": "^5.5.4"
34-
}
2+
"name": "@arte/ra-apisix-oidc",
3+
"version": "1.2.1",
4+
"repository": {
5+
"type": "git",
6+
"url": "git+ssh://git@github.com/ArteGEIE/ra-apisix-oidc.git"
7+
},
8+
"author": "Guillaume BILLEY <guillaume@marmelab.com>",
9+
"license": "MIT",
10+
"files": [
11+
"*.md",
12+
"lib",
13+
"esm",
14+
"src"
15+
],
16+
"main": "lib/index",
17+
"module": "esm/index.js",
18+
"types": "esm/index.d.ts",
19+
"sideEffects": false,
20+
"peerDependencies": {
21+
"ra-core": "*"
22+
},
23+
"scripts": {
24+
"build": "yarn run build-cjs && yarn run build-esm",
25+
"build-cjs": "rimraf ./lib && tsc",
26+
"build-esm": "rimraf ./esm && tsc --outDir esm --module es2015",
27+
"watch": "tsc --outDir esm --module es2015 --watch",
28+
"lint": "eslint --fix ./src",
29+
"test-unit": "jest"
30+
},
31+
"devDependencies": {
32+
"rimraf": "^6.0.1",
33+
"typescript": "^5.5.4"
34+
}
3535
}

packages/ra-apisix-oidc/src/apisixOidcAuthProvider.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { type AuthProvider, PreviousLocationStorageKey } from "ra-core";
2-
1+
import { type AuthProvider, PreviousLocationStorageKey } from 'ra-core';
32

43
export type ApisixAuthProviderParams = {
54
loginURL?: string;
@@ -27,56 +26,48 @@ export type ApisixAuthProviderParams = {
2726
* meURL: 'http://localhost:9080/oidc/me',
2827
* });
2928
*/
30-
export const apisixOidcAuthProvider: (options?: ApisixAuthProviderParams) => AuthProvider = (options) => {
29+
export const apisixOidcAuthProvider = (
30+
options?: ApisixAuthProviderParams
31+
): AuthProvider => {
3132
const {
3233
loginURL = `${window.location.origin}/oidc/login`,
3334
logoutURL = `${window.location.origin}/oidc/logout`,
3435
userInfoURL = `${window.location.origin}/oidc/me`,
3536
storage = localStorage,
3637
} = options || {};
37-
let isRedirecting = false;
3838
return {
3939
login: () => {
4040
return Promise.reject();
4141
},
4242
logout: async () => {
43-
const accessToken = storage.getItem("access_token");
43+
const accessToken = storage.getItem('access_token');
4444
if (!accessToken) {
45-
return Promise.resolve();
45+
return Promise.reject();
4646
}
47-
storage.removeItem("access_token");
48-
return Promise.resolve(logoutURL);
47+
storage.removeItem('access_token');
48+
window.location.href = logoutURL;
49+
return Promise.reject();
4950
},
50-
checkError: (error) => {
51+
checkError: error => {
5152
if (error.status === 401) {
52-
storage.removeItem("access_token");
53-
if (!isRedirecting) {
54-
isRedirecting = true;
55-
saveCurrentLocation(storage);
56-
setTimeout(() => {
57-
window.location.href = loginURL;
58-
}, 100);
59-
}
60-
return Promise.reject({ logoutUser: false });
53+
storage.removeItem('access_token');
54+
saveCurrentLocation(storage);
55+
window.location.href = loginURL;
56+
return Promise.reject();
6157
}
6258
return Promise.resolve();
6359
},
6460
checkAuth: async () => {
65-
const accessToken = storage.getItem("access_token");
61+
const accessToken = storage.getItem('access_token');
6662
if (!accessToken) {
67-
if (!isRedirecting) {
68-
isRedirecting = true;
69-
saveCurrentLocation(storage);
70-
setTimeout(() => {
71-
window.location.href = loginURL;
72-
}, 100);
73-
}
74-
return Promise.reject({ redirectTo: false });
63+
saveCurrentLocation(storage);
64+
window.location.href = loginURL;
65+
return Promise.reject();
7566
}
7667
return Promise.resolve();
7768
},
7869
getIdentity: async () => {
79-
const accessToken = storage.getItem("access_token");
70+
const accessToken = storage.getItem('access_token');
8071
if (!accessToken) {
8172
return Promise.reject();
8273
}
@@ -96,9 +87,9 @@ export const apisixOidcAuthProvider: (options?: ApisixAuthProviderParams) => Aut
9687
const identity = {
9788
...user,
9889
id: user.sub,
99-
fullName: user.preferred_username || user.name || "",
100-
avatar: user.picture || "",
101-
email: user.email || "",
90+
fullName: user.preferred_username || user.name || '',
91+
avatar: user.picture || '',
92+
email: user.email || '',
10293
roles: user.roles || [],
10394
};
10495
return Promise.resolve(identity);
@@ -112,17 +103,17 @@ export const apisixOidcAuthProvider: (options?: ApisixAuthProviderParams) => Aut
112103
if (!body.accessToken) {
113104
return Promise.reject();
114105
}
115-
storage.setItem("access_token", body.accessToken);
106+
storage.setItem('access_token', body.accessToken);
116107
},
117-
}
108+
};
118109
};
119110

120111
const saveCurrentLocation = (storage: Storage) => {
121-
if (window.location.href.includes("login")) {
112+
if (window.location.href.includes('login')) {
122113
return; // Do not save the location if it's the login page
123114
}
124115
const locationToSave = window.location.href
125-
.replace(window.location.origin, "")
126-
.replace("/#/", "/");
116+
.replace(window.location.origin, '')
117+
.replace('/#/', '/');
127118
storage.setItem(PreviousLocationStorageKey, locationToSave);
128119
};

0 commit comments

Comments
 (0)