clefbase 2.0.4 → 2.0.6
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/auth/index.d.ts +21 -36
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +25 -136
- package/dist/auth/index.js.map +1 -1
- package/dist/cli-src/cli/commands/init.js +12 -451
- package/dist/cli.js +13 -404
- package/dist/functions.d.ts +2 -2
- package/dist/functions.d.ts.map +1 -1
- package/dist/functions.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +27 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -85,11 +85,6 @@ async function runInit(cwd = process.cwd()) {
|
|
|
85
85
|
hosting: services.includes("hosting"),
|
|
86
86
|
functions: services.includes("functions"),
|
|
87
87
|
};
|
|
88
|
-
// ── Step 4: Auth setup ────────────────────────────────────────────────────
|
|
89
|
-
let authSetup;
|
|
90
|
-
if (cfg.services.auth) {
|
|
91
|
-
authSetup = await setupAuth(cwd);
|
|
92
|
-
}
|
|
93
88
|
// ── Step 5: Hosting setup ─────────────────────────────────────────────────
|
|
94
89
|
if (cfg.services.hosting) {
|
|
95
90
|
await setupHosting(cfg, cwd);
|
|
@@ -105,11 +100,6 @@ async function runInit(cwd = process.cwd()) {
|
|
|
105
100
|
(0, config_1.writeEnvExample)(cfg, cwd);
|
|
106
101
|
// ── Step 8: Scaffold src/lib ──────────────────────────────────────────────
|
|
107
102
|
const libResult = scaffoldLib(cfg, cwd);
|
|
108
|
-
// ── Step 9: Scaffold auth files ──────────────────────────────────────────
|
|
109
|
-
let authResult;
|
|
110
|
-
if (cfg.services.auth && authSetup) {
|
|
111
|
-
authResult = await scaffoldAuth(cwd, authSetup);
|
|
112
|
-
}
|
|
113
103
|
// ── Done ──────────────────────────────────────────────────────────────────
|
|
114
104
|
console.log();
|
|
115
105
|
console.log(chalk_1.default.green.bold(" ✓ Project initialised!"));
|
|
@@ -121,172 +111,12 @@ async function runInit(cwd = process.cwd()) {
|
|
|
121
111
|
console.log(chalk_1.default.dim(` Lib config: ${libResult.configCopy}`));
|
|
122
112
|
console.log(chalk_1.default.dim(` Lib entry: ${libResult.libFile}`));
|
|
123
113
|
}
|
|
124
|
-
if (authResult?.hasAuth) {
|
|
125
|
-
if (authResult.context)
|
|
126
|
-
console.log(chalk_1.default.dim(` Auth context: ${authResult.context}`));
|
|
127
|
-
if (authResult.modal)
|
|
128
|
-
console.log(chalk_1.default.dim(` Auth modal: ${authResult.modal}`));
|
|
129
|
-
if (authResult.protected)
|
|
130
|
-
console.log(chalk_1.default.dim(` Protected: ${authResult.protected}`));
|
|
131
|
-
}
|
|
132
114
|
if (cfg.services.functions) {
|
|
133
115
|
console.log(chalk_1.default.dim(` Functions: functions/ (${functionsRuntime ?? "node"})`));
|
|
134
116
|
console.log(chalk_1.default.dim(` run: node functions/deploy.mjs`));
|
|
135
117
|
}
|
|
136
118
|
console.log();
|
|
137
|
-
printUsageHint(cfg
|
|
138
|
-
}
|
|
139
|
-
// ─── Auth scaffolding ────────────────────────────────────────────────────────
|
|
140
|
-
/**
|
|
141
|
-
* Interactively scaffold auth files (AuthContext, AuthModal, etc.).
|
|
142
|
-
*/
|
|
143
|
-
async function setupAuth(cwd) {
|
|
144
|
-
console.log();
|
|
145
|
-
console.log(chalk_1.default.bold(" Auth Setup"));
|
|
146
|
-
console.log();
|
|
147
|
-
const { scaffoldContext } = await inquirer_1.default.prompt([{
|
|
148
|
-
type: "confirm",
|
|
149
|
-
name: "scaffoldContext",
|
|
150
|
-
message: "Scaffold React AuthContext?",
|
|
151
|
-
default: true,
|
|
152
|
-
}]);
|
|
153
|
-
const { scaffoldModal } = await inquirer_1.default.prompt([{
|
|
154
|
-
type: "confirm",
|
|
155
|
-
name: "scaffoldModal",
|
|
156
|
-
message: "Scaffold AuthModal component?",
|
|
157
|
-
default: true,
|
|
158
|
-
}]);
|
|
159
|
-
const { updateApp } = await inquirer_1.default.prompt([{
|
|
160
|
-
type: "confirm",
|
|
161
|
-
name: "updateApp",
|
|
162
|
-
message: "Update App.tsx to use AuthProvider?",
|
|
163
|
-
default: false,
|
|
164
|
-
}]);
|
|
165
|
-
return { scaffoldContext, scaffoldModal, updateApp };
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Get the path to a template file within the clefbase CLI package.
|
|
169
|
-
*/
|
|
170
|
-
function getTemplatePath(filename) {
|
|
171
|
-
// In compiled dist, __dirname will be dist-src/cli/commands
|
|
172
|
-
// We want to resolve to src/cli/templates/ in source
|
|
173
|
-
// Or in dist, it would be at ../templates/
|
|
174
|
-
let dir = __dirname;
|
|
175
|
-
// Check if we're in dist (compiled)
|
|
176
|
-
if (dir.includes("/dist-src/")) {
|
|
177
|
-
return path_1.default.join(dir, "../templates", filename);
|
|
178
|
-
}
|
|
179
|
-
// Otherwise we're in source
|
|
180
|
-
return path_1.default.join(dir, "../templates", filename);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Read a template file with fallback to embedded template.
|
|
184
|
-
*/
|
|
185
|
-
function readTemplate(filename, fallback) {
|
|
186
|
-
try {
|
|
187
|
-
const templatePath = getTemplatePath(filename);
|
|
188
|
-
return fs_1.default.readFileSync(templatePath, "utf-8");
|
|
189
|
-
}
|
|
190
|
-
catch {
|
|
191
|
-
return fallback;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Scaffold auth-related files (AuthContext, AuthModal, ProtectedRoute).
|
|
196
|
-
*/
|
|
197
|
-
async function scaffoldAuth(cwd, setup) {
|
|
198
|
-
const result = { hasAuth: false };
|
|
199
|
-
// Create context directory
|
|
200
|
-
const contextDir = path_1.default.join(cwd, "src", "context");
|
|
201
|
-
fs_1.default.mkdirSync(contextDir, { recursive: true });
|
|
202
|
-
if (setup.scaffoldContext) {
|
|
203
|
-
const contextFile = path_1.default.join(contextDir, "AuthContext.tsx");
|
|
204
|
-
try {
|
|
205
|
-
const content = readTemplate("AuthContext.tsx.template", AUTH_CONTEXT_FALLBACK);
|
|
206
|
-
fs_1.default.writeFileSync(contextFile, content);
|
|
207
|
-
result.context = path_1.default.relative(cwd, contextFile);
|
|
208
|
-
result.hasAuth = true;
|
|
209
|
-
}
|
|
210
|
-
catch (err) {
|
|
211
|
-
console.warn(chalk_1.default.yellow(` Warning: Could not scaffold AuthContext: ${err.message}`));
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (setup.scaffoldModal) {
|
|
215
|
-
const componentDir = path_1.default.join(cwd, "src", "components");
|
|
216
|
-
fs_1.default.mkdirSync(componentDir, { recursive: true });
|
|
217
|
-
const modalFile = path_1.default.join(componentDir, "AuthModal.tsx");
|
|
218
|
-
try {
|
|
219
|
-
const content = readTemplate("AuthModal.tsx.template", AUTH_MODAL_FALLBACK);
|
|
220
|
-
fs_1.default.writeFileSync(modalFile, content);
|
|
221
|
-
result.modal = path_1.default.relative(cwd, modalFile);
|
|
222
|
-
result.hasAuth = true;
|
|
223
|
-
}
|
|
224
|
-
catch (err) {
|
|
225
|
-
console.warn(chalk_1.default.yellow(` Warning: Could not scaffold AuthModal: ${err.message}`));
|
|
226
|
-
}
|
|
227
|
-
// Also scaffold ProtectedRoute
|
|
228
|
-
const protectedFile = path_1.default.join(componentDir, "ProtectedRoute.tsx");
|
|
229
|
-
try {
|
|
230
|
-
const content = readTemplate("ProtectedRoute.tsx.template", PROTECTED_ROUTE_FALLBACK);
|
|
231
|
-
fs_1.default.writeFileSync(protectedFile, content);
|
|
232
|
-
result.protected = path_1.default.relative(cwd, protectedFile);
|
|
233
|
-
}
|
|
234
|
-
catch (err) {
|
|
235
|
-
console.warn(chalk_1.default.yellow(` Warning: Could not scaffold ProtectedRoute: ${err.message}`));
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
// Update App.tsx if it exists and user wants it
|
|
239
|
-
if (setup.updateApp) {
|
|
240
|
-
const appPath = path_1.default.join(cwd, "src", "App.tsx");
|
|
241
|
-
if (fs_1.default.existsSync(appPath)) {
|
|
242
|
-
try {
|
|
243
|
-
updateAppWithAuth(appPath);
|
|
244
|
-
}
|
|
245
|
-
catch (err) {
|
|
246
|
-
console.warn(chalk_1.default.yellow(` Warning: Could not update App.tsx: ${err.message}`));
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return result;
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Update an existing App.tsx to wrap with AuthProvider.
|
|
254
|
-
*/
|
|
255
|
-
function updateAppWithAuth(appPath) {
|
|
256
|
-
let content = fs_1.default.readFileSync(appPath, "utf-8");
|
|
257
|
-
// Check if already has AuthProvider
|
|
258
|
-
if (content.includes("AuthProvider")) {
|
|
259
|
-
return; // Already updated
|
|
260
|
-
}
|
|
261
|
-
// Add import if not present
|
|
262
|
-
if (!content.includes("import { AuthProvider, useAuth }")) {
|
|
263
|
-
const importLine = "import { AuthProvider, useAuth } from '@/context/AuthContext';\n";
|
|
264
|
-
content = importLine + content;
|
|
265
|
-
}
|
|
266
|
-
// Check if this is a functional component that exports a component
|
|
267
|
-
// Look for "export default function" or "export default"
|
|
268
|
-
const exportMatch = content.match(/export\s+default\s+(function\s+\w+\s*\(|const\s+\w+\s*=|\(\)|=>)/);
|
|
269
|
-
if (exportMatch) {
|
|
270
|
-
// Find the return statement and wrap the JSX with AuthProvider
|
|
271
|
-
// This is a simple approach - find the outermost return/JSX in export
|
|
272
|
-
const returnMatch = content.match(/export\s+default\s+(?:function\s+\w+\s*\([^)]*\)\s*\{|\(.*?\)\s*=>\s*)[\s\S]*?return\s+/);
|
|
273
|
-
if (returnMatch) {
|
|
274
|
-
// Insert AuthProvider wrapper
|
|
275
|
-
// This is complex, so we'll do a simpler approach:
|
|
276
|
-
// Just add the import and let users wrap manually
|
|
277
|
-
// Or check if it's a simple functional component
|
|
278
|
-
if (content.includes("return (") || content.includes("return <")) {
|
|
279
|
-
// Try to wrap the main rendered content
|
|
280
|
-
// Find the JSX being returned and wrap it
|
|
281
|
-
const beforeReturn = content.substring(0, content.lastIndexOf("return"));
|
|
282
|
-
const afterReturn = content.substring(content.lastIndexOf("return"));
|
|
283
|
-
const wrappedReturn = afterReturn
|
|
284
|
-
.replace(/return\s+(<[^>]+>[\s\S]*)/m, 'return (\n <AuthProvider>\n $1\n </AuthProvider>\n )');
|
|
285
|
-
content = beforeReturn + wrappedReturn;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
fs_1.default.writeFileSync(appPath, content);
|
|
119
|
+
printUsageHint(cfg);
|
|
290
120
|
}
|
|
291
121
|
// ─── Functions scaffolding ────────────────────────────────────────────────────
|
|
292
122
|
/**
|
|
@@ -414,16 +244,14 @@ can return any JSON-serialisable value.
|
|
|
414
244
|
### FunctionContext shape
|
|
415
245
|
|
|
416
246
|
\`\`\`ts
|
|
247
|
+
import type { FunctionContext, AuthUser } from "clefbase";
|
|
248
|
+
|
|
417
249
|
interface FunctionContext {
|
|
418
250
|
/** Payload supplied by the caller (HTTP) or the event (triggers). */
|
|
419
|
-
data
|
|
251
|
+
data?: unknown;
|
|
420
252
|
|
|
421
253
|
/** Populated when the caller includes a valid auth token. */
|
|
422
|
-
auth?:
|
|
423
|
-
uid: string;
|
|
424
|
-
email?: string;
|
|
425
|
-
metadata: Record<string, unknown>;
|
|
426
|
-
};
|
|
254
|
+
auth?: AuthUser;
|
|
427
255
|
|
|
428
256
|
/** Describes what fired the function. */
|
|
429
257
|
trigger: {
|
|
@@ -619,7 +447,11 @@ const FUNCTIONS_TSCONFIG = `{
|
|
|
619
447
|
"strict": true,
|
|
620
448
|
"esModuleInterop": true,
|
|
621
449
|
"skipLibCheck": true,
|
|
622
|
-
"outDir": "dist"
|
|
450
|
+
"outDir": "dist",
|
|
451
|
+
"baseUrl": ".",
|
|
452
|
+
"paths": {
|
|
453
|
+
"@/*": ["src/*"]
|
|
454
|
+
}
|
|
623
455
|
},
|
|
624
456
|
"include": ["src/**/*"],
|
|
625
457
|
"exclude": ["node_modules", "dist"]
|
|
@@ -941,7 +773,7 @@ function buildLibTs(cfg) {
|
|
|
941
773
|
` * const { user } = await auth.signIn("alice@example.com", "pass");`,
|
|
942
774
|
` *`,
|
|
943
775
|
` * // Google sign-in — add this to your root component / entry point:`,
|
|
944
|
-
` * await auth.
|
|
776
|
+
` * const result = await auth.handleAuthCallback(); // handles ?cfx_token= on return`,
|
|
945
777
|
` *`,
|
|
946
778
|
` * // On sign-in button click:`,
|
|
947
779
|
` * await auth.signInWithGateway("google");`,
|
|
@@ -985,20 +817,6 @@ function buildLibTs(cfg) {
|
|
|
985
817
|
lines.push(`/** Clefbase Auth — sign up, sign in, manage sessions. */`);
|
|
986
818
|
lines.push(`export const auth: Auth = getAuth(app);`);
|
|
987
819
|
lines.push("");
|
|
988
|
-
lines.push(`/**`);
|
|
989
|
-
lines.push(` * Call this once on every page load (before rendering your UI).`);
|
|
990
|
-
lines.push(` * Detects the ?cfx_token= param the gateway appends after OAuth,`);
|
|
991
|
-
lines.push(` * saves the session, and cleans the URL.`);
|
|
992
|
-
lines.push(` *`);
|
|
993
|
-
lines.push(` * @example`);
|
|
994
|
-
lines.push(` * // React / Next.js — in your root layout or _app.tsx:`);
|
|
995
|
-
lines.push(` * useEffect(() => { auth.handleGatewayCallback(); }, []);`);
|
|
996
|
-
lines.push(` *`);
|
|
997
|
-
lines.push(` * // Vanilla JS — at the top of your entry point:`);
|
|
998
|
-
lines.push(` * await auth.handleGatewayCallback();`);
|
|
999
|
-
lines.push(` */`);
|
|
1000
|
-
lines.push(`export { auth };`);
|
|
1001
|
-
lines.push("");
|
|
1002
820
|
}
|
|
1003
821
|
if (storage) {
|
|
1004
822
|
lines.push(`/** Clefbase Storage — upload and manage files. */`);
|
|
@@ -1141,16 +959,12 @@ function printUsageHint(cfg, authSetup) {
|
|
|
1141
959
|
console.log(chalk_1.default.cyan(` import { AuthProvider, useAuth } from "@/context/AuthContext";`));
|
|
1142
960
|
console.log(chalk_1.default.cyan(` const { user, signIn, signOut } = useAuth();`));
|
|
1143
961
|
}
|
|
1144
|
-
if (authSetup?.scaffoldModal) {
|
|
1145
|
-
console.log(chalk_1.default.cyan(` import AuthModal from "@/components/AuthModal";`));
|
|
1146
|
-
}
|
|
1147
962
|
if (authSetup?.scaffoldContext) {
|
|
1148
963
|
console.log(chalk_1.default.cyan(` import { EmailVerificationCard } from "clefbase/react";`));
|
|
1149
964
|
}
|
|
1150
965
|
if (!authSetup?.scaffoldContext) {
|
|
1151
966
|
console.log(chalk_1.default.cyan(` const { user } = await auth.signIn("email", "pass");`));
|
|
1152
|
-
console.log(chalk_1.default.cyan(` await auth.
|
|
1153
|
-
console.log(chalk_1.default.cyan(` await auth.handleGatewayCallback(); // call on every page load`));
|
|
967
|
+
console.log(chalk_1.default.cyan(` const result = await auth.handleAuthCallback(); // call on every page load`));
|
|
1154
968
|
}
|
|
1155
969
|
}
|
|
1156
970
|
if (cfg.services.storage) {
|
|
@@ -1178,256 +992,3 @@ function printUsageHint(cfg, authSetup) {
|
|
|
1178
992
|
}
|
|
1179
993
|
console.log();
|
|
1180
994
|
}
|
|
1181
|
-
// ─── Template Fallbacks ──────────────────────────────────────────────────
|
|
1182
|
-
const AUTH_CONTEXT_FALLBACK = `'use client';
|
|
1183
|
-
import React, { createContext, useContext, useEffect, useState } from 'react';
|
|
1184
|
-
import type { AuthUser } from 'clefbase';
|
|
1185
|
-
import { getAuth } from 'clefbase';
|
|
1186
|
-
|
|
1187
|
-
interface AuthContextType {
|
|
1188
|
-
user: AuthUser | null;
|
|
1189
|
-
token: string | null;
|
|
1190
|
-
loading: boolean;
|
|
1191
|
-
error: string | null;
|
|
1192
|
-
isAuthModalOpen: boolean;
|
|
1193
|
-
signIn: (email: string, password: string) => Promise<void>;
|
|
1194
|
-
signUp: (email: string, password: string, profile?: any) => Promise<void>;
|
|
1195
|
-
signOut: () => Promise<void>;
|
|
1196
|
-
updateProfile: (updates: any) => Promise<void>;
|
|
1197
|
-
verifyEmail: (code: string) => Promise<void>;
|
|
1198
|
-
sendPasswordResetEmail: (email: string) => Promise<void>;
|
|
1199
|
-
refreshUser: () => Promise<void>;
|
|
1200
|
-
openAuthModal: () => void;
|
|
1201
|
-
closeAuthModal: () => void;
|
|
1202
|
-
}
|
|
1203
|
-
|
|
1204
|
-
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
1205
|
-
|
|
1206
|
-
export function useAuth(): AuthContextType {
|
|
1207
|
-
const context = useContext(AuthContext);
|
|
1208
|
-
if (!context) throw new Error('useAuth() must be used within <AuthProvider>');
|
|
1209
|
-
return context;
|
|
1210
|
-
}
|
|
1211
|
-
|
|
1212
|
-
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|
1213
|
-
const [user, setUser] = useState<AuthUser | null>(null);
|
|
1214
|
-
const [token, setToken] = useState<string | null>(null);
|
|
1215
|
-
const [loading, setLoading] = useState(true);
|
|
1216
|
-
const [error, setError] = useState<string | null>(null);
|
|
1217
|
-
const [isAuthModalOpen, setIsAuthModalOpen] = useState(false);
|
|
1218
|
-
const auth = getAuth();
|
|
1219
|
-
|
|
1220
|
-
useEffect(() => {
|
|
1221
|
-
const initAuth = async () => {
|
|
1222
|
-
try {
|
|
1223
|
-
setLoading(true);
|
|
1224
|
-
const currentUser = auth.currentUser;
|
|
1225
|
-
const currentToken = auth.currentToken;
|
|
1226
|
-
setUser(currentUser);
|
|
1227
|
-
setToken(currentToken);
|
|
1228
|
-
const unsubscribe = auth.onAuthStateChanged((newUser) => {
|
|
1229
|
-
setUser(newUser);
|
|
1230
|
-
setToken(newUser ? auth.currentToken : null);
|
|
1231
|
-
});
|
|
1232
|
-
setLoading(false);
|
|
1233
|
-
return () => unsubscribe();
|
|
1234
|
-
} catch (err) {
|
|
1235
|
-
console.error('Auth init error:', err);
|
|
1236
|
-
setLoading(false);
|
|
1237
|
-
}
|
|
1238
|
-
};
|
|
1239
|
-
initAuth();
|
|
1240
|
-
}, [auth]);
|
|
1241
|
-
|
|
1242
|
-
const handleError = (err: unknown): string => {
|
|
1243
|
-
if (err instanceof Error) return err.message;
|
|
1244
|
-
if (typeof err === 'string') return err;
|
|
1245
|
-
return 'Unknown error';
|
|
1246
|
-
};
|
|
1247
|
-
|
|
1248
|
-
const signIn = async (email: string, password: string) => {
|
|
1249
|
-
try {
|
|
1250
|
-
setError(null);
|
|
1251
|
-
setLoading(true);
|
|
1252
|
-
await auth.signIn(email, password);
|
|
1253
|
-
} catch (err) {
|
|
1254
|
-
const message = handleError(err);
|
|
1255
|
-
setError(message);
|
|
1256
|
-
throw err;
|
|
1257
|
-
} finally {
|
|
1258
|
-
setLoading(false);
|
|
1259
|
-
}
|
|
1260
|
-
};
|
|
1261
|
-
|
|
1262
|
-
const signUp = async (email: string, password: string, profile?: any) => {
|
|
1263
|
-
try {
|
|
1264
|
-
setError(null);
|
|
1265
|
-
setLoading(true);
|
|
1266
|
-
await auth.signUp(email, password, profile);
|
|
1267
|
-
} catch (err) {
|
|
1268
|
-
const message = handleError(err);
|
|
1269
|
-
setError(message);
|
|
1270
|
-
throw err;
|
|
1271
|
-
} finally {
|
|
1272
|
-
setLoading(false);
|
|
1273
|
-
}
|
|
1274
|
-
};
|
|
1275
|
-
|
|
1276
|
-
const signOut = async () => {
|
|
1277
|
-
try {
|
|
1278
|
-
setError(null);
|
|
1279
|
-
setLoading(true);
|
|
1280
|
-
await auth.signOut();
|
|
1281
|
-
setUser(null);
|
|
1282
|
-
setToken(null);
|
|
1283
|
-
} catch (err) {
|
|
1284
|
-
const message = handleError(err);
|
|
1285
|
-
setError(message);
|
|
1286
|
-
throw err;
|
|
1287
|
-
} finally {
|
|
1288
|
-
setLoading(false);
|
|
1289
|
-
}
|
|
1290
|
-
};
|
|
1291
|
-
|
|
1292
|
-
const updateProfile = async (updates: any) => {
|
|
1293
|
-
try {
|
|
1294
|
-
setError(null);
|
|
1295
|
-
setLoading(true);
|
|
1296
|
-
await auth.updateProfile(updates);
|
|
1297
|
-
} catch (err) {
|
|
1298
|
-
const message = handleError(err);
|
|
1299
|
-
setError(message);
|
|
1300
|
-
throw err;
|
|
1301
|
-
} finally {
|
|
1302
|
-
setLoading(false);
|
|
1303
|
-
}
|
|
1304
|
-
};
|
|
1305
|
-
|
|
1306
|
-
const verifyEmail = async (code: string) => {
|
|
1307
|
-
try {
|
|
1308
|
-
setError(null);
|
|
1309
|
-
setLoading(true);
|
|
1310
|
-
const result = await auth.verifyEmail(code);
|
|
1311
|
-
setUser(result);
|
|
1312
|
-
} catch (err) {
|
|
1313
|
-
const message = handleError(err);
|
|
1314
|
-
setError(message);
|
|
1315
|
-
throw err;
|
|
1316
|
-
} finally {
|
|
1317
|
-
setLoading(false);
|
|
1318
|
-
}
|
|
1319
|
-
};
|
|
1320
|
-
|
|
1321
|
-
const sendPasswordResetEmail = async (email: string) => {
|
|
1322
|
-
try {
|
|
1323
|
-
setError(null);
|
|
1324
|
-
setLoading(true);
|
|
1325
|
-
await auth.sendPasswordResetEmail(email);
|
|
1326
|
-
} catch (err) {
|
|
1327
|
-
const message = handleError(err);
|
|
1328
|
-
setError(message);
|
|
1329
|
-
throw err;
|
|
1330
|
-
} finally {
|
|
1331
|
-
setLoading(false);
|
|
1332
|
-
}
|
|
1333
|
-
};
|
|
1334
|
-
|
|
1335
|
-
const refreshUser = async () => {
|
|
1336
|
-
try {
|
|
1337
|
-
setError(null);
|
|
1338
|
-
const updated = await auth.refreshCurrentUser();
|
|
1339
|
-
if (updated) setUser(updated);
|
|
1340
|
-
} catch (err) {
|
|
1341
|
-
const message = handleError(err);
|
|
1342
|
-
setError(message);
|
|
1343
|
-
throw err;
|
|
1344
|
-
}
|
|
1345
|
-
};
|
|
1346
|
-
|
|
1347
|
-
const openAuthModal = () => setIsAuthModalOpen(true);
|
|
1348
|
-
const closeAuthModal = () => setIsAuthModalOpen(false);
|
|
1349
|
-
|
|
1350
|
-
const value: AuthContextType = {
|
|
1351
|
-
user, token, loading, error, isAuthModalOpen,
|
|
1352
|
-
signIn, signUp, signOut, updateProfile, verifyEmail,
|
|
1353
|
-
sendPasswordResetEmail, refreshUser, openAuthModal, closeAuthModal,
|
|
1354
|
-
};
|
|
1355
|
-
|
|
1356
|
-
return (
|
|
1357
|
-
<AuthContext.Provider value={value}>
|
|
1358
|
-
{!loading && children}
|
|
1359
|
-
</AuthContext.Provider>
|
|
1360
|
-
);
|
|
1361
|
-
}`;
|
|
1362
|
-
const AUTH_MODAL_FALLBACK = `'use client';
|
|
1363
|
-
import React, { useEffect, CSSProperties } from 'react';
|
|
1364
|
-
import { useAuth } from './AuthContext';
|
|
1365
|
-
|
|
1366
|
-
export default function AuthModal({ isOpen, onClose, overlayStyle, cardStyle, showCloseButton = true, backdropDismiss = true }: any) {
|
|
1367
|
-
const { closeAuthModal } = useAuth();
|
|
1368
|
-
const projectId = typeof window !== 'undefined' && (window as any).__CLEFORYX_PROJECT_ID;
|
|
1369
|
-
|
|
1370
|
-
useEffect(() => {
|
|
1371
|
-
if (!isOpen) return;
|
|
1372
|
-
const handleMessage = (e: MessageEvent) => {
|
|
1373
|
-
if (e.data?.source === 'cleforyx-auth' && e.data?.type === 'auth_success') {
|
|
1374
|
-
closeAuthModal();
|
|
1375
|
-
onClose();
|
|
1376
|
-
}
|
|
1377
|
-
};
|
|
1378
|
-
window.addEventListener('message', handleMessage);
|
|
1379
|
-
return () => window.removeEventListener('message', handleMessage);
|
|
1380
|
-
}, [isOpen, closeAuthModal, onClose]);
|
|
1381
|
-
|
|
1382
|
-
if (!isOpen) return null;
|
|
1383
|
-
|
|
1384
|
-
const currentUrl = typeof window !== 'undefined' ? window.location.origin : '';
|
|
1385
|
-
const iframeUrl = \`https://auth.cleforyx.com/login?project=\${projectId}&redirect=\${encodeURIComponent(currentUrl)}&embed=popup\`;
|
|
1386
|
-
|
|
1387
|
-
const defaultOverlayStyle: CSSProperties = {
|
|
1388
|
-
position: 'fixed', inset: 0, backgroundColor: 'rgba(15, 23, 42, 0.6)',
|
|
1389
|
-
display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
1390
|
-
zIndex: 9999, backdropFilter: 'blur(4px)', padding: '16px', boxSizing: 'border-box',
|
|
1391
|
-
};
|
|
1392
|
-
|
|
1393
|
-
const defaultCardStyle: CSSProperties = {
|
|
1394
|
-
backgroundColor: 'white', borderRadius: '20px',
|
|
1395
|
-
boxShadow: '0 24px 64px rgba(0, 0, 0, 0.2)',
|
|
1396
|
-
overflow: 'hidden', width: '100%', maxWidth: '460px', position: 'relative',
|
|
1397
|
-
};
|
|
1398
|
-
|
|
1399
|
-
const handleBackdropClick = (e: React.MouseEvent) => {
|
|
1400
|
-
if (backdropDismiss && e.target === e.currentTarget) onClose();
|
|
1401
|
-
};
|
|
1402
|
-
|
|
1403
|
-
return (
|
|
1404
|
-
<div style={{ ...defaultOverlayStyle, ...overlayStyle }} onClick={handleBackdropClick}>
|
|
1405
|
-
<div style={{ ...defaultCardStyle, ...cardStyle }}>
|
|
1406
|
-
{showCloseButton && (
|
|
1407
|
-
<button style={{ position: 'absolute', top: '14px', right: '16px', background: 'rgba(0,0,0,0.06)', border: 'none', width: '28px', height: '28px', borderRadius: '50%', cursor: 'pointer', zIndex: 1 }} onClick={onClose} aria-label="Close">✕</button>
|
|
1408
|
-
)}
|
|
1409
|
-
<iframe src={iframeUrl} style={{ width: '100%', height: '560px', border: 'none', display: 'block' }} title="Sign in" allow="identity-credentials-get" />
|
|
1410
|
-
</div>
|
|
1411
|
-
</div>
|
|
1412
|
-
);
|
|
1413
|
-
}`;
|
|
1414
|
-
const PROTECTED_ROUTE_FALLBACK = `'use client';
|
|
1415
|
-
import React from 'react';
|
|
1416
|
-
import { useAuth } from './AuthContext';
|
|
1417
|
-
|
|
1418
|
-
export function ProtectedRoute({ children, LoadingComponent, onUnauthorized }: any) {
|
|
1419
|
-
const { user, loading, openAuthModal } = useAuth();
|
|
1420
|
-
|
|
1421
|
-
if (loading) {
|
|
1422
|
-
if (LoadingComponent) return <LoadingComponent />;
|
|
1423
|
-
return <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '100vh' }}>Loading...</div>;
|
|
1424
|
-
}
|
|
1425
|
-
|
|
1426
|
-
if (!user) {
|
|
1427
|
-
onUnauthorized?.();
|
|
1428
|
-
openAuthModal();
|
|
1429
|
-
return null;
|
|
1430
|
-
}
|
|
1431
|
-
|
|
1432
|
-
return <>{children}</>;
|
|
1433
|
-
}`;
|