ra-auth-auth0 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/authProvider.cjs +146 -0
- package/{esm/authProvider.d.ts.map → dist/authProvider.d.cts.map} +1 -1
- package/{lib → dist}/authProvider.d.ts.map +1 -1
- package/dist/authProvider.js +142 -0
- package/dist/httpClient.cjs +41 -0
- package/dist/httpClient.js +36 -0
- package/{lib/index.js → dist/index.cjs} +2 -2
- package/dist/index.d.cts +3 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/package.json +26 -15
- package/src/authProvider.ts +21 -9
- package/esm/authProvider.js +0 -228
- package/esm/httpClient.js +0 -91
- package/esm/index.d.ts +0 -3
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -2
- package/lib/authProvider.js +0 -232
- package/lib/httpClient.js +0 -96
- package/lib/index.d.ts +0 -3
- package/lib/index.d.ts.map +0 -1
- /package/{esm/authProvider.d.ts → dist/authProvider.d.cts} +0 -0
- /package/{lib → dist}/authProvider.d.ts +0 -0
- /package/{esm/httpClient.d.ts → dist/httpClient.d.cts} +0 -0
- /package/{esm/httpClient.d.ts.map → dist/httpClient.d.cts.map} +0 -0
- /package/{lib → dist}/httpClient.d.ts +0 -0
- /package/{lib → dist}/httpClient.d.ts.map +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Auth0AuthProvider = void 0;
|
|
4
|
+
const react_admin_1 = require("react-admin");
|
|
5
|
+
/**
|
|
6
|
+
* An authProvider which handles authentication via the Auth0 instance.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* import React from 'react';
|
|
11
|
+
* import { Admin, Resource } from 'react-admin';
|
|
12
|
+
* import { BrowserRouter } from 'react-router-dom';
|
|
13
|
+
* import { Auth0AuthProvider } from 'ra-auth-auth0';
|
|
14
|
+
* import { Auth0Client } from '@auth0/auth0-spa-js';
|
|
15
|
+
* import dataProvider from './dataProvider';
|
|
16
|
+
* import posts from './posts';
|
|
17
|
+
*
|
|
18
|
+
* const clientAuth0 = new Auth0Client({
|
|
19
|
+
* domain: 'your-domain.auth0.com',
|
|
20
|
+
* clientId: 'your-client-id',
|
|
21
|
+
* cacheLocation: 'localstorage',
|
|
22
|
+
* // optional
|
|
23
|
+
* authorizationParams: {
|
|
24
|
+
* audience: 'https://your-domain.auth0.com/api/v2/',
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* const authProvider = Auth0AuthProvider(clientAuth0, {
|
|
29
|
+
* loginRedirectUri: 'http://localhost:3000/auth-callback',
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const App = () => {
|
|
33
|
+
* return (
|
|
34
|
+
* <BrowserRouter>
|
|
35
|
+
* <Admin
|
|
36
|
+
* authProvider={authProvider}
|
|
37
|
+
* dataProvider={dataProvider}
|
|
38
|
+
* title="Example Admin"
|
|
39
|
+
* >
|
|
40
|
+
* <Resource name="posts" {...posts} />
|
|
41
|
+
* </Admin>
|
|
42
|
+
* <BrowserRouter>
|
|
43
|
+
* );
|
|
44
|
+
* };
|
|
45
|
+
* export default App;
|
|
46
|
+
*
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param client the Auth0 client
|
|
50
|
+
* @param options The authProvider options
|
|
51
|
+
* @param options.loginRedirectUri The URI to which users should be redirected after they signed in. Must be whitelisted in the Auth0 application settings.
|
|
52
|
+
* @param options.logoutRedirectUri The URI to which users should be redirected after they signed out. Must be whitelisted in the Auth0 application settings.
|
|
53
|
+
* @param options.redirectOnCheckAuth If true, the user will be redirected to Auth0 when checkAuth fails. Defaults to true.
|
|
54
|
+
* @returns an authProvider ready to be used by React-Admin.
|
|
55
|
+
*/
|
|
56
|
+
const Auth0AuthProvider = (client, { loginRedirectUri, logoutRedirectUri, redirectOnCheckAuth = true, } = {
|
|
57
|
+
redirectOnCheckAuth: true,
|
|
58
|
+
}) => ({
|
|
59
|
+
// Used when the redirection to Auth0 is done from a custom login page
|
|
60
|
+
async login() {
|
|
61
|
+
client.loginWithRedirect({
|
|
62
|
+
authorizationParams: {
|
|
63
|
+
redirect_uri: loginRedirectUri ??
|
|
64
|
+
`${window.location.origin}/auth-callback`,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
// called when the user clicks on the logout button
|
|
69
|
+
async logout() {
|
|
70
|
+
await client.logout({
|
|
71
|
+
logoutParams: {
|
|
72
|
+
returnTo: logoutRedirectUri || window.location.origin,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
return false;
|
|
76
|
+
},
|
|
77
|
+
// called when the API returns an error
|
|
78
|
+
async checkError({ status }) {
|
|
79
|
+
if (status === 401 || status === 403) {
|
|
80
|
+
throw new Error('Unauthorized');
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
// called when the user navigates to a new location, to check for authentication
|
|
84
|
+
async checkAuth() {
|
|
85
|
+
const isAuthenticated = await client.isAuthenticated();
|
|
86
|
+
if (isAuthenticated) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (redirectOnCheckAuth) {
|
|
90
|
+
localStorage.setItem(react_admin_1.PreviousLocationStorageKey, window.location.href);
|
|
91
|
+
return client.loginWithRedirect({
|
|
92
|
+
authorizationParams: {
|
|
93
|
+
redirect_uri: loginRedirectUri ??
|
|
94
|
+
`${window.location.origin}/auth-callback`,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
throw new Error('Unauthorized');
|
|
99
|
+
},
|
|
100
|
+
// called when the user navigates to a new location, to check for permissions / roles
|
|
101
|
+
async getPermissions() {
|
|
102
|
+
if (!(await client.isAuthenticated())) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
// If Auth0 instance contains rules for returning permissions, use them
|
|
106
|
+
const claims = await client.getIdTokenClaims();
|
|
107
|
+
const roleProperty = Object.keys(claims).find(key => key.includes('role'));
|
|
108
|
+
return claims[roleProperty];
|
|
109
|
+
},
|
|
110
|
+
async getIdentity() {
|
|
111
|
+
if (await client.isAuthenticated()) {
|
|
112
|
+
const user = await client.getUser();
|
|
113
|
+
return {
|
|
114
|
+
id: user.email,
|
|
115
|
+
fullName: user.name,
|
|
116
|
+
avatar: user.picture,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
throw new Error('Failed to get identity.');
|
|
120
|
+
},
|
|
121
|
+
async handleCallback() {
|
|
122
|
+
if (!handleCallbackPromise) {
|
|
123
|
+
handleCallbackPromise = new Promise(async (resolve, reject) => {
|
|
124
|
+
const query = window.location.search;
|
|
125
|
+
if (query.includes('code=') && query.includes('state=')) {
|
|
126
|
+
try {
|
|
127
|
+
await client.handleRedirectCallback();
|
|
128
|
+
return resolve();
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
return reject({
|
|
132
|
+
redirectTo: false,
|
|
133
|
+
message: error.message,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return reject({
|
|
138
|
+
message: 'Failed to handle login callback.',
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return handleCallbackPromise;
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
exports.Auth0AuthProvider = Auth0AuthProvider;
|
|
146
|
+
let handleCallbackPromise = null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authProvider.d.ts","sourceRoot":"","sources":["../src/authProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA8B,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,eAAO,MAAM,iBAAiB,WAClB,WAAW,kEAKhB;IACC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,mBAAmB,CAAC,EAAE,OAAO,CAAC;CACjC,KAGF,
|
|
1
|
+
{"version":3,"file":"authProvider.d.ts","sourceRoot":"","sources":["../src/authProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA8B,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,eAAO,MAAM,iBAAiB,WAClB,WAAW,kEAKhB;IACC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,mBAAmB,CAAC,EAAE,OAAO,CAAC;CACjC,KAGF,YA8FD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authProvider.d.ts","sourceRoot":"","sources":["../src/authProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA8B,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,eAAO,MAAM,iBAAiB,WAClB,WAAW,kEAKhB;IACC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,mBAAmB,CAAC,EAAE,OAAO,CAAC;CACjC,KAGF,
|
|
1
|
+
{"version":3,"file":"authProvider.d.ts","sourceRoot":"","sources":["../src/authProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA8B,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,eAAO,MAAM,iBAAiB,WAClB,WAAW,kEAKhB;IACC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,mBAAmB,CAAC,EAAE,OAAO,CAAC;CACjC,KAGF,YA8FD,CAAC"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { PreviousLocationStorageKey } from 'react-admin';
|
|
2
|
+
/**
|
|
3
|
+
* An authProvider which handles authentication via the Auth0 instance.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* import React from 'react';
|
|
8
|
+
* import { Admin, Resource } from 'react-admin';
|
|
9
|
+
* import { BrowserRouter } from 'react-router-dom';
|
|
10
|
+
* import { Auth0AuthProvider } from 'ra-auth-auth0';
|
|
11
|
+
* import { Auth0Client } from '@auth0/auth0-spa-js';
|
|
12
|
+
* import dataProvider from './dataProvider';
|
|
13
|
+
* import posts from './posts';
|
|
14
|
+
*
|
|
15
|
+
* const clientAuth0 = new Auth0Client({
|
|
16
|
+
* domain: 'your-domain.auth0.com',
|
|
17
|
+
* clientId: 'your-client-id',
|
|
18
|
+
* cacheLocation: 'localstorage',
|
|
19
|
+
* // optional
|
|
20
|
+
* authorizationParams: {
|
|
21
|
+
* audience: 'https://your-domain.auth0.com/api/v2/',
|
|
22
|
+
* },
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const authProvider = Auth0AuthProvider(clientAuth0, {
|
|
26
|
+
* loginRedirectUri: 'http://localhost:3000/auth-callback',
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* const App = () => {
|
|
30
|
+
* return (
|
|
31
|
+
* <BrowserRouter>
|
|
32
|
+
* <Admin
|
|
33
|
+
* authProvider={authProvider}
|
|
34
|
+
* dataProvider={dataProvider}
|
|
35
|
+
* title="Example Admin"
|
|
36
|
+
* >
|
|
37
|
+
* <Resource name="posts" {...posts} />
|
|
38
|
+
* </Admin>
|
|
39
|
+
* <BrowserRouter>
|
|
40
|
+
* );
|
|
41
|
+
* };
|
|
42
|
+
* export default App;
|
|
43
|
+
*
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @param client the Auth0 client
|
|
47
|
+
* @param options The authProvider options
|
|
48
|
+
* @param options.loginRedirectUri The URI to which users should be redirected after they signed in. Must be whitelisted in the Auth0 application settings.
|
|
49
|
+
* @param options.logoutRedirectUri The URI to which users should be redirected after they signed out. Must be whitelisted in the Auth0 application settings.
|
|
50
|
+
* @param options.redirectOnCheckAuth If true, the user will be redirected to Auth0 when checkAuth fails. Defaults to true.
|
|
51
|
+
* @returns an authProvider ready to be used by React-Admin.
|
|
52
|
+
*/
|
|
53
|
+
export const Auth0AuthProvider = (client, { loginRedirectUri, logoutRedirectUri, redirectOnCheckAuth = true, } = {
|
|
54
|
+
redirectOnCheckAuth: true,
|
|
55
|
+
}) => ({
|
|
56
|
+
// Used when the redirection to Auth0 is done from a custom login page
|
|
57
|
+
async login() {
|
|
58
|
+
client.loginWithRedirect({
|
|
59
|
+
authorizationParams: {
|
|
60
|
+
redirect_uri: loginRedirectUri ??
|
|
61
|
+
`${window.location.origin}/auth-callback`,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
// called when the user clicks on the logout button
|
|
66
|
+
async logout() {
|
|
67
|
+
await client.logout({
|
|
68
|
+
logoutParams: {
|
|
69
|
+
returnTo: logoutRedirectUri || window.location.origin,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
return false;
|
|
73
|
+
},
|
|
74
|
+
// called when the API returns an error
|
|
75
|
+
async checkError({ status }) {
|
|
76
|
+
if (status === 401 || status === 403) {
|
|
77
|
+
throw new Error('Unauthorized');
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
// called when the user navigates to a new location, to check for authentication
|
|
81
|
+
async checkAuth() {
|
|
82
|
+
const isAuthenticated = await client.isAuthenticated();
|
|
83
|
+
if (isAuthenticated) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (redirectOnCheckAuth) {
|
|
87
|
+
localStorage.setItem(PreviousLocationStorageKey, window.location.href);
|
|
88
|
+
return client.loginWithRedirect({
|
|
89
|
+
authorizationParams: {
|
|
90
|
+
redirect_uri: loginRedirectUri ??
|
|
91
|
+
`${window.location.origin}/auth-callback`,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
throw new Error('Unauthorized');
|
|
96
|
+
},
|
|
97
|
+
// called when the user navigates to a new location, to check for permissions / roles
|
|
98
|
+
async getPermissions() {
|
|
99
|
+
if (!(await client.isAuthenticated())) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
// If Auth0 instance contains rules for returning permissions, use them
|
|
103
|
+
const claims = await client.getIdTokenClaims();
|
|
104
|
+
const roleProperty = Object.keys(claims).find(key => key.includes('role'));
|
|
105
|
+
return claims[roleProperty];
|
|
106
|
+
},
|
|
107
|
+
async getIdentity() {
|
|
108
|
+
if (await client.isAuthenticated()) {
|
|
109
|
+
const user = await client.getUser();
|
|
110
|
+
return {
|
|
111
|
+
id: user.email,
|
|
112
|
+
fullName: user.name,
|
|
113
|
+
avatar: user.picture,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
throw new Error('Failed to get identity.');
|
|
117
|
+
},
|
|
118
|
+
async handleCallback() {
|
|
119
|
+
if (!handleCallbackPromise) {
|
|
120
|
+
handleCallbackPromise = new Promise(async (resolve, reject) => {
|
|
121
|
+
const query = window.location.search;
|
|
122
|
+
if (query.includes('code=') && query.includes('state=')) {
|
|
123
|
+
try {
|
|
124
|
+
await client.handleRedirectCallback();
|
|
125
|
+
return resolve();
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
return reject({
|
|
129
|
+
redirectTo: false,
|
|
130
|
+
message: error.message,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return reject({
|
|
135
|
+
message: 'Failed to handle login callback.',
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return handleCallbackPromise;
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
let handleCallbackPromise = null;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAuth0Headers = exports.httpClient = void 0;
|
|
4
|
+
const react_admin_1 = require("react-admin");
|
|
5
|
+
/**
|
|
6
|
+
* An httpClient that adds authentication headers needed by Auth0 in all requests.
|
|
7
|
+
* @param auth0Client the Auth0 client.
|
|
8
|
+
* @returns a function with the same definition as `httpClient` that adds an `Authorization` header containing the Auth0 token.
|
|
9
|
+
*/
|
|
10
|
+
const httpClient = (auth0Client) => async (url, options) => {
|
|
11
|
+
let token = null;
|
|
12
|
+
try {
|
|
13
|
+
token = await auth0Client.getTokenSilently();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
// Nothing to do, we just won't set the Authorization header
|
|
17
|
+
}
|
|
18
|
+
const requestHeaders = (0, exports.getAuth0Headers)(token, options);
|
|
19
|
+
return react_admin_1.fetchUtils.fetchJson(url, {
|
|
20
|
+
...options,
|
|
21
|
+
headers: requestHeaders,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
exports.httpClient = httpClient;
|
|
25
|
+
/**
|
|
26
|
+
* Return the headers needed by Auth0.
|
|
27
|
+
* @param token the Auth0 token
|
|
28
|
+
* @param options the fetch options (so that we do not override other headers)
|
|
29
|
+
* @returns the headers needed by Auth0
|
|
30
|
+
*/
|
|
31
|
+
const getAuth0Headers = (token, options) => {
|
|
32
|
+
const headers = ((options && options.headers) ||
|
|
33
|
+
new Headers({
|
|
34
|
+
Accept: 'application/json',
|
|
35
|
+
}));
|
|
36
|
+
if (token) {
|
|
37
|
+
headers.set('Authorization', `Bearer ${token}`);
|
|
38
|
+
}
|
|
39
|
+
return headers;
|
|
40
|
+
};
|
|
41
|
+
exports.getAuth0Headers = getAuth0Headers;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { fetchUtils } from 'react-admin';
|
|
2
|
+
/**
|
|
3
|
+
* An httpClient that adds authentication headers needed by Auth0 in all requests.
|
|
4
|
+
* @param auth0Client the Auth0 client.
|
|
5
|
+
* @returns a function with the same definition as `httpClient` that adds an `Authorization` header containing the Auth0 token.
|
|
6
|
+
*/
|
|
7
|
+
export const httpClient = (auth0Client) => async (url, options) => {
|
|
8
|
+
let token = null;
|
|
9
|
+
try {
|
|
10
|
+
token = await auth0Client.getTokenSilently();
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
// Nothing to do, we just won't set the Authorization header
|
|
14
|
+
}
|
|
15
|
+
const requestHeaders = getAuth0Headers(token, options);
|
|
16
|
+
return fetchUtils.fetchJson(url, {
|
|
17
|
+
...options,
|
|
18
|
+
headers: requestHeaders,
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Return the headers needed by Auth0.
|
|
23
|
+
* @param token the Auth0 token
|
|
24
|
+
* @param options the fetch options (so that we do not override other headers)
|
|
25
|
+
* @returns the headers needed by Auth0
|
|
26
|
+
*/
|
|
27
|
+
export const getAuth0Headers = (token, options) => {
|
|
28
|
+
const headers = ((options && options.headers) ||
|
|
29
|
+
new Headers({
|
|
30
|
+
Accept: 'application/json',
|
|
31
|
+
}));
|
|
32
|
+
if (token) {
|
|
33
|
+
headers.set('Authorization', `Bearer ${token}`);
|
|
34
|
+
}
|
|
35
|
+
return headers;
|
|
36
|
+
};
|
|
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./authProvider"), exports);
|
|
18
|
-
__exportStar(require("./httpClient"), exports);
|
|
17
|
+
__exportStar(require("./authProvider.cjs"), exports);
|
|
18
|
+
__exportStar(require("./httpClient.cjs"), exports);
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mCAA+B;AAC/B,iCAA6B"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kCAA+B;AAC/B,gCAA6B"}
|
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,30 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ra-auth-auth0",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"repository": "git@github.com:marmelab/ra-auth-auth0.git",
|
|
5
5
|
"author": "Anthony Rimet <anthony@marmelab.com> (https://marmelab.com/)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"files": [
|
|
8
8
|
"*.md",
|
|
9
|
-
"
|
|
10
|
-
"esm",
|
|
9
|
+
"dist",
|
|
11
10
|
"src"
|
|
12
11
|
],
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
12
|
+
"type": "module",
|
|
13
|
+
"zshy": {
|
|
14
|
+
"exports": "./src/index.ts",
|
|
15
|
+
"noEdit": true
|
|
16
|
+
},
|
|
17
|
+
"main": "./dist/index.cjs",
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.cts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./dist/index.d.cts",
|
|
28
|
+
"default": "./dist/index.cjs"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
16
32
|
"sideEffects": false,
|
|
17
33
|
"peerDependencies": {
|
|
18
34
|
"@auth0/auth0-spa-js": "^2.0.0",
|
|
19
|
-
"react": "^
|
|
20
|
-
"react-admin": "^5.1.1",
|
|
21
|
-
"react-dom": "^18.0.0 || ^19.0.0"
|
|
35
|
+
"react-admin": "^5.1.1"
|
|
22
36
|
},
|
|
23
37
|
"scripts": {
|
|
24
|
-
"build": "
|
|
25
|
-
"build-cjs": "rimraf ./lib && tsc",
|
|
26
|
-
"build-esm": "rimraf ./esm && tsc --outDir esm --module es2015",
|
|
27
|
-
"watch": "tsc --outDir esm --module es2015 --watch",
|
|
38
|
+
"build": "zshy",
|
|
28
39
|
"lint": "eslint --fix ./src",
|
|
29
40
|
"test-unit": "jest"
|
|
30
41
|
},
|
|
@@ -34,9 +45,9 @@
|
|
|
34
45
|
"jest-environment-jsdom": "^29.1.1",
|
|
35
46
|
"react": "^18.3.1",
|
|
36
47
|
"react-dom": "^18.3.1",
|
|
37
|
-
"rimraf": "^3.0.2",
|
|
38
48
|
"ts-jest": "^29.0.3",
|
|
39
|
-
"typescript": "^5.5.4"
|
|
49
|
+
"typescript": "^5.5.4",
|
|
50
|
+
"zshy": "^0.8.0"
|
|
40
51
|
},
|
|
41
52
|
"jest": {
|
|
42
53
|
"preset": "ts-jest",
|
package/src/authProvider.ts
CHANGED
|
@@ -138,15 +138,27 @@ export const Auth0AuthProvider = (
|
|
|
138
138
|
throw new Error('Failed to get identity.');
|
|
139
139
|
},
|
|
140
140
|
async handleCallback() {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
141
|
+
if (!handleCallbackPromise) {
|
|
142
|
+
handleCallbackPromise = new Promise(async (resolve, reject) => {
|
|
143
|
+
const query = window.location.search;
|
|
144
|
+
if (query.includes('code=') && query.includes('state=')) {
|
|
145
|
+
try {
|
|
146
|
+
await client.handleRedirectCallback();
|
|
147
|
+
return resolve();
|
|
148
|
+
} catch (error) {
|
|
149
|
+
return reject({
|
|
150
|
+
redirectTo: false,
|
|
151
|
+
message: error.message,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return reject({
|
|
156
|
+
message: 'Failed to handle login callback.',
|
|
157
|
+
});
|
|
158
|
+
});
|
|
149
159
|
}
|
|
150
|
-
|
|
160
|
+
return handleCallbackPromise;
|
|
151
161
|
},
|
|
152
162
|
});
|
|
163
|
+
|
|
164
|
+
let handleCallbackPromise: Promise<void> | null = null;
|
package/esm/authProvider.js
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
import { PreviousLocationStorageKey } from 'react-admin';
|
|
38
|
-
/**
|
|
39
|
-
* An authProvider which handles authentication via the Auth0 instance.
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```tsx
|
|
43
|
-
* import React from 'react';
|
|
44
|
-
* import { Admin, Resource } from 'react-admin';
|
|
45
|
-
* import { BrowserRouter } from 'react-router-dom';
|
|
46
|
-
* import { Auth0AuthProvider } from 'ra-auth-auth0';
|
|
47
|
-
* import { Auth0Client } from '@auth0/auth0-spa-js';
|
|
48
|
-
* import dataProvider from './dataProvider';
|
|
49
|
-
* import posts from './posts';
|
|
50
|
-
*
|
|
51
|
-
* const clientAuth0 = new Auth0Client({
|
|
52
|
-
* domain: 'your-domain.auth0.com',
|
|
53
|
-
* clientId: 'your-client-id',
|
|
54
|
-
* cacheLocation: 'localstorage',
|
|
55
|
-
* // optional
|
|
56
|
-
* authorizationParams: {
|
|
57
|
-
* audience: 'https://your-domain.auth0.com/api/v2/',
|
|
58
|
-
* },
|
|
59
|
-
* });
|
|
60
|
-
*
|
|
61
|
-
* const authProvider = Auth0AuthProvider(clientAuth0, {
|
|
62
|
-
* loginRedirectUri: 'http://localhost:3000/auth-callback',
|
|
63
|
-
* });
|
|
64
|
-
*
|
|
65
|
-
* const App = () => {
|
|
66
|
-
* return (
|
|
67
|
-
* <BrowserRouter>
|
|
68
|
-
* <Admin
|
|
69
|
-
* authProvider={authProvider}
|
|
70
|
-
* dataProvider={dataProvider}
|
|
71
|
-
* title="Example Admin"
|
|
72
|
-
* >
|
|
73
|
-
* <Resource name="posts" {...posts} />
|
|
74
|
-
* </Admin>
|
|
75
|
-
* <BrowserRouter>
|
|
76
|
-
* );
|
|
77
|
-
* };
|
|
78
|
-
* export default App;
|
|
79
|
-
*
|
|
80
|
-
* ```
|
|
81
|
-
*
|
|
82
|
-
* @param client the Auth0 client
|
|
83
|
-
* @param options The authProvider options
|
|
84
|
-
* @param options.loginRedirectUri The URI to which users should be redirected after they signed in. Must be whitelisted in the Auth0 application settings.
|
|
85
|
-
* @param options.logoutRedirectUri The URI to which users should be redirected after they signed out. Must be whitelisted in the Auth0 application settings.
|
|
86
|
-
* @param options.redirectOnCheckAuth If true, the user will be redirected to Auth0 when checkAuth fails. Defaults to true.
|
|
87
|
-
* @returns an authProvider ready to be used by React-Admin.
|
|
88
|
-
*/
|
|
89
|
-
export var Auth0AuthProvider = function (client, _a) {
|
|
90
|
-
var _b = _a === void 0 ? {
|
|
91
|
-
redirectOnCheckAuth: true,
|
|
92
|
-
} : _a, loginRedirectUri = _b.loginRedirectUri, logoutRedirectUri = _b.logoutRedirectUri, _c = _b.redirectOnCheckAuth, redirectOnCheckAuth = _c === void 0 ? true : _c;
|
|
93
|
-
return ({
|
|
94
|
-
// Used when the redirection to Auth0 is done from a custom login page
|
|
95
|
-
login: function () {
|
|
96
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
97
|
-
return __generator(this, function (_a) {
|
|
98
|
-
client.loginWithRedirect({
|
|
99
|
-
authorizationParams: {
|
|
100
|
-
redirect_uri: loginRedirectUri !== null && loginRedirectUri !== void 0 ? loginRedirectUri : "".concat(window.location.origin, "/auth-callback"),
|
|
101
|
-
},
|
|
102
|
-
});
|
|
103
|
-
return [2 /*return*/];
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
},
|
|
107
|
-
// called when the user clicks on the logout button
|
|
108
|
-
logout: function () {
|
|
109
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
110
|
-
return __generator(this, function (_a) {
|
|
111
|
-
switch (_a.label) {
|
|
112
|
-
case 0: return [4 /*yield*/, client.logout({
|
|
113
|
-
logoutParams: {
|
|
114
|
-
returnTo: logoutRedirectUri || window.location.origin,
|
|
115
|
-
},
|
|
116
|
-
})];
|
|
117
|
-
case 1:
|
|
118
|
-
_a.sent();
|
|
119
|
-
return [2 /*return*/, false];
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
},
|
|
124
|
-
// called when the API returns an error
|
|
125
|
-
checkError: function (_a) {
|
|
126
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
127
|
-
var status = _b.status;
|
|
128
|
-
return __generator(this, function (_c) {
|
|
129
|
-
if (status === 401 || status === 403) {
|
|
130
|
-
throw new Error('Unauthorized');
|
|
131
|
-
}
|
|
132
|
-
return [2 /*return*/];
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
},
|
|
136
|
-
// called when the user navigates to a new location, to check for authentication
|
|
137
|
-
checkAuth: function () {
|
|
138
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
139
|
-
var isAuthenticated;
|
|
140
|
-
return __generator(this, function (_a) {
|
|
141
|
-
switch (_a.label) {
|
|
142
|
-
case 0: return [4 /*yield*/, client.isAuthenticated()];
|
|
143
|
-
case 1:
|
|
144
|
-
isAuthenticated = _a.sent();
|
|
145
|
-
if (isAuthenticated) {
|
|
146
|
-
return [2 /*return*/];
|
|
147
|
-
}
|
|
148
|
-
if (redirectOnCheckAuth) {
|
|
149
|
-
localStorage.setItem(PreviousLocationStorageKey, window.location.href);
|
|
150
|
-
return [2 /*return*/, client.loginWithRedirect({
|
|
151
|
-
authorizationParams: {
|
|
152
|
-
redirect_uri: loginRedirectUri !== null && loginRedirectUri !== void 0 ? loginRedirectUri : "".concat(window.location.origin, "/auth-callback"),
|
|
153
|
-
},
|
|
154
|
-
})];
|
|
155
|
-
}
|
|
156
|
-
throw new Error('Unauthorized');
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
},
|
|
161
|
-
// called when the user navigates to a new location, to check for permissions / roles
|
|
162
|
-
getPermissions: function () {
|
|
163
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
164
|
-
var claims, roleProperty;
|
|
165
|
-
return __generator(this, function (_a) {
|
|
166
|
-
switch (_a.label) {
|
|
167
|
-
case 0: return [4 /*yield*/, client.isAuthenticated()];
|
|
168
|
-
case 1:
|
|
169
|
-
if (!(_a.sent())) {
|
|
170
|
-
return [2 /*return*/];
|
|
171
|
-
}
|
|
172
|
-
return [4 /*yield*/, client.getIdTokenClaims()];
|
|
173
|
-
case 2:
|
|
174
|
-
claims = _a.sent();
|
|
175
|
-
roleProperty = Object.keys(claims).find(function (key) {
|
|
176
|
-
return key.includes('role');
|
|
177
|
-
});
|
|
178
|
-
return [2 /*return*/, claims[roleProperty]];
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
},
|
|
183
|
-
getIdentity: function () {
|
|
184
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
185
|
-
var user;
|
|
186
|
-
return __generator(this, function (_a) {
|
|
187
|
-
switch (_a.label) {
|
|
188
|
-
case 0: return [4 /*yield*/, client.isAuthenticated()];
|
|
189
|
-
case 1:
|
|
190
|
-
if (!_a.sent()) return [3 /*break*/, 3];
|
|
191
|
-
return [4 /*yield*/, client.getUser()];
|
|
192
|
-
case 2:
|
|
193
|
-
user = _a.sent();
|
|
194
|
-
return [2 /*return*/, {
|
|
195
|
-
id: user.email,
|
|
196
|
-
fullName: user.name,
|
|
197
|
-
avatar: user.picture,
|
|
198
|
-
}];
|
|
199
|
-
case 3: throw new Error('Failed to get identity.');
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
},
|
|
204
|
-
handleCallback: function () {
|
|
205
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
206
|
-
var query, error_1;
|
|
207
|
-
return __generator(this, function (_a) {
|
|
208
|
-
switch (_a.label) {
|
|
209
|
-
case 0:
|
|
210
|
-
query = window.location.search;
|
|
211
|
-
if (!(query.includes('code=') && query.includes('state='))) return [3 /*break*/, 4];
|
|
212
|
-
_a.label = 1;
|
|
213
|
-
case 1:
|
|
214
|
-
_a.trys.push([1, 3, , 4]);
|
|
215
|
-
return [4 /*yield*/, client.handleRedirectCallback()];
|
|
216
|
-
case 2:
|
|
217
|
-
_a.sent();
|
|
218
|
-
return [2 /*return*/];
|
|
219
|
-
case 3:
|
|
220
|
-
error_1 = _a.sent();
|
|
221
|
-
throw { redirectTo: false, message: error_1.message };
|
|
222
|
-
case 4: throw new Error('Failed to handle login callback.');
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
},
|
|
227
|
-
});
|
|
228
|
-
};
|
package/esm/httpClient.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
import { fetchUtils } from 'react-admin';
|
|
49
|
-
/**
|
|
50
|
-
* An httpClient that adds authentication headers needed by Auth0 in all requests.
|
|
51
|
-
* @param auth0Client the Auth0 client.
|
|
52
|
-
* @returns a function with the same definition as `httpClient` that adds an `Authorization` header containing the Auth0 token.
|
|
53
|
-
*/
|
|
54
|
-
export var httpClient = function (auth0Client) { return function (url, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
55
|
-
var token, error_1, requestHeaders;
|
|
56
|
-
return __generator(this, function (_a) {
|
|
57
|
-
switch (_a.label) {
|
|
58
|
-
case 0:
|
|
59
|
-
token = null;
|
|
60
|
-
_a.label = 1;
|
|
61
|
-
case 1:
|
|
62
|
-
_a.trys.push([1, 3, , 4]);
|
|
63
|
-
return [4 /*yield*/, auth0Client.getTokenSilently()];
|
|
64
|
-
case 2:
|
|
65
|
-
token = _a.sent();
|
|
66
|
-
return [3 /*break*/, 4];
|
|
67
|
-
case 3:
|
|
68
|
-
error_1 = _a.sent();
|
|
69
|
-
return [3 /*break*/, 4];
|
|
70
|
-
case 4:
|
|
71
|
-
requestHeaders = getAuth0Headers(token, options);
|
|
72
|
-
return [2 /*return*/, fetchUtils.fetchJson(url, __assign(__assign({}, options), { headers: requestHeaders }))];
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
}); }; };
|
|
76
|
-
/**
|
|
77
|
-
* Return the headers needed by Auth0.
|
|
78
|
-
* @param token the Auth0 token
|
|
79
|
-
* @param options the fetch options (so that we do not override other headers)
|
|
80
|
-
* @returns the headers needed by Auth0
|
|
81
|
-
*/
|
|
82
|
-
export var getAuth0Headers = function (token, options) {
|
|
83
|
-
var headers = ((options && options.headers) ||
|
|
84
|
-
new Headers({
|
|
85
|
-
Accept: 'application/json',
|
|
86
|
-
}));
|
|
87
|
-
if (token) {
|
|
88
|
-
headers.set('Authorization', "Bearer ".concat(token));
|
|
89
|
-
}
|
|
90
|
-
return headers;
|
|
91
|
-
};
|
package/esm/index.d.ts
DELETED
package/esm/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC"}
|
package/esm/index.js
DELETED
package/lib/authProvider.js
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.Auth0AuthProvider = void 0;
|
|
40
|
-
var react_admin_1 = require("react-admin");
|
|
41
|
-
/**
|
|
42
|
-
* An authProvider which handles authentication via the Auth0 instance.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```tsx
|
|
46
|
-
* import React from 'react';
|
|
47
|
-
* import { Admin, Resource } from 'react-admin';
|
|
48
|
-
* import { BrowserRouter } from 'react-router-dom';
|
|
49
|
-
* import { Auth0AuthProvider } from 'ra-auth-auth0';
|
|
50
|
-
* import { Auth0Client } from '@auth0/auth0-spa-js';
|
|
51
|
-
* import dataProvider from './dataProvider';
|
|
52
|
-
* import posts from './posts';
|
|
53
|
-
*
|
|
54
|
-
* const clientAuth0 = new Auth0Client({
|
|
55
|
-
* domain: 'your-domain.auth0.com',
|
|
56
|
-
* clientId: 'your-client-id',
|
|
57
|
-
* cacheLocation: 'localstorage',
|
|
58
|
-
* // optional
|
|
59
|
-
* authorizationParams: {
|
|
60
|
-
* audience: 'https://your-domain.auth0.com/api/v2/',
|
|
61
|
-
* },
|
|
62
|
-
* });
|
|
63
|
-
*
|
|
64
|
-
* const authProvider = Auth0AuthProvider(clientAuth0, {
|
|
65
|
-
* loginRedirectUri: 'http://localhost:3000/auth-callback',
|
|
66
|
-
* });
|
|
67
|
-
*
|
|
68
|
-
* const App = () => {
|
|
69
|
-
* return (
|
|
70
|
-
* <BrowserRouter>
|
|
71
|
-
* <Admin
|
|
72
|
-
* authProvider={authProvider}
|
|
73
|
-
* dataProvider={dataProvider}
|
|
74
|
-
* title="Example Admin"
|
|
75
|
-
* >
|
|
76
|
-
* <Resource name="posts" {...posts} />
|
|
77
|
-
* </Admin>
|
|
78
|
-
* <BrowserRouter>
|
|
79
|
-
* );
|
|
80
|
-
* };
|
|
81
|
-
* export default App;
|
|
82
|
-
*
|
|
83
|
-
* ```
|
|
84
|
-
*
|
|
85
|
-
* @param client the Auth0 client
|
|
86
|
-
* @param options The authProvider options
|
|
87
|
-
* @param options.loginRedirectUri The URI to which users should be redirected after they signed in. Must be whitelisted in the Auth0 application settings.
|
|
88
|
-
* @param options.logoutRedirectUri The URI to which users should be redirected after they signed out. Must be whitelisted in the Auth0 application settings.
|
|
89
|
-
* @param options.redirectOnCheckAuth If true, the user will be redirected to Auth0 when checkAuth fails. Defaults to true.
|
|
90
|
-
* @returns an authProvider ready to be used by React-Admin.
|
|
91
|
-
*/
|
|
92
|
-
var Auth0AuthProvider = function (client, _a) {
|
|
93
|
-
var _b = _a === void 0 ? {
|
|
94
|
-
redirectOnCheckAuth: true,
|
|
95
|
-
} : _a, loginRedirectUri = _b.loginRedirectUri, logoutRedirectUri = _b.logoutRedirectUri, _c = _b.redirectOnCheckAuth, redirectOnCheckAuth = _c === void 0 ? true : _c;
|
|
96
|
-
return ({
|
|
97
|
-
// Used when the redirection to Auth0 is done from a custom login page
|
|
98
|
-
login: function () {
|
|
99
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
100
|
-
return __generator(this, function (_a) {
|
|
101
|
-
client.loginWithRedirect({
|
|
102
|
-
authorizationParams: {
|
|
103
|
-
redirect_uri: loginRedirectUri !== null && loginRedirectUri !== void 0 ? loginRedirectUri : "".concat(window.location.origin, "/auth-callback"),
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
return [2 /*return*/];
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
},
|
|
110
|
-
// called when the user clicks on the logout button
|
|
111
|
-
logout: function () {
|
|
112
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
-
return __generator(this, function (_a) {
|
|
114
|
-
switch (_a.label) {
|
|
115
|
-
case 0: return [4 /*yield*/, client.logout({
|
|
116
|
-
logoutParams: {
|
|
117
|
-
returnTo: logoutRedirectUri || window.location.origin,
|
|
118
|
-
},
|
|
119
|
-
})];
|
|
120
|
-
case 1:
|
|
121
|
-
_a.sent();
|
|
122
|
-
return [2 /*return*/, false];
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
},
|
|
127
|
-
// called when the API returns an error
|
|
128
|
-
checkError: function (_a) {
|
|
129
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
130
|
-
var status = _b.status;
|
|
131
|
-
return __generator(this, function (_c) {
|
|
132
|
-
if (status === 401 || status === 403) {
|
|
133
|
-
throw new Error('Unauthorized');
|
|
134
|
-
}
|
|
135
|
-
return [2 /*return*/];
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
},
|
|
139
|
-
// called when the user navigates to a new location, to check for authentication
|
|
140
|
-
checkAuth: function () {
|
|
141
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
142
|
-
var isAuthenticated;
|
|
143
|
-
return __generator(this, function (_a) {
|
|
144
|
-
switch (_a.label) {
|
|
145
|
-
case 0: return [4 /*yield*/, client.isAuthenticated()];
|
|
146
|
-
case 1:
|
|
147
|
-
isAuthenticated = _a.sent();
|
|
148
|
-
if (isAuthenticated) {
|
|
149
|
-
return [2 /*return*/];
|
|
150
|
-
}
|
|
151
|
-
if (redirectOnCheckAuth) {
|
|
152
|
-
localStorage.setItem(react_admin_1.PreviousLocationStorageKey, window.location.href);
|
|
153
|
-
return [2 /*return*/, client.loginWithRedirect({
|
|
154
|
-
authorizationParams: {
|
|
155
|
-
redirect_uri: loginRedirectUri !== null && loginRedirectUri !== void 0 ? loginRedirectUri : "".concat(window.location.origin, "/auth-callback"),
|
|
156
|
-
},
|
|
157
|
-
})];
|
|
158
|
-
}
|
|
159
|
-
throw new Error('Unauthorized');
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
},
|
|
164
|
-
// called when the user navigates to a new location, to check for permissions / roles
|
|
165
|
-
getPermissions: function () {
|
|
166
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
167
|
-
var claims, roleProperty;
|
|
168
|
-
return __generator(this, function (_a) {
|
|
169
|
-
switch (_a.label) {
|
|
170
|
-
case 0: return [4 /*yield*/, client.isAuthenticated()];
|
|
171
|
-
case 1:
|
|
172
|
-
if (!(_a.sent())) {
|
|
173
|
-
return [2 /*return*/];
|
|
174
|
-
}
|
|
175
|
-
return [4 /*yield*/, client.getIdTokenClaims()];
|
|
176
|
-
case 2:
|
|
177
|
-
claims = _a.sent();
|
|
178
|
-
roleProperty = Object.keys(claims).find(function (key) {
|
|
179
|
-
return key.includes('role');
|
|
180
|
-
});
|
|
181
|
-
return [2 /*return*/, claims[roleProperty]];
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
},
|
|
186
|
-
getIdentity: function () {
|
|
187
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
188
|
-
var user;
|
|
189
|
-
return __generator(this, function (_a) {
|
|
190
|
-
switch (_a.label) {
|
|
191
|
-
case 0: return [4 /*yield*/, client.isAuthenticated()];
|
|
192
|
-
case 1:
|
|
193
|
-
if (!_a.sent()) return [3 /*break*/, 3];
|
|
194
|
-
return [4 /*yield*/, client.getUser()];
|
|
195
|
-
case 2:
|
|
196
|
-
user = _a.sent();
|
|
197
|
-
return [2 /*return*/, {
|
|
198
|
-
id: user.email,
|
|
199
|
-
fullName: user.name,
|
|
200
|
-
avatar: user.picture,
|
|
201
|
-
}];
|
|
202
|
-
case 3: throw new Error('Failed to get identity.');
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
},
|
|
207
|
-
handleCallback: function () {
|
|
208
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
209
|
-
var query, error_1;
|
|
210
|
-
return __generator(this, function (_a) {
|
|
211
|
-
switch (_a.label) {
|
|
212
|
-
case 0:
|
|
213
|
-
query = window.location.search;
|
|
214
|
-
if (!(query.includes('code=') && query.includes('state='))) return [3 /*break*/, 4];
|
|
215
|
-
_a.label = 1;
|
|
216
|
-
case 1:
|
|
217
|
-
_a.trys.push([1, 3, , 4]);
|
|
218
|
-
return [4 /*yield*/, client.handleRedirectCallback()];
|
|
219
|
-
case 2:
|
|
220
|
-
_a.sent();
|
|
221
|
-
return [2 /*return*/];
|
|
222
|
-
case 3:
|
|
223
|
-
error_1 = _a.sent();
|
|
224
|
-
throw { redirectTo: false, message: error_1.message };
|
|
225
|
-
case 4: throw new Error('Failed to handle login callback.');
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
},
|
|
230
|
-
});
|
|
231
|
-
};
|
|
232
|
-
exports.Auth0AuthProvider = Auth0AuthProvider;
|
package/lib/httpClient.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.getAuth0Headers = exports.httpClient = void 0;
|
|
51
|
-
var react_admin_1 = require("react-admin");
|
|
52
|
-
/**
|
|
53
|
-
* An httpClient that adds authentication headers needed by Auth0 in all requests.
|
|
54
|
-
* @param auth0Client the Auth0 client.
|
|
55
|
-
* @returns a function with the same definition as `httpClient` that adds an `Authorization` header containing the Auth0 token.
|
|
56
|
-
*/
|
|
57
|
-
var httpClient = function (auth0Client) { return function (url, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
58
|
-
var token, error_1, requestHeaders;
|
|
59
|
-
return __generator(this, function (_a) {
|
|
60
|
-
switch (_a.label) {
|
|
61
|
-
case 0:
|
|
62
|
-
token = null;
|
|
63
|
-
_a.label = 1;
|
|
64
|
-
case 1:
|
|
65
|
-
_a.trys.push([1, 3, , 4]);
|
|
66
|
-
return [4 /*yield*/, auth0Client.getTokenSilently()];
|
|
67
|
-
case 2:
|
|
68
|
-
token = _a.sent();
|
|
69
|
-
return [3 /*break*/, 4];
|
|
70
|
-
case 3:
|
|
71
|
-
error_1 = _a.sent();
|
|
72
|
-
return [3 /*break*/, 4];
|
|
73
|
-
case 4:
|
|
74
|
-
requestHeaders = (0, exports.getAuth0Headers)(token, options);
|
|
75
|
-
return [2 /*return*/, react_admin_1.fetchUtils.fetchJson(url, __assign(__assign({}, options), { headers: requestHeaders }))];
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}); }; };
|
|
79
|
-
exports.httpClient = httpClient;
|
|
80
|
-
/**
|
|
81
|
-
* Return the headers needed by Auth0.
|
|
82
|
-
* @param token the Auth0 token
|
|
83
|
-
* @param options the fetch options (so that we do not override other headers)
|
|
84
|
-
* @returns the headers needed by Auth0
|
|
85
|
-
*/
|
|
86
|
-
var getAuth0Headers = function (token, options) {
|
|
87
|
-
var headers = ((options && options.headers) ||
|
|
88
|
-
new Headers({
|
|
89
|
-
Accept: 'application/json',
|
|
90
|
-
}));
|
|
91
|
-
if (token) {
|
|
92
|
-
headers.set('Authorization', "Bearer ".concat(token));
|
|
93
|
-
}
|
|
94
|
-
return headers;
|
|
95
|
-
};
|
|
96
|
-
exports.getAuth0Headers = getAuth0Headers;
|
package/lib/index.d.ts
DELETED
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|