@ttoss/react-auth-strapi 0.2.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/index.js ADDED
@@ -0,0 +1,340 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ "use strict";
3
+
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __export = (target, all) => {
11
+ for (var name in all) __defProp(target, name, {
12
+ get: all[name],
13
+ enumerable: true
14
+ });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
19
+ get: () => from[key],
20
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
21
+ });
22
+ }
23
+ return to;
24
+ };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
+ // If the importer is in node compatibility mode or this is not an ESM
27
+ // file that has been converted to a CommonJS file using a Babel-
28
+ // compatible transform (i.e. "__esModule" has not been set), then set
29
+ // "default" to the CommonJS "module.exports" for node compatibility.
30
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
31
+ value: mod,
32
+ enumerable: true
33
+ }) : target, mod));
34
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
35
+ value: true
36
+ }), mod);
37
+
38
+ // src/index.ts
39
+ var index_exports = {};
40
+ __export(index_exports, {
41
+ Auth: () => Auth,
42
+ AuthProvider: () => AuthProvider,
43
+ useAuth: () => useAuth
44
+ });
45
+ module.exports = __toCommonJS(index_exports);
46
+
47
+ // src/Auth.tsx
48
+ var import_react_auth_core2 = require("@ttoss/react-auth-core");
49
+ var import_react_notifications = require("@ttoss/react-notifications");
50
+ var React2 = __toESM(require("react"), 1);
51
+
52
+ // src/AuthProvider.tsx
53
+ var import_react_auth_core = require("@ttoss/react-auth-core");
54
+ var React = __toESM(require("react"), 1);
55
+
56
+ // src/storage.ts
57
+ var AUTH_STORAGE_REFRESH_TOKEN_KEY = "ttoss-strapi-auth-refresh-token";
58
+ var isBrowser = typeof window !== "undefined";
59
+ var getLocalStorage = () => {
60
+ try {
61
+ return isBrowser ? window.localStorage : null;
62
+ } catch {
63
+ return null;
64
+ }
65
+ };
66
+ var storage = {
67
+ getRefreshToken: () => {
68
+ const ls = getLocalStorage();
69
+ if (!ls) return null;
70
+ try {
71
+ return ls.getItem(AUTH_STORAGE_REFRESH_TOKEN_KEY);
72
+ } catch {
73
+ return null;
74
+ }
75
+ },
76
+ setRefreshToken: refreshToken => {
77
+ const ls = getLocalStorage();
78
+ if (!ls) return;
79
+ try {
80
+ ls.setItem(AUTH_STORAGE_REFRESH_TOKEN_KEY, refreshToken);
81
+ } catch {}
82
+ },
83
+ clearRefreshToken: () => {
84
+ const ls = getLocalStorage();
85
+ if (!ls) return;
86
+ try {
87
+ ls.removeItem(AUTH_STORAGE_REFRESH_TOKEN_KEY);
88
+ } catch {}
89
+ }
90
+ };
91
+
92
+ // src/AuthProvider.tsx
93
+ var import_jsx_runtime = require("react/jsx-runtime");
94
+ var AuthContext = React.createContext({
95
+ apiUrl: ""
96
+ });
97
+ var AuthProvider = props => {
98
+ const getAuthData = React.useCallback(async () => {
99
+ try {
100
+ const refreshToken = storage.getRefreshToken();
101
+ if (!refreshToken) {
102
+ return null;
103
+ }
104
+ const refreshResponse = await fetch(`${props.apiUrl}/auth/local/refresh`, {
105
+ method: "POST",
106
+ headers: {
107
+ "Content-Type": "application/json"
108
+ },
109
+ body: JSON.stringify({
110
+ refreshToken
111
+ })
112
+ });
113
+ const refreshData = await refreshResponse.json();
114
+ if ("error" in refreshData) {
115
+ storage.clearRefreshToken();
116
+ return null;
117
+ }
118
+ const meResponse = await fetch(`${props.apiUrl}/users/me`, {
119
+ headers: {
120
+ Authorization: `Bearer ${refreshData.jwt}`
121
+ }
122
+ });
123
+ const data = await meResponse.json();
124
+ if (!meResponse.ok) {
125
+ storage.clearRefreshToken();
126
+ return null;
127
+ }
128
+ return {
129
+ user: {
130
+ id: data.id,
131
+ email: data.email,
132
+ emailVerified: data.confirmed ?? data.emailVerified ?? data.user?.confirmed
133
+ },
134
+ tokens: {
135
+ accessToken: refreshData.jwt,
136
+ refreshToken: refreshData.refreshToken
137
+ },
138
+ isAuthenticated: true
139
+ };
140
+ } catch {
141
+ storage.clearRefreshToken();
142
+ return null;
143
+ }
144
+ }, [props.apiUrl]);
145
+ const signOut = React.useCallback(async () => {
146
+ storage.clearRefreshToken();
147
+ }, []);
148
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(AuthContext.Provider, {
149
+ value: {
150
+ apiUrl: props.apiUrl
151
+ },
152
+ children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_react_auth_core.AuthProvider, {
153
+ getAuthData,
154
+ signOut,
155
+ children: props.children
156
+ })
157
+ });
158
+ };
159
+ var useAuth = () => {
160
+ const authCore = (0, import_react_auth_core.useAuth)();
161
+ const {
162
+ apiUrl
163
+ } = React.useContext(AuthContext);
164
+ return {
165
+ apiUrl,
166
+ ...authCore
167
+ };
168
+ };
169
+
170
+ // src/Auth.tsx
171
+ var import_jsx_runtime2 = require("react/jsx-runtime");
172
+ var Auth = () => {
173
+ const {
174
+ setAuthData,
175
+ apiUrl
176
+ } = useAuth();
177
+ const {
178
+ screen,
179
+ setScreen
180
+ } = (0, import_react_auth_core2.useAuthScreen)();
181
+ const {
182
+ addNotification
183
+ } = (0, import_react_notifications.useNotifications)();
184
+ const onSignIn = React2.useCallback(async ({
185
+ email,
186
+ password
187
+ }) => {
188
+ try {
189
+ const response = await fetch(`${apiUrl}/auth/local`, {
190
+ method: "POST",
191
+ headers: {
192
+ "Content-Type": "application/json"
193
+ },
194
+ body: JSON.stringify({
195
+ identifier: email,
196
+ password
197
+ })
198
+ });
199
+ const data = await response.json();
200
+ if (!response.ok) {
201
+ const errorMessage = data.error?.message;
202
+ if (errorMessage === "Your account email is not confirmed") {
203
+ const resendResponse = await fetch(`${apiUrl}/auth/send-email-confirmation`, {
204
+ method: "POST",
205
+ headers: {
206
+ "Content-Type": "application/json"
207
+ },
208
+ body: JSON.stringify({
209
+ email
210
+ })
211
+ });
212
+ if (!resendResponse.ok) {
213
+ const resendData = await resendResponse.json();
214
+ addNotification({
215
+ title: "Resend confirmation email failed",
216
+ message: resendData.error?.message || "An error occurred while resending the confirmation email.",
217
+ type: "error"
218
+ });
219
+ return;
220
+ }
221
+ setScreen({
222
+ value: "confirmSignUpCheckEmail"
223
+ });
224
+ return;
225
+ }
226
+ addNotification({
227
+ title: "Sign in failed",
228
+ message: data.error?.message || "An error occurred during sign in.",
229
+ type: "error"
230
+ });
231
+ return;
232
+ }
233
+ storage.setRefreshToken(data.refreshToken);
234
+ setAuthData({
235
+ user: {
236
+ id: data.user.id,
237
+ email: data.user.email,
238
+ emailVerified: data.user.confirmed
239
+ },
240
+ tokens: {
241
+ accessToken: data.jwt,
242
+ refreshToken: data.refreshToken
243
+ },
244
+ isAuthenticated: true
245
+ });
246
+ } catch {
247
+ addNotification({
248
+ title: "Network Error",
249
+ message: "Unable to connect to the server. Please check your connection.",
250
+ type: "error"
251
+ });
252
+ }
253
+ }, [setAuthData, setScreen, addNotification, apiUrl]);
254
+ const onSignUp = React2.useCallback(async ({
255
+ email,
256
+ password
257
+ }) => {
258
+ try {
259
+ const response = await fetch(`${apiUrl}/auth/local/register`, {
260
+ method: "POST",
261
+ headers: {
262
+ "Content-Type": "application/json"
263
+ },
264
+ body: JSON.stringify({
265
+ username: email,
266
+ // Assuming username is the same as email
267
+ email,
268
+ password
269
+ })
270
+ });
271
+ const data = await response.json();
272
+ if (!response.ok) {
273
+ addNotification({
274
+ title: "Sign up failed",
275
+ message: data.error?.message || "An error occurred during sign up.",
276
+ type: "error"
277
+ });
278
+ return;
279
+ }
280
+ setScreen({
281
+ value: "confirmSignUpCheckEmail"
282
+ });
283
+ } catch {
284
+ addNotification({
285
+ title: "Network Error",
286
+ message: "Unable to connect to the server. Please check your connection.",
287
+ type: "error"
288
+ });
289
+ }
290
+ }, [addNotification, setScreen, apiUrl]);
291
+ const onForgotPassword = React2.useCallback(async ({
292
+ email
293
+ }) => {
294
+ try {
295
+ const response = await fetch(`${apiUrl}/auth/forgot-password`, {
296
+ method: "POST",
297
+ headers: {
298
+ "Content-Type": "application/json"
299
+ },
300
+ body: JSON.stringify({
301
+ email
302
+ })
303
+ });
304
+ const data = await response.json();
305
+ if (!response.ok) {
306
+ addNotification({
307
+ title: "Forgot password failed",
308
+ message: data.error?.message || "An error occurred during forgot password.",
309
+ type: "error"
310
+ });
311
+ return;
312
+ }
313
+ } catch {
314
+ addNotification({
315
+ title: "Network Error",
316
+ message: "Unable to connect to the server. Please check your connection.",
317
+ type: "error"
318
+ });
319
+ }
320
+ }, [addNotification, apiUrl]);
321
+ const onConfirmSignUpCheckEmail = React2.useCallback(async () => {
322
+ setScreen({
323
+ value: "signIn"
324
+ });
325
+ }, [setScreen]);
326
+ return /* @__PURE__ */(0, import_jsx_runtime2.jsx)(import_react_auth_core2.Auth, {
327
+ screen,
328
+ setScreen,
329
+ onSignIn,
330
+ onSignUp,
331
+ onForgotPassword,
332
+ onConfirmSignUpCheckEmail
333
+ });
334
+ };
335
+ // Annotate the CommonJS export names for ESM import in node:
336
+ 0 && (module.exports = {
337
+ Auth,
338
+ AuthProvider,
339
+ useAuth
340
+ });
@@ -0,0 +1,272 @@
1
+ {
2
+ "0XOzcH": [
3
+ {
4
+ "type": 0,
5
+ "value": "Required field"
6
+ }
7
+ ],
8
+ "38HSjr": [
9
+ {
10
+ "type": 0,
11
+ "value": "Sign In"
12
+ }
13
+ ],
14
+ "5E12mO": [
15
+ {
16
+ "type": 0,
17
+ "value": "Email"
18
+ }
19
+ ],
20
+ "6PdOcy": [
21
+ {
22
+ "type": 0,
23
+ "value": "Cancel"
24
+ }
25
+ ],
26
+ "8GMUPk": [
27
+ {
28
+ "type": 0,
29
+ "value": "I'm already registered"
30
+ }
31
+ ],
32
+ "9cApwd": [
33
+ {
34
+ "type": 0,
35
+ "value": "Please, insert a valid e-mail"
36
+ }
37
+ ],
38
+ "BtK6KR": [
39
+ {
40
+ "type": 0,
41
+ "value": "Forgot password?"
42
+ }
43
+ ],
44
+ "Co3exe": [
45
+ {
46
+ "type": 0,
47
+ "value": "By signing up, you agree to the following Terms and Conditions."
48
+ }
49
+ ],
50
+ "D1C6fR": [
51
+ {
52
+ "type": 0,
53
+ "value": "Field must be at least "
54
+ },
55
+ {
56
+ "type": 1,
57
+ "value": "min"
58
+ },
59
+ {
60
+ "type": 0,
61
+ "value": " characters"
62
+ }
63
+ ],
64
+ "EZ3YF2": [
65
+ {
66
+ "type": 0,
67
+ "value": "Sign up"
68
+ }
69
+ ],
70
+ "HT4tSM": [
71
+ {
72
+ "type": 0,
73
+ "value": "Reset Password"
74
+ }
75
+ ],
76
+ "KY2T6J": [
77
+ {
78
+ "type": 0,
79
+ "value": "Code"
80
+ }
81
+ ],
82
+ "MfWGyg": [
83
+ {
84
+ "type": 0,
85
+ "value": "Field is required"
86
+ }
87
+ ],
88
+ "NJ57Qj": [
89
+ {
90
+ "type": 0,
91
+ "value": "Confirm password field is required"
92
+ }
93
+ ],
94
+ "OhDL0i": [
95
+ {
96
+ "type": 0,
97
+ "value": "Invalid email"
98
+ }
99
+ ],
100
+ "PylVqx": [
101
+ {
102
+ "type": 0,
103
+ "value": "Password"
104
+ }
105
+ ],
106
+ "S3pjKw": [
107
+ {
108
+ "type": 0,
109
+ "value": "Minimum "
110
+ },
111
+ {
112
+ "type": 1,
113
+ "value": "value"
114
+ },
115
+ {
116
+ "type": 0,
117
+ "value": " characters"
118
+ }
119
+ ],
120
+ "S4bbEj": [
121
+ {
122
+ "type": 0,
123
+ "value": "Recovering Password"
124
+ }
125
+ ],
126
+ "SQJto2": [
127
+ {
128
+ "type": 0,
129
+ "value": "Sign in"
130
+ }
131
+ ],
132
+ "SfWKyS": [
133
+ {
134
+ "type": 0,
135
+ "value": "Maximum "
136
+ },
137
+ {
138
+ "type": 1,
139
+ "value": "value"
140
+ },
141
+ {
142
+ "type": 0,
143
+ "value": " characters"
144
+ }
145
+ ],
146
+ "TZ4WUk": [
147
+ {
148
+ "type": 0,
149
+ "value": "Password requires "
150
+ },
151
+ {
152
+ "type": 1,
153
+ "value": "value"
154
+ },
155
+ {
156
+ "type": 0,
157
+ "value": " characters"
158
+ }
159
+ ],
160
+ "UNttd+": [
161
+ {
162
+ "type": 0,
163
+ "value": "Confirm"
164
+ }
165
+ ],
166
+ "URJDrG": [
167
+ {
168
+ "type": 0,
169
+ "value": "Sign up"
170
+ }
171
+ ],
172
+ "WU/CqP": [
173
+ {
174
+ "type": 0,
175
+ "value": "Passwords are not the same"
176
+ }
177
+ ],
178
+ "XreZg+": [
179
+ {
180
+ "type": 0,
181
+ "value": "Registered Email"
182
+ }
183
+ ],
184
+ "ZhaPt0": [
185
+ {
186
+ "type": 0,
187
+ "value": "Invalid Value for Field of type "
188
+ },
189
+ {
190
+ "type": 1,
191
+ "value": "type"
192
+ }
193
+ ],
194
+ "cGR2eI": [
195
+ {
196
+ "type": 0,
197
+ "value": "Confirmation"
198
+ }
199
+ ],
200
+ "d1YCuH": [
201
+ {
202
+ "type": 0,
203
+ "value": "Enter your email address"
204
+ }
205
+ ],
206
+ "e3IQoc": [
207
+ {
208
+ "type": 0,
209
+ "value": "New Password"
210
+ }
211
+ ],
212
+ "kdFYba": [
213
+ {
214
+ "type": 0,
215
+ "value": "Password field is required"
216
+ }
217
+ ],
218
+ "khMx/2": [
219
+ {
220
+ "type": 0,
221
+ "value": "An error occurred with your authentication. Please try again."
222
+ }
223
+ ],
224
+ "lY+cuM": [
225
+ {
226
+ "type": 0,
227
+ "value": "Confirm password"
228
+ }
229
+ ],
230
+ "lZvoYL": [
231
+ {
232
+ "type": 0,
233
+ "value": "Sign up now"
234
+ }
235
+ ],
236
+ "mZzmNV": [
237
+ {
238
+ "type": 0,
239
+ "value": "Recover Password"
240
+ }
241
+ ],
242
+ "oayEC3": [
243
+ {
244
+ "type": 0,
245
+ "value": "Sign up"
246
+ }
247
+ ],
248
+ "pwv2cR": [
249
+ {
250
+ "type": 0,
251
+ "value": "We have sent a confirmation code to your email address. Please enter the code below."
252
+ }
253
+ ],
254
+ "qTQ4hP": [
255
+ {
256
+ "type": 0,
257
+ "value": "An email has been sent to your address. Please check your inbox and follow the instructions to confirm your sign up."
258
+ }
259
+ ],
260
+ "qnQYqN": [
261
+ {
262
+ "type": 0,
263
+ "value": "You must accept the terms and conditions"
264
+ }
265
+ ],
266
+ "s1OmP0": [
267
+ {
268
+ "type": 0,
269
+ "value": "Confirmation code"
270
+ }
271
+ ]
272
+ }