ra-auth-auth0 1.0.0 → 1.1.1

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/Readme.md CHANGED
@@ -20,6 +20,7 @@ npm install --save ra-auth-auth0
20
20
  // in src/App.tsx
21
21
  import React, { useEffect, useRef, useState } from 'react';
22
22
  import { Admin, Resource } from 'react-admin';
23
+ import { BrowserRouter } from 'react-router-dom';
23
24
  import { Auth0AuthProvider } from 'ra-auth-auth0';
24
25
  import { Auth0Client } from '@auth0/auth0-spa-js';
25
26
  import dataProvider from './dataProvider';
@@ -41,17 +42,21 @@ const authProvider = Auth0AuthProvider(auth0, {
41
42
 
42
43
  const App = () => {
43
44
  return (
44
- <Admin
45
- authProvider={authProvider}
46
- dataProvider={dataProvider}
47
- >
48
- <Resource name="posts" {...posts} />
49
- </Admin>
45
+ <BrowserRouter>
46
+ <Admin
47
+ authProvider={authProvider}
48
+ dataProvider={dataProvider}
49
+ >
50
+ <Resource name="posts" {...posts} />
51
+ </Admin>
52
+ </BrowserRouter>
50
53
  );
51
54
  };
52
55
  export default App;
53
56
  ```
54
57
 
58
+ **Note:** You need to wrap your app in a [`BrowserRouter`](https://reactrouter.com/en/6/router-components/browser-router) component from `react-router-dom` for the `Auth0AuthProvider` to work.
59
+
55
60
  ## `Auth0AuthProvider` Parameters
56
61
 
57
62
  - `loginRedirectUri` - _optional_ - URI used to override the redirect URI after successful login. Defaults to react-admin `/auth-callback` route.
@@ -1,4 +1,4 @@
1
- import { AuthProvider } from 'react-admin';
1
+ import { AuthProvider } from 'ra-core';
2
2
  import { Auth0Client } from '@auth0/auth0-spa-js';
3
3
  /**
4
4
  * An authProvider which handles authentication via the Auth0 instance.
@@ -7,6 +7,7 @@ import { Auth0Client } from '@auth0/auth0-spa-js';
7
7
  * ```tsx
8
8
  * import React from 'react';
9
9
  * import { Admin, Resource } from 'react-admin';
10
+ * import { BrowserRouter } from 'react-router-dom';
10
11
  * import { Auth0AuthProvider } from 'ra-auth-auth0';
11
12
  * import { Auth0Client } from '@auth0/auth0-spa-js';
12
13
  * import dataProvider from './dataProvider';
@@ -24,18 +25,19 @@ import { Auth0Client } from '@auth0/auth0-spa-js';
24
25
  *
25
26
  * const authProvider = Auth0AuthProvider(clientAuth0, {
26
27
  * loginRedirectUri: 'http://localhost:3000/auth-callback',
27
- * logoutRedirectUri: 'http://localhost:3000',
28
28
  * });
29
29
  *
30
30
  * const App = () => {
31
31
  * return (
32
+ * <BrowserRouter>
32
33
  * <Admin
33
- * authProvider={authProvider}
34
- * dataProvider={dataProvider}
35
- * title="Example Admin"
36
- * >
37
- * <Resource name="posts" {...posts} />
38
- * </Admin>
34
+ * authProvider={authProvider}
35
+ * dataProvider={dataProvider}
36
+ * title="Example Admin"
37
+ * >
38
+ * <Resource name="posts" {...posts} />
39
+ * </Admin>
40
+ * <BrowserRouter>
39
41
  * );
40
42
  * };
41
43
  * export default App;
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,eAAO,MAAM,iBAAiB,WAClB,WAAW;uBAMI,MAAM;wBACL,MAAM;0BACJ,OAAO;MAIlC,YAmFD,CAAC"}
1
+ {"version":3,"file":"authProvider.d.ts","sourceRoot":"","sources":["../src/authProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA8B,MAAM,SAAS,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,eAAO,MAAM,iBAAiB,WAClB,WAAW;uBAMI,MAAM;wBACL,MAAM;0BACJ,OAAO;MAIlC,YAoFD,CAAC"}
@@ -34,7 +34,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- import { PreviousLocationStorageKey } from 'react-admin';
37
+ import { PreviousLocationStorageKey } from 'ra-core';
38
38
  /**
39
39
  * An authProvider which handles authentication via the Auth0 instance.
40
40
  *
@@ -42,6 +42,7 @@ import { PreviousLocationStorageKey } from 'react-admin';
42
42
  * ```tsx
43
43
  * import React from 'react';
44
44
  * import { Admin, Resource } from 'react-admin';
45
+ * import { BrowserRouter } from 'react-router-dom';
45
46
  * import { Auth0AuthProvider } from 'ra-auth-auth0';
46
47
  * import { Auth0Client } from '@auth0/auth0-spa-js';
47
48
  * import dataProvider from './dataProvider';
@@ -59,18 +60,19 @@ import { PreviousLocationStorageKey } from 'react-admin';
59
60
  *
60
61
  * const authProvider = Auth0AuthProvider(clientAuth0, {
61
62
  * loginRedirectUri: 'http://localhost:3000/auth-callback',
62
- * logoutRedirectUri: 'http://localhost:3000',
63
63
  * });
64
64
  *
65
65
  * const App = () => {
66
66
  * return (
67
+ * <BrowserRouter>
67
68
  * <Admin
68
- * authProvider={authProvider}
69
- * dataProvider={dataProvider}
70
- * title="Example Admin"
71
- * >
72
- * <Resource name="posts" {...posts} />
73
- * </Admin>
69
+ * authProvider={authProvider}
70
+ * dataProvider={dataProvider}
71
+ * title="Example Admin"
72
+ * >
73
+ * <Resource name="posts" {...posts} />
74
+ * </Admin>
75
+ * <BrowserRouter>
74
76
  * );
75
77
  * };
76
78
  * export default App;
@@ -106,11 +108,16 @@ export var Auth0AuthProvider = function (client, _a) {
106
108
  logout: function () {
107
109
  return __awaiter(this, void 0, void 0, function () {
108
110
  return __generator(this, function (_a) {
109
- return [2 /*return*/, client.logout({
110
- logoutParams: {
111
- returnTo: logoutRedirectUri || window.location.origin,
112
- },
113
- })];
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
+ }
114
121
  });
115
122
  });
116
123
  },
@@ -140,13 +147,13 @@ export var Auth0AuthProvider = function (client, _a) {
140
147
  }
141
148
  if (redirectOnCheckAuth) {
142
149
  localStorage.setItem(PreviousLocationStorageKey, window.location.href);
143
- client.loginWithRedirect({
144
- authorizationParams: {
145
- redirect_uri: loginRedirectUri !== null && loginRedirectUri !== void 0 ? loginRedirectUri : "".concat(window.location.origin, "/auth-callback"),
146
- },
147
- });
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
+ })];
148
155
  }
149
- return [2 /*return*/];
156
+ throw new Error('Unauthorized');
150
157
  }
151
158
  });
152
159
  });
@@ -211,8 +218,7 @@ export var Auth0AuthProvider = function (client, _a) {
211
218
  return [2 /*return*/];
212
219
  case 3:
213
220
  error_1 = _a.sent();
214
- console.log('error', error_1);
215
- throw error_1;
221
+ throw { redirectTo: false, message: error_1.message };
216
222
  case 4: throw new Error('Failed to handle login callback.');
217
223
  }
218
224
  });
@@ -1,4 +1,4 @@
1
- import { fetchUtils } from 'react-admin';
1
+ import { fetchUtils } from 'ra-core';
2
2
  /**
3
3
  * An httpClient that adds authentication headers needed by Auth0 in all requests.
4
4
  * @param auth0Client the Auth0 client.
@@ -1 +1 @@
1
- {"version":3,"file":"httpClient.d.ts","sourceRoot":"","sources":["../src/httpClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC;;;;GAIG;AACH,eAAO,MAAM,UAAU,gBAAiB,GAAG,WAClC,GAAG,WACC,WAAW,OAAO,GAAG,SAAS;;;;;EAQ1C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,UACjB,MAAM,GAAG,IAAI,WACX,WAAW,OAAO,GAAG,SAAS,KACxC,OASF,CAAC"}
1
+ {"version":3,"file":"httpClient.d.ts","sourceRoot":"","sources":["../src/httpClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC;;;;GAIG;AACH,eAAO,MAAM,UAAU,gBAAiB,GAAG,WAClC,GAAG,WACC,WAAW,OAAO,GAAG,SAAS;;;;;EAa1C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,UACjB,MAAM,GAAG,IAAI,WACX,WAAW,OAAO,GAAG,SAAS,KACxC,OASF,CAAC"}
package/esm/httpClient.js CHANGED
@@ -45,19 +45,29 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
45
45
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
46
  }
47
47
  };
48
- import { fetchUtils } from 'react-admin';
48
+ import { fetchUtils } from 'ra-core';
49
49
  /**
50
50
  * An httpClient that adds authentication headers needed by Auth0 in all requests.
51
51
  * @param auth0Client the Auth0 client.
52
52
  * @returns a function with the same definition as `httpClient` that adds an `Authorization` header containing the Auth0 token.
53
53
  */
54
54
  export var httpClient = function (auth0Client) { return function (url, options) { return __awaiter(void 0, void 0, void 0, function () {
55
- var token, requestHeaders;
55
+ var token, error_1, requestHeaders;
56
56
  return __generator(this, function (_a) {
57
57
  switch (_a.label) {
58
- case 0: return [4 /*yield*/, auth0Client.getTokenSilently()];
58
+ case 0:
59
+ token = null;
60
+ _a.label = 1;
59
61
  case 1:
62
+ _a.trys.push([1, 3, , 4]);
63
+ return [4 /*yield*/, auth0Client.getTokenSilently()];
64
+ case 2:
60
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:
61
71
  requestHeaders = getAuth0Headers(token, options);
62
72
  return [2 /*return*/, fetchUtils.fetchJson(url, __assign(__assign({}, options), { headers: requestHeaders }))];
63
73
  }
@@ -1,4 +1,4 @@
1
- import { AuthProvider } from 'react-admin';
1
+ import { AuthProvider } from 'ra-core';
2
2
  import { Auth0Client } from '@auth0/auth0-spa-js';
3
3
  /**
4
4
  * An authProvider which handles authentication via the Auth0 instance.
@@ -7,6 +7,7 @@ import { Auth0Client } from '@auth0/auth0-spa-js';
7
7
  * ```tsx
8
8
  * import React from 'react';
9
9
  * import { Admin, Resource } from 'react-admin';
10
+ * import { BrowserRouter } from 'react-router-dom';
10
11
  * import { Auth0AuthProvider } from 'ra-auth-auth0';
11
12
  * import { Auth0Client } from '@auth0/auth0-spa-js';
12
13
  * import dataProvider from './dataProvider';
@@ -24,18 +25,19 @@ import { Auth0Client } from '@auth0/auth0-spa-js';
24
25
  *
25
26
  * const authProvider = Auth0AuthProvider(clientAuth0, {
26
27
  * loginRedirectUri: 'http://localhost:3000/auth-callback',
27
- * logoutRedirectUri: 'http://localhost:3000',
28
28
  * });
29
29
  *
30
30
  * const App = () => {
31
31
  * return (
32
+ * <BrowserRouter>
32
33
  * <Admin
33
- * authProvider={authProvider}
34
- * dataProvider={dataProvider}
35
- * title="Example Admin"
36
- * >
37
- * <Resource name="posts" {...posts} />
38
- * </Admin>
34
+ * authProvider={authProvider}
35
+ * dataProvider={dataProvider}
36
+ * title="Example Admin"
37
+ * >
38
+ * <Resource name="posts" {...posts} />
39
+ * </Admin>
40
+ * <BrowserRouter>
39
41
  * );
40
42
  * };
41
43
  * export default App;
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,eAAO,MAAM,iBAAiB,WAClB,WAAW;uBAMI,MAAM;wBACL,MAAM;0BACJ,OAAO;MAIlC,YAmFD,CAAC"}
1
+ {"version":3,"file":"authProvider.d.ts","sourceRoot":"","sources":["../src/authProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA8B,MAAM,SAAS,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,eAAO,MAAM,iBAAiB,WAClB,WAAW;uBAMI,MAAM;wBACL,MAAM;0BACJ,OAAO;MAIlC,YAoFD,CAAC"}
@@ -37,7 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.Auth0AuthProvider = void 0;
40
- var react_admin_1 = require("react-admin");
40
+ var ra_core_1 = require("ra-core");
41
41
  /**
42
42
  * An authProvider which handles authentication via the Auth0 instance.
43
43
  *
@@ -45,6 +45,7 @@ var react_admin_1 = require("react-admin");
45
45
  * ```tsx
46
46
  * import React from 'react';
47
47
  * import { Admin, Resource } from 'react-admin';
48
+ * import { BrowserRouter } from 'react-router-dom';
48
49
  * import { Auth0AuthProvider } from 'ra-auth-auth0';
49
50
  * import { Auth0Client } from '@auth0/auth0-spa-js';
50
51
  * import dataProvider from './dataProvider';
@@ -62,18 +63,19 @@ var react_admin_1 = require("react-admin");
62
63
  *
63
64
  * const authProvider = Auth0AuthProvider(clientAuth0, {
64
65
  * loginRedirectUri: 'http://localhost:3000/auth-callback',
65
- * logoutRedirectUri: 'http://localhost:3000',
66
66
  * });
67
67
  *
68
68
  * const App = () => {
69
69
  * return (
70
+ * <BrowserRouter>
70
71
  * <Admin
71
- * authProvider={authProvider}
72
- * dataProvider={dataProvider}
73
- * title="Example Admin"
74
- * >
75
- * <Resource name="posts" {...posts} />
76
- * </Admin>
72
+ * authProvider={authProvider}
73
+ * dataProvider={dataProvider}
74
+ * title="Example Admin"
75
+ * >
76
+ * <Resource name="posts" {...posts} />
77
+ * </Admin>
78
+ * <BrowserRouter>
77
79
  * );
78
80
  * };
79
81
  * export default App;
@@ -109,11 +111,16 @@ var Auth0AuthProvider = function (client, _a) {
109
111
  logout: function () {
110
112
  return __awaiter(this, void 0, void 0, function () {
111
113
  return __generator(this, function (_a) {
112
- return [2 /*return*/, client.logout({
113
- logoutParams: {
114
- returnTo: logoutRedirectUri || window.location.origin,
115
- },
116
- })];
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
+ }
117
124
  });
118
125
  });
119
126
  },
@@ -142,14 +149,14 @@ var Auth0AuthProvider = function (client, _a) {
142
149
  return [2 /*return*/];
143
150
  }
144
151
  if (redirectOnCheckAuth) {
145
- localStorage.setItem(react_admin_1.PreviousLocationStorageKey, window.location.href);
146
- client.loginWithRedirect({
147
- authorizationParams: {
148
- redirect_uri: loginRedirectUri !== null && loginRedirectUri !== void 0 ? loginRedirectUri : "".concat(window.location.origin, "/auth-callback"),
149
- },
150
- });
152
+ localStorage.setItem(ra_core_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
+ })];
151
158
  }
152
- return [2 /*return*/];
159
+ throw new Error('Unauthorized');
153
160
  }
154
161
  });
155
162
  });
@@ -214,8 +221,7 @@ var Auth0AuthProvider = function (client, _a) {
214
221
  return [2 /*return*/];
215
222
  case 3:
216
223
  error_1 = _a.sent();
217
- console.log('error', error_1);
218
- throw error_1;
224
+ throw { redirectTo: false, message: error_1.message };
219
225
  case 4: throw new Error('Failed to handle login callback.');
220
226
  }
221
227
  });
@@ -1,4 +1,4 @@
1
- import { fetchUtils } from 'react-admin';
1
+ import { fetchUtils } from 'ra-core';
2
2
  /**
3
3
  * An httpClient that adds authentication headers needed by Auth0 in all requests.
4
4
  * @param auth0Client the Auth0 client.
@@ -1 +1 @@
1
- {"version":3,"file":"httpClient.d.ts","sourceRoot":"","sources":["../src/httpClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC;;;;GAIG;AACH,eAAO,MAAM,UAAU,gBAAiB,GAAG,WAClC,GAAG,WACC,WAAW,OAAO,GAAG,SAAS;;;;;EAQ1C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,UACjB,MAAM,GAAG,IAAI,WACX,WAAW,OAAO,GAAG,SAAS,KACxC,OASF,CAAC"}
1
+ {"version":3,"file":"httpClient.d.ts","sourceRoot":"","sources":["../src/httpClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC;;;;GAIG;AACH,eAAO,MAAM,UAAU,gBAAiB,GAAG,WAClC,GAAG,WACC,WAAW,OAAO,GAAG,SAAS;;;;;EAa1C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,UACjB,MAAM,GAAG,IAAI,WACX,WAAW,OAAO,GAAG,SAAS,KACxC,OASF,CAAC"}
package/lib/httpClient.js CHANGED
@@ -48,21 +48,31 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
48
48
  };
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.getAuth0Headers = exports.httpClient = void 0;
51
- var react_admin_1 = require("react-admin");
51
+ var ra_core_1 = require("ra-core");
52
52
  /**
53
53
  * An httpClient that adds authentication headers needed by Auth0 in all requests.
54
54
  * @param auth0Client the Auth0 client.
55
55
  * @returns a function with the same definition as `httpClient` that adds an `Authorization` header containing the Auth0 token.
56
56
  */
57
57
  var httpClient = function (auth0Client) { return function (url, options) { return __awaiter(void 0, void 0, void 0, function () {
58
- var token, requestHeaders;
58
+ var token, error_1, requestHeaders;
59
59
  return __generator(this, function (_a) {
60
60
  switch (_a.label) {
61
- case 0: return [4 /*yield*/, auth0Client.getTokenSilently()];
61
+ case 0:
62
+ token = null;
63
+ _a.label = 1;
62
64
  case 1:
65
+ _a.trys.push([1, 3, , 4]);
66
+ return [4 /*yield*/, auth0Client.getTokenSilently()];
67
+ case 2:
63
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:
64
74
  requestHeaders = (0, exports.getAuth0Headers)(token, options);
65
- return [2 /*return*/, react_admin_1.fetchUtils.fetchJson(url, __assign(__assign({}, options), { headers: requestHeaders }))];
75
+ return [2 /*return*/, ra_core_1.fetchUtils.fetchJson(url, __assign(__assign({}, options), { headers: requestHeaders }))];
66
76
  }
67
77
  });
68
78
  }); }; };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ra-auth-auth0",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
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",
@@ -16,13 +16,10 @@
16
16
  "sideEffects": false,
17
17
  "peerDependencies": {
18
18
  "@auth0/auth0-spa-js": "^2.0.0",
19
- "ra-core": "^4.7.0",
19
+ "ra-core": "^4.10.0",
20
20
  "react": "^16.9.0 || ^17.0.0 || ^18.0.0",
21
21
  "react-dom": "^16.9.0 || ^17.0.0 || ^18.0.0"
22
22
  },
23
- "dependencies": {
24
- "react-admin": "^4.7.0"
25
- },
26
23
  "scripts": {
27
24
  "build": "yarn run build-cjs && yarn run build-esm",
28
25
  "build-cjs": "rimraf ./lib && tsc",
@@ -1,4 +1,4 @@
1
- import { AuthProvider, PreviousLocationStorageKey } from 'react-admin';
1
+ import { AuthProvider, PreviousLocationStorageKey } from 'ra-core';
2
2
  import { Auth0Client } from '@auth0/auth0-spa-js';
3
3
 
4
4
  /**
@@ -8,6 +8,7 @@ import { Auth0Client } from '@auth0/auth0-spa-js';
8
8
  * ```tsx
9
9
  * import React from 'react';
10
10
  * import { Admin, Resource } from 'react-admin';
11
+ * import { BrowserRouter } from 'react-router-dom';
11
12
  * import { Auth0AuthProvider } from 'ra-auth-auth0';
12
13
  * import { Auth0Client } from '@auth0/auth0-spa-js';
13
14
  * import dataProvider from './dataProvider';
@@ -25,18 +26,19 @@ import { Auth0Client } from '@auth0/auth0-spa-js';
25
26
  *
26
27
  * const authProvider = Auth0AuthProvider(clientAuth0, {
27
28
  * loginRedirectUri: 'http://localhost:3000/auth-callback',
28
- * logoutRedirectUri: 'http://localhost:3000',
29
29
  * });
30
30
  *
31
31
  * const App = () => {
32
32
  * return (
33
+ * <BrowserRouter>
33
34
  * <Admin
34
- * authProvider={authProvider}
35
- * dataProvider={dataProvider}
36
- * title="Example Admin"
37
- * >
38
- * <Resource name="posts" {...posts} />
39
- * </Admin>
35
+ * authProvider={authProvider}
36
+ * dataProvider={dataProvider}
37
+ * title="Example Admin"
38
+ * >
39
+ * <Resource name="posts" {...posts} />
40
+ * </Admin>
41
+ * <BrowserRouter>
40
42
  * );
41
43
  * };
42
44
  * export default App;
@@ -76,11 +78,12 @@ export const Auth0AuthProvider = (
76
78
  },
77
79
  // called when the user clicks on the logout button
78
80
  async logout() {
79
- return client.logout({
81
+ await client.logout({
80
82
  logoutParams: {
81
83
  returnTo: logoutRedirectUri || window.location.origin,
82
84
  },
83
85
  });
86
+ return false;
84
87
  },
85
88
  // called when the API returns an error
86
89
  async checkError({ status }) {
@@ -100,7 +103,7 @@ export const Auth0AuthProvider = (
100
103
  PreviousLocationStorageKey,
101
104
  window.location.href
102
105
  );
103
- client.loginWithRedirect({
106
+ return client.loginWithRedirect({
104
107
  authorizationParams: {
105
108
  redirect_uri:
106
109
  loginRedirectUri ??
@@ -108,6 +111,7 @@ export const Auth0AuthProvider = (
108
111
  },
109
112
  });
110
113
  }
114
+ throw new Error('Unauthorized');
111
115
  },
112
116
  // called when the user navigates to a new location, to check for permissions / roles
113
117
  async getPermissions() {
@@ -140,8 +144,7 @@ export const Auth0AuthProvider = (
140
144
  await client.handleRedirectCallback();
141
145
  return;
142
146
  } catch (error) {
143
- console.log('error', error);
144
- throw error;
147
+ throw { redirectTo: false, message: error.message };
145
148
  }
146
149
  }
147
150
  throw new Error('Failed to handle login callback.');
package/src/httpClient.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { fetchUtils } from 'react-admin';
1
+ import { fetchUtils } from 'ra-core';
2
2
 
3
3
  /**
4
4
  * An httpClient that adds authentication headers needed by Auth0 in all requests.
@@ -9,7 +9,12 @@ export const httpClient = (auth0Client: any) => async (
9
9
  url: any,
10
10
  options: fetchUtils.Options | undefined
11
11
  ) => {
12
- const token = await auth0Client.getTokenSilently();
12
+ let token: string | null = null;
13
+ try {
14
+ token = await auth0Client.getTokenSilently();
15
+ } catch (error) {
16
+ // Nothing to do, we just won't set the Authorization header
17
+ }
13
18
  const requestHeaders = getAuth0Headers(token, options);
14
19
  return fetchUtils.fetchJson(url, {
15
20
  ...options,