@ttoss/react-auth-strapi 0.2.43 → 0.2.45
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/package.json +9 -9
- package/dist/esm/index.js +0 -293
- package/dist/index.d.cts +0 -20
- package/dist/index.d.ts +0 -20
- package/dist/index.js +0 -342
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/react-auth-strapi",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.45",
|
|
4
4
|
"description": "Authentication components and abstractions for React apps using Strapi.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ttoss",
|
|
@@ -26,19 +26,19 @@
|
|
|
26
26
|
"sideEffects": false,
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"react": ">=16.8.0",
|
|
29
|
-
"@ttoss/react-
|
|
30
|
-
"@ttoss/react-notifications": "^2.4.
|
|
31
|
-
"@ttoss/react-
|
|
29
|
+
"@ttoss/react-auth-core": "^0.2.45",
|
|
30
|
+
"@ttoss/react-notifications": "^2.4.39",
|
|
31
|
+
"@ttoss/react-i18n": "^2.0.25"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@types/react": "^19.2.
|
|
34
|
+
"@types/react": "^19.2.7",
|
|
35
35
|
"jest": "^30.2.0",
|
|
36
|
-
"react": "^19.2.
|
|
36
|
+
"react": "^19.2.1",
|
|
37
37
|
"tsup": "^8.5.1",
|
|
38
38
|
"@ttoss/i18n-cli": "^0.7.38",
|
|
39
|
-
"@ttoss/react-
|
|
40
|
-
"@ttoss/react-
|
|
41
|
-
"@ttoss/react-
|
|
39
|
+
"@ttoss/react-auth-core": "^0.2.45",
|
|
40
|
+
"@ttoss/react-i18n": "^2.0.25",
|
|
41
|
+
"@ttoss/react-notifications": "^2.4.39"
|
|
42
42
|
},
|
|
43
43
|
"keywords": [
|
|
44
44
|
"React",
|
package/dist/esm/index.js
DELETED
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __name = (target, value) => __defProp(target, "name", {
|
|
4
|
-
value,
|
|
5
|
-
configurable: true
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
// src/Auth.tsx
|
|
9
|
-
import { Auth as AuthCore, useAuthScreen } from "@ttoss/react-auth-core";
|
|
10
|
-
import { useNotifications } from "@ttoss/react-notifications";
|
|
11
|
-
import * as React2 from "react";
|
|
12
|
-
|
|
13
|
-
// src/AuthProvider.tsx
|
|
14
|
-
import { AuthProvider as AuthProviderCore, useAuth as useAuthCore } from "@ttoss/react-auth-core";
|
|
15
|
-
import * as React from "react";
|
|
16
|
-
|
|
17
|
-
// src/storage.ts
|
|
18
|
-
var AUTH_STORAGE_REFRESH_TOKEN_KEY = "ttoss-strapi-auth-refresh-token";
|
|
19
|
-
var isBrowser = typeof window !== "undefined";
|
|
20
|
-
var getLocalStorage = /* @__PURE__ */__name(() => {
|
|
21
|
-
try {
|
|
22
|
-
return isBrowser ? window.localStorage : null;
|
|
23
|
-
} catch {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
}, "getLocalStorage");
|
|
27
|
-
var storage = {
|
|
28
|
-
getRefreshToken: /* @__PURE__ */__name(() => {
|
|
29
|
-
const ls = getLocalStorage();
|
|
30
|
-
if (!ls) return null;
|
|
31
|
-
try {
|
|
32
|
-
return ls.getItem(AUTH_STORAGE_REFRESH_TOKEN_KEY);
|
|
33
|
-
} catch {
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
}, "getRefreshToken"),
|
|
37
|
-
setRefreshToken: /* @__PURE__ */__name(refreshToken => {
|
|
38
|
-
const ls = getLocalStorage();
|
|
39
|
-
if (!ls) return;
|
|
40
|
-
try {
|
|
41
|
-
ls.setItem(AUTH_STORAGE_REFRESH_TOKEN_KEY, refreshToken);
|
|
42
|
-
} catch {}
|
|
43
|
-
}, "setRefreshToken"),
|
|
44
|
-
clearRefreshToken: /* @__PURE__ */__name(() => {
|
|
45
|
-
const ls = getLocalStorage();
|
|
46
|
-
if (!ls) return;
|
|
47
|
-
try {
|
|
48
|
-
ls.removeItem(AUTH_STORAGE_REFRESH_TOKEN_KEY);
|
|
49
|
-
} catch {}
|
|
50
|
-
}, "clearRefreshToken")
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/AuthProvider.tsx
|
|
54
|
-
var AuthContext = /* @__PURE__ */React.createContext({
|
|
55
|
-
apiUrl: ""
|
|
56
|
-
});
|
|
57
|
-
var AuthProvider = /* @__PURE__ */__name(props => {
|
|
58
|
-
const getAuthData = React.useCallback(async () => {
|
|
59
|
-
try {
|
|
60
|
-
const refreshToken = storage.getRefreshToken();
|
|
61
|
-
if (!refreshToken) {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
const refreshResponse = await fetch(`${props.apiUrl}/auth/local/refresh`, {
|
|
65
|
-
method: "POST",
|
|
66
|
-
headers: {
|
|
67
|
-
"Content-Type": "application/json"
|
|
68
|
-
},
|
|
69
|
-
body: JSON.stringify({
|
|
70
|
-
refreshToken
|
|
71
|
-
})
|
|
72
|
-
});
|
|
73
|
-
const refreshData = await refreshResponse.json();
|
|
74
|
-
if ("error" in refreshData) {
|
|
75
|
-
storage.clearRefreshToken();
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
|
-
const meResponse = await fetch(`${props.apiUrl}/users/me`, {
|
|
79
|
-
headers: {
|
|
80
|
-
Authorization: `Bearer ${refreshData.jwt}`
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
const data = await meResponse.json();
|
|
84
|
-
if (!meResponse.ok) {
|
|
85
|
-
storage.clearRefreshToken();
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
storage.setRefreshToken(refreshData.refreshToken);
|
|
89
|
-
return {
|
|
90
|
-
user: {
|
|
91
|
-
id: data.id,
|
|
92
|
-
email: data.email,
|
|
93
|
-
emailVerified: data.confirmed ?? data.emailVerified ?? data.user?.confirmed
|
|
94
|
-
},
|
|
95
|
-
tokens: {
|
|
96
|
-
accessToken: refreshData.jwt,
|
|
97
|
-
refreshToken: refreshData.refreshToken
|
|
98
|
-
},
|
|
99
|
-
isAuthenticated: true
|
|
100
|
-
};
|
|
101
|
-
} catch {
|
|
102
|
-
storage.clearRefreshToken();
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
}, [props.apiUrl]);
|
|
106
|
-
const signOut = React.useCallback(async () => {
|
|
107
|
-
storage.clearRefreshToken();
|
|
108
|
-
}, []);
|
|
109
|
-
return /* @__PURE__ */React.createElement(AuthContext.Provider, {
|
|
110
|
-
value: {
|
|
111
|
-
apiUrl: props.apiUrl
|
|
112
|
-
}
|
|
113
|
-
}, /* @__PURE__ */React.createElement(AuthProviderCore, {
|
|
114
|
-
getAuthData,
|
|
115
|
-
signOut
|
|
116
|
-
}, props.children));
|
|
117
|
-
}, "AuthProvider");
|
|
118
|
-
var useAuth = /* @__PURE__ */__name(() => {
|
|
119
|
-
const authCore = useAuthCore();
|
|
120
|
-
const {
|
|
121
|
-
apiUrl
|
|
122
|
-
} = React.useContext(AuthContext);
|
|
123
|
-
return {
|
|
124
|
-
apiUrl,
|
|
125
|
-
...authCore
|
|
126
|
-
};
|
|
127
|
-
}, "useAuth");
|
|
128
|
-
|
|
129
|
-
// src/Auth.tsx
|
|
130
|
-
var Auth = /* @__PURE__ */__name(props => {
|
|
131
|
-
const {
|
|
132
|
-
setAuthData,
|
|
133
|
-
apiUrl
|
|
134
|
-
} = useAuth();
|
|
135
|
-
const {
|
|
136
|
-
screen,
|
|
137
|
-
setScreen
|
|
138
|
-
} = useAuthScreen();
|
|
139
|
-
const {
|
|
140
|
-
addNotification
|
|
141
|
-
} = useNotifications();
|
|
142
|
-
const onSignIn = React2.useCallback(async ({
|
|
143
|
-
email,
|
|
144
|
-
password
|
|
145
|
-
}) => {
|
|
146
|
-
try {
|
|
147
|
-
const response = await fetch(`${apiUrl}/auth/local`, {
|
|
148
|
-
method: "POST",
|
|
149
|
-
headers: {
|
|
150
|
-
"Content-Type": "application/json"
|
|
151
|
-
},
|
|
152
|
-
body: JSON.stringify({
|
|
153
|
-
identifier: email,
|
|
154
|
-
password
|
|
155
|
-
})
|
|
156
|
-
});
|
|
157
|
-
const data = await response.json();
|
|
158
|
-
if (!response.ok) {
|
|
159
|
-
const errorMessage = data.error?.message;
|
|
160
|
-
if (errorMessage === "Your account email is not confirmed") {
|
|
161
|
-
const resendResponse = await fetch(`${apiUrl}/auth/send-email-confirmation`, {
|
|
162
|
-
method: "POST",
|
|
163
|
-
headers: {
|
|
164
|
-
"Content-Type": "application/json"
|
|
165
|
-
},
|
|
166
|
-
body: JSON.stringify({
|
|
167
|
-
email
|
|
168
|
-
})
|
|
169
|
-
});
|
|
170
|
-
if (!resendResponse.ok) {
|
|
171
|
-
const resendData = await resendResponse.json();
|
|
172
|
-
addNotification({
|
|
173
|
-
title: "Resend confirmation email failed",
|
|
174
|
-
message: resendData.error?.message || "An error occurred while resending the confirmation email.",
|
|
175
|
-
type: "error"
|
|
176
|
-
});
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
setScreen({
|
|
180
|
-
value: "confirmSignUpCheckEmail"
|
|
181
|
-
});
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
addNotification({
|
|
185
|
-
title: "Sign in failed",
|
|
186
|
-
message: data.error?.message || "An error occurred during sign in.",
|
|
187
|
-
type: "error"
|
|
188
|
-
});
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
storage.setRefreshToken(data.refreshToken);
|
|
192
|
-
setAuthData({
|
|
193
|
-
user: {
|
|
194
|
-
id: data.user.id,
|
|
195
|
-
email: data.user.email,
|
|
196
|
-
emailVerified: data.user.confirmed
|
|
197
|
-
},
|
|
198
|
-
tokens: {
|
|
199
|
-
accessToken: data.jwt,
|
|
200
|
-
refreshToken: data.refreshToken
|
|
201
|
-
},
|
|
202
|
-
isAuthenticated: true
|
|
203
|
-
});
|
|
204
|
-
} catch {
|
|
205
|
-
addNotification({
|
|
206
|
-
title: "Network Error",
|
|
207
|
-
message: "Unable to connect to the server. Please check your connection.",
|
|
208
|
-
type: "error"
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
}, [setAuthData, setScreen, addNotification, apiUrl]);
|
|
212
|
-
const onSignUp = React2.useCallback(async ({
|
|
213
|
-
email,
|
|
214
|
-
password
|
|
215
|
-
}) => {
|
|
216
|
-
try {
|
|
217
|
-
const response = await fetch(`${apiUrl}/auth/local/register`, {
|
|
218
|
-
method: "POST",
|
|
219
|
-
headers: {
|
|
220
|
-
"Content-Type": "application/json"
|
|
221
|
-
},
|
|
222
|
-
body: JSON.stringify({
|
|
223
|
-
username: email,
|
|
224
|
-
email,
|
|
225
|
-
password
|
|
226
|
-
})
|
|
227
|
-
});
|
|
228
|
-
const data = await response.json();
|
|
229
|
-
if (!response.ok) {
|
|
230
|
-
addNotification({
|
|
231
|
-
title: "Sign up failed",
|
|
232
|
-
message: data.error?.message || "An error occurred during sign up.",
|
|
233
|
-
type: "error"
|
|
234
|
-
});
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
setScreen({
|
|
238
|
-
value: "confirmSignUpCheckEmail"
|
|
239
|
-
});
|
|
240
|
-
} catch {
|
|
241
|
-
addNotification({
|
|
242
|
-
title: "Network Error",
|
|
243
|
-
message: "Unable to connect to the server. Please check your connection.",
|
|
244
|
-
type: "error"
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
}, [addNotification, setScreen, apiUrl]);
|
|
248
|
-
const onForgotPassword = React2.useCallback(async ({
|
|
249
|
-
email
|
|
250
|
-
}) => {
|
|
251
|
-
try {
|
|
252
|
-
const response = await fetch(`${apiUrl}/auth/forgot-password`, {
|
|
253
|
-
method: "POST",
|
|
254
|
-
headers: {
|
|
255
|
-
"Content-Type": "application/json"
|
|
256
|
-
},
|
|
257
|
-
body: JSON.stringify({
|
|
258
|
-
email
|
|
259
|
-
})
|
|
260
|
-
});
|
|
261
|
-
const data = await response.json();
|
|
262
|
-
if (!response.ok) {
|
|
263
|
-
addNotification({
|
|
264
|
-
title: "Forgot password failed",
|
|
265
|
-
message: data.error?.message || "An error occurred during forgot password.",
|
|
266
|
-
type: "error"
|
|
267
|
-
});
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
} catch {
|
|
271
|
-
addNotification({
|
|
272
|
-
title: "Network Error",
|
|
273
|
-
message: "Unable to connect to the server. Please check your connection.",
|
|
274
|
-
type: "error"
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
}, [addNotification, apiUrl]);
|
|
278
|
-
const onConfirmSignUpCheckEmail = React2.useCallback(async () => {
|
|
279
|
-
setScreen({
|
|
280
|
-
value: "signIn"
|
|
281
|
-
});
|
|
282
|
-
}, [setScreen]);
|
|
283
|
-
return /* @__PURE__ */React2.createElement(AuthCore, {
|
|
284
|
-
...props,
|
|
285
|
-
screen,
|
|
286
|
-
setScreen,
|
|
287
|
-
onSignIn,
|
|
288
|
-
onSignUp,
|
|
289
|
-
onForgotPassword,
|
|
290
|
-
onConfirmSignUpCheckEmail
|
|
291
|
-
});
|
|
292
|
-
}, "Auth");
|
|
293
|
-
export { Auth, AuthProvider, useAuth };
|
package/dist/index.d.cts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import * as _ttoss_react_auth_core from '@ttoss/react-auth-core';
|
|
3
|
-
import { AuthProps } from '@ttoss/react-auth-core';
|
|
4
|
-
import * as React from 'react';
|
|
5
|
-
|
|
6
|
-
declare const Auth: (props: Pick<AuthProps, "logo" | "layout">) => react_jsx_runtime.JSX.Element;
|
|
7
|
-
|
|
8
|
-
declare const AuthProvider: (props: React.PropsWithChildren<{
|
|
9
|
-
apiUrl: string;
|
|
10
|
-
}>) => react_jsx_runtime.JSX.Element;
|
|
11
|
-
declare const useAuth: () => {
|
|
12
|
-
signOut: () => Promise<void>;
|
|
13
|
-
isAuthenticated: boolean;
|
|
14
|
-
user: _ttoss_react_auth_core.AuthUser | null;
|
|
15
|
-
tokens: _ttoss_react_auth_core.AuthTokens | null;
|
|
16
|
-
setAuthData: React.Dispatch<React.SetStateAction<_ttoss_react_auth_core.AuthData>>;
|
|
17
|
-
apiUrl: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export { Auth, AuthProvider, useAuth };
|
package/dist/index.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import * as _ttoss_react_auth_core from '@ttoss/react-auth-core';
|
|
3
|
-
import { AuthProps } from '@ttoss/react-auth-core';
|
|
4
|
-
import * as React from 'react';
|
|
5
|
-
|
|
6
|
-
declare const Auth: (props: Pick<AuthProps, "logo" | "layout">) => react_jsx_runtime.JSX.Element;
|
|
7
|
-
|
|
8
|
-
declare const AuthProvider: (props: React.PropsWithChildren<{
|
|
9
|
-
apiUrl: string;
|
|
10
|
-
}>) => react_jsx_runtime.JSX.Element;
|
|
11
|
-
declare const useAuth: () => {
|
|
12
|
-
signOut: () => Promise<void>;
|
|
13
|
-
isAuthenticated: boolean;
|
|
14
|
-
user: _ttoss_react_auth_core.AuthUser | null;
|
|
15
|
-
tokens: _ttoss_react_auth_core.AuthTokens | null;
|
|
16
|
-
setAuthData: React.Dispatch<React.SetStateAction<_ttoss_react_auth_core.AuthData>>;
|
|
17
|
-
apiUrl: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export { Auth, AuthProvider, useAuth };
|
package/dist/index.js
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
-
import * as React from 'react';
|
|
3
|
-
"use strict";
|
|
4
|
-
|
|
5
|
-
var __create = Object.create;
|
|
6
|
-
var __defProp = Object.defineProperty;
|
|
7
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
8
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
9
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
-
var __name = (target, value) => __defProp(target, "name", {
|
|
12
|
-
value,
|
|
13
|
-
configurable: true
|
|
14
|
-
});
|
|
15
|
-
var __export = (target, all) => {
|
|
16
|
-
for (var name in all) __defProp(target, name, {
|
|
17
|
-
get: all[name],
|
|
18
|
-
enumerable: true
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __copyProps = (to, from, except, desc) => {
|
|
22
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
-
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
24
|
-
get: () => from[key],
|
|
25
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
return to;
|
|
29
|
-
};
|
|
30
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
31
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
32
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
33
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
34
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
35
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
36
|
-
value: mod,
|
|
37
|
-
enumerable: true
|
|
38
|
-
}) : target, mod));
|
|
39
|
-
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
40
|
-
value: true
|
|
41
|
-
}), mod);
|
|
42
|
-
|
|
43
|
-
// src/index.ts
|
|
44
|
-
var index_exports = {};
|
|
45
|
-
__export(index_exports, {
|
|
46
|
-
Auth: () => Auth,
|
|
47
|
-
AuthProvider: () => AuthProvider,
|
|
48
|
-
useAuth: () => useAuth
|
|
49
|
-
});
|
|
50
|
-
module.exports = __toCommonJS(index_exports);
|
|
51
|
-
|
|
52
|
-
// src/Auth.tsx
|
|
53
|
-
var import_react_auth_core2 = require("@ttoss/react-auth-core");
|
|
54
|
-
var import_react_notifications = require("@ttoss/react-notifications");
|
|
55
|
-
var React2 = __toESM(require("react"), 1);
|
|
56
|
-
|
|
57
|
-
// src/AuthProvider.tsx
|
|
58
|
-
var import_react_auth_core = require("@ttoss/react-auth-core");
|
|
59
|
-
var React = __toESM(require("react"), 1);
|
|
60
|
-
|
|
61
|
-
// src/storage.ts
|
|
62
|
-
var AUTH_STORAGE_REFRESH_TOKEN_KEY = "ttoss-strapi-auth-refresh-token";
|
|
63
|
-
var isBrowser = typeof window !== "undefined";
|
|
64
|
-
var getLocalStorage = /* @__PURE__ */__name(() => {
|
|
65
|
-
try {
|
|
66
|
-
return isBrowser ? window.localStorage : null;
|
|
67
|
-
} catch {
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
}, "getLocalStorage");
|
|
71
|
-
var storage = {
|
|
72
|
-
getRefreshToken: /* @__PURE__ */__name(() => {
|
|
73
|
-
const ls = getLocalStorage();
|
|
74
|
-
if (!ls) return null;
|
|
75
|
-
try {
|
|
76
|
-
return ls.getItem(AUTH_STORAGE_REFRESH_TOKEN_KEY);
|
|
77
|
-
} catch {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
}, "getRefreshToken"),
|
|
81
|
-
setRefreshToken: /* @__PURE__ */__name(refreshToken => {
|
|
82
|
-
const ls = getLocalStorage();
|
|
83
|
-
if (!ls) return;
|
|
84
|
-
try {
|
|
85
|
-
ls.setItem(AUTH_STORAGE_REFRESH_TOKEN_KEY, refreshToken);
|
|
86
|
-
} catch {}
|
|
87
|
-
}, "setRefreshToken"),
|
|
88
|
-
clearRefreshToken: /* @__PURE__ */__name(() => {
|
|
89
|
-
const ls = getLocalStorage();
|
|
90
|
-
if (!ls) return;
|
|
91
|
-
try {
|
|
92
|
-
ls.removeItem(AUTH_STORAGE_REFRESH_TOKEN_KEY);
|
|
93
|
-
} catch {}
|
|
94
|
-
}, "clearRefreshToken")
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// src/AuthProvider.tsx
|
|
98
|
-
var AuthContext = /* @__PURE__ */React.createContext({
|
|
99
|
-
apiUrl: ""
|
|
100
|
-
});
|
|
101
|
-
var AuthProvider = /* @__PURE__ */__name(props => {
|
|
102
|
-
const getAuthData = React.useCallback(async () => {
|
|
103
|
-
try {
|
|
104
|
-
const refreshToken = storage.getRefreshToken();
|
|
105
|
-
if (!refreshToken) {
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
const refreshResponse = await fetch(`${props.apiUrl}/auth/local/refresh`, {
|
|
109
|
-
method: "POST",
|
|
110
|
-
headers: {
|
|
111
|
-
"Content-Type": "application/json"
|
|
112
|
-
},
|
|
113
|
-
body: JSON.stringify({
|
|
114
|
-
refreshToken
|
|
115
|
-
})
|
|
116
|
-
});
|
|
117
|
-
const refreshData = await refreshResponse.json();
|
|
118
|
-
if ("error" in refreshData) {
|
|
119
|
-
storage.clearRefreshToken();
|
|
120
|
-
return null;
|
|
121
|
-
}
|
|
122
|
-
const meResponse = await fetch(`${props.apiUrl}/users/me`, {
|
|
123
|
-
headers: {
|
|
124
|
-
Authorization: `Bearer ${refreshData.jwt}`
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
const data = await meResponse.json();
|
|
128
|
-
if (!meResponse.ok) {
|
|
129
|
-
storage.clearRefreshToken();
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
storage.setRefreshToken(refreshData.refreshToken);
|
|
133
|
-
return {
|
|
134
|
-
user: {
|
|
135
|
-
id: data.id,
|
|
136
|
-
email: data.email,
|
|
137
|
-
emailVerified: data.confirmed ?? data.emailVerified ?? data.user?.confirmed
|
|
138
|
-
},
|
|
139
|
-
tokens: {
|
|
140
|
-
accessToken: refreshData.jwt,
|
|
141
|
-
refreshToken: refreshData.refreshToken
|
|
142
|
-
},
|
|
143
|
-
isAuthenticated: true
|
|
144
|
-
};
|
|
145
|
-
} catch {
|
|
146
|
-
storage.clearRefreshToken();
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
}, [props.apiUrl]);
|
|
150
|
-
const signOut = React.useCallback(async () => {
|
|
151
|
-
storage.clearRefreshToken();
|
|
152
|
-
}, []);
|
|
153
|
-
return /* @__PURE__ */React.createElement(AuthContext.Provider, {
|
|
154
|
-
value: {
|
|
155
|
-
apiUrl: props.apiUrl
|
|
156
|
-
}
|
|
157
|
-
}, /* @__PURE__ */React.createElement(import_react_auth_core.AuthProvider, {
|
|
158
|
-
getAuthData,
|
|
159
|
-
signOut
|
|
160
|
-
}, props.children));
|
|
161
|
-
}, "AuthProvider");
|
|
162
|
-
var useAuth = /* @__PURE__ */__name(() => {
|
|
163
|
-
const authCore = (0, import_react_auth_core.useAuth)();
|
|
164
|
-
const {
|
|
165
|
-
apiUrl
|
|
166
|
-
} = React.useContext(AuthContext);
|
|
167
|
-
return {
|
|
168
|
-
apiUrl,
|
|
169
|
-
...authCore
|
|
170
|
-
};
|
|
171
|
-
}, "useAuth");
|
|
172
|
-
|
|
173
|
-
// src/Auth.tsx
|
|
174
|
-
var Auth = /* @__PURE__ */__name(props => {
|
|
175
|
-
const {
|
|
176
|
-
setAuthData,
|
|
177
|
-
apiUrl
|
|
178
|
-
} = useAuth();
|
|
179
|
-
const {
|
|
180
|
-
screen,
|
|
181
|
-
setScreen
|
|
182
|
-
} = (0, import_react_auth_core2.useAuthScreen)();
|
|
183
|
-
const {
|
|
184
|
-
addNotification
|
|
185
|
-
} = (0, import_react_notifications.useNotifications)();
|
|
186
|
-
const onSignIn = React2.useCallback(async ({
|
|
187
|
-
email,
|
|
188
|
-
password
|
|
189
|
-
}) => {
|
|
190
|
-
try {
|
|
191
|
-
const response = await fetch(`${apiUrl}/auth/local`, {
|
|
192
|
-
method: "POST",
|
|
193
|
-
headers: {
|
|
194
|
-
"Content-Type": "application/json"
|
|
195
|
-
},
|
|
196
|
-
body: JSON.stringify({
|
|
197
|
-
identifier: email,
|
|
198
|
-
password
|
|
199
|
-
})
|
|
200
|
-
});
|
|
201
|
-
const data = await response.json();
|
|
202
|
-
if (!response.ok) {
|
|
203
|
-
const errorMessage = data.error?.message;
|
|
204
|
-
if (errorMessage === "Your account email is not confirmed") {
|
|
205
|
-
const resendResponse = await fetch(`${apiUrl}/auth/send-email-confirmation`, {
|
|
206
|
-
method: "POST",
|
|
207
|
-
headers: {
|
|
208
|
-
"Content-Type": "application/json"
|
|
209
|
-
},
|
|
210
|
-
body: JSON.stringify({
|
|
211
|
-
email
|
|
212
|
-
})
|
|
213
|
-
});
|
|
214
|
-
if (!resendResponse.ok) {
|
|
215
|
-
const resendData = await resendResponse.json();
|
|
216
|
-
addNotification({
|
|
217
|
-
title: "Resend confirmation email failed",
|
|
218
|
-
message: resendData.error?.message || "An error occurred while resending the confirmation email.",
|
|
219
|
-
type: "error"
|
|
220
|
-
});
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
setScreen({
|
|
224
|
-
value: "confirmSignUpCheckEmail"
|
|
225
|
-
});
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
addNotification({
|
|
229
|
-
title: "Sign in failed",
|
|
230
|
-
message: data.error?.message || "An error occurred during sign in.",
|
|
231
|
-
type: "error"
|
|
232
|
-
});
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
storage.setRefreshToken(data.refreshToken);
|
|
236
|
-
setAuthData({
|
|
237
|
-
user: {
|
|
238
|
-
id: data.user.id,
|
|
239
|
-
email: data.user.email,
|
|
240
|
-
emailVerified: data.user.confirmed
|
|
241
|
-
},
|
|
242
|
-
tokens: {
|
|
243
|
-
accessToken: data.jwt,
|
|
244
|
-
refreshToken: data.refreshToken
|
|
245
|
-
},
|
|
246
|
-
isAuthenticated: true
|
|
247
|
-
});
|
|
248
|
-
} catch {
|
|
249
|
-
addNotification({
|
|
250
|
-
title: "Network Error",
|
|
251
|
-
message: "Unable to connect to the server. Please check your connection.",
|
|
252
|
-
type: "error"
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
}, [setAuthData, setScreen, addNotification, apiUrl]);
|
|
256
|
-
const onSignUp = React2.useCallback(async ({
|
|
257
|
-
email,
|
|
258
|
-
password
|
|
259
|
-
}) => {
|
|
260
|
-
try {
|
|
261
|
-
const response = await fetch(`${apiUrl}/auth/local/register`, {
|
|
262
|
-
method: "POST",
|
|
263
|
-
headers: {
|
|
264
|
-
"Content-Type": "application/json"
|
|
265
|
-
},
|
|
266
|
-
body: JSON.stringify({
|
|
267
|
-
username: email,
|
|
268
|
-
email,
|
|
269
|
-
password
|
|
270
|
-
})
|
|
271
|
-
});
|
|
272
|
-
const data = await response.json();
|
|
273
|
-
if (!response.ok) {
|
|
274
|
-
addNotification({
|
|
275
|
-
title: "Sign up failed",
|
|
276
|
-
message: data.error?.message || "An error occurred during sign up.",
|
|
277
|
-
type: "error"
|
|
278
|
-
});
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
setScreen({
|
|
282
|
-
value: "confirmSignUpCheckEmail"
|
|
283
|
-
});
|
|
284
|
-
} catch {
|
|
285
|
-
addNotification({
|
|
286
|
-
title: "Network Error",
|
|
287
|
-
message: "Unable to connect to the server. Please check your connection.",
|
|
288
|
-
type: "error"
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
}, [addNotification, setScreen, apiUrl]);
|
|
292
|
-
const onForgotPassword = React2.useCallback(async ({
|
|
293
|
-
email
|
|
294
|
-
}) => {
|
|
295
|
-
try {
|
|
296
|
-
const response = await fetch(`${apiUrl}/auth/forgot-password`, {
|
|
297
|
-
method: "POST",
|
|
298
|
-
headers: {
|
|
299
|
-
"Content-Type": "application/json"
|
|
300
|
-
},
|
|
301
|
-
body: JSON.stringify({
|
|
302
|
-
email
|
|
303
|
-
})
|
|
304
|
-
});
|
|
305
|
-
const data = await response.json();
|
|
306
|
-
if (!response.ok) {
|
|
307
|
-
addNotification({
|
|
308
|
-
title: "Forgot password failed",
|
|
309
|
-
message: data.error?.message || "An error occurred during forgot password.",
|
|
310
|
-
type: "error"
|
|
311
|
-
});
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
} catch {
|
|
315
|
-
addNotification({
|
|
316
|
-
title: "Network Error",
|
|
317
|
-
message: "Unable to connect to the server. Please check your connection.",
|
|
318
|
-
type: "error"
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
}, [addNotification, apiUrl]);
|
|
322
|
-
const onConfirmSignUpCheckEmail = React2.useCallback(async () => {
|
|
323
|
-
setScreen({
|
|
324
|
-
value: "signIn"
|
|
325
|
-
});
|
|
326
|
-
}, [setScreen]);
|
|
327
|
-
return /* @__PURE__ */React2.createElement(import_react_auth_core2.Auth, {
|
|
328
|
-
...props,
|
|
329
|
-
screen,
|
|
330
|
-
setScreen,
|
|
331
|
-
onSignIn,
|
|
332
|
-
onSignUp,
|
|
333
|
-
onForgotPassword,
|
|
334
|
-
onConfirmSignUpCheckEmail
|
|
335
|
-
});
|
|
336
|
-
}, "Auth");
|
|
337
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
338
|
-
0 && (module.exports = {
|
|
339
|
-
Auth,
|
|
340
|
-
AuthProvider,
|
|
341
|
-
useAuth
|
|
342
|
-
});
|