@frank-auth/react
Version:
Flexible and customizable React UI components for Frank Authentication
244 lines (243 loc) • 6.31 kB
JavaScript
import { useState as E, useCallback as u, useMemo as o } from "react";
import { useAuth as z } from "./use-auth.js";
function P() {
const { user: e, updateUser: h, deleteUser: f, reload: l, sdk: a } = z(), [d, t] = E(!1), [m, s] = E(null), n = u((r) => {
const i = {
code: r.code || "UNKNOWN_ERROR",
message: r.message || "An unknown error occurred",
details: r.details,
field: r.field
};
throw s(i), i;
}, []), c = u(async (r) => {
if (!a.user) throw new Error("User not authenticated");
try {
t(!0), s(null);
const i = await a.user.updateProfile(r);
return await l(), i;
} catch (i) {
return n(i);
} finally {
t(!1);
}
}, [a.user, l, n]), y = u(async (r) => {
if (!a.user) throw new Error("User not authenticated");
try {
t(!0), s(null), await a.user.changePassword(r);
} catch (i) {
n(i);
} finally {
t(!1);
}
}, [a.user, n]), p = u(async () => {
try {
t(!0), s(null), await f();
} catch (r) {
n(r);
} finally {
t(!1);
}
}, [f, n]), w = u(async (r) => {
try {
t(!0), s(null), await c({ primaryEmailAddress: r });
} catch (i) {
n(i);
} finally {
t(!1);
}
}, [c, n]), g = u(async (r) => {
if (!a.user) throw new Error("User not authenticated");
try {
t(!0), s(null);
const i = {
code: r,
type: "email"
};
await a.auth.verifyEmail(i), await l();
} catch (i) {
n(i);
} finally {
t(!1);
}
}, [a.user, l, n]), V = u(async () => {
if (!a.user || !e?.primaryEmailAddress) throw new Error("User not authenticated");
try {
t(!0), s(null), await a.user.resendEmailVerification(e.primaryEmailAddress);
} catch (r) {
n(r);
} finally {
t(!1);
}
}, [a.user, e?.primaryEmailAddress, n]), A = u(async (r) => {
try {
t(!0), s(null), await c({ primaryPhoneNumber: r });
} catch (i) {
n(i);
} finally {
t(!1);
}
}, [c, n]), N = u(async (r) => {
if (!a.user) throw new Error("User not authenticated");
try {
t(!0), s(null);
const i = {
code: r,
type: "phone"
};
await a.auth.verifyPhone(i), await l();
} catch (i) {
n(i);
} finally {
t(!1);
}
}, [a.user, l, n]), U = u(async () => {
if (!a.user || !e?.primaryPhoneNumber) throw new Error("User not authenticated");
try {
t(!0), s(null), await a.user.resendPhoneVerification(e.primaryPhoneNumber);
} catch (r) {
n(r);
} finally {
t(!1);
}
}, [a.user, e?.primaryPhoneNumber, n]), v = u(async (r) => c({ profileImageUrl: r }), [c]), I = u(async () => c({ profileImageUrl: (void 0)() }), [c]), b = u(async (r) => c({ unsafeMetadata: r }), [c]), k = o(() => e?.firstName || null, [e]), L = o(() => e?.lastName || null, [e]), R = o(() => {
if (!e) return null;
const r = [e.firstName, e.lastName].filter(Boolean);
return r.length > 0 ? r.join(" ") : null;
}, [e]), S = o(() => e?.primaryEmailAddress || null, [e]), M = o(() => e?.primaryPhoneNumber || null, [e]), B = o(() => e?.profileImageUrl || null, [e]), q = o(() => e?.username || null, [e]), D = o(() => e?.emailVerified || !1, [e]), O = o(() => e?.phoneVerified || !1, [e]), j = o(() => !!e && !e.emailVerified, [e]), x = o(() => !!e && !!e.primaryPhoneNumber && !e.phoneVerified, [e]), C = o(() => e?.active || !1, [e]), K = o(() => e?.blocked || !1, [e]), W = o(() => e?.createdAt ? new Date(e.createdAt) : null, [e]), _ = o(() => e?.lastSignInAt ? new Date(e.lastSignInAt) : null, [e]);
return {
// User state
user: e,
isLoaded: !!e,
isLoading: d,
error: m,
// Profile management
updateProfile: c,
changePassword: y,
deleteAccount: p,
// Email management
updateEmail: w,
verifyEmail: g,
resendEmailVerification: V,
// Phone management
updatePhone: A,
verifyPhone: N,
resendPhoneVerification: U,
// Profile image
updateProfileImage: v,
removeProfileImage: I,
// Metadata management
updateMetadata: b,
// Convenience properties
firstName: k,
lastName: L,
fullName: R,
email: S,
phone: M,
profileImageUrl: B,
username: q,
// Verification status
isEmailVerified: D,
isPhoneVerified: O,
needsEmailVerification: j,
needsPhoneVerification: x,
// Account status
isActive: C,
isBlocked: K,
createdAt: W,
lastSignInAt: _
};
}
function H() {
const {
user: e,
firstName: h,
lastName: f,
fullName: l,
email: a,
phone: d,
profileImageUrl: t,
username: m,
isEmailVerified: s,
isPhoneVerified: n,
isActive: c,
isBlocked: y,
createdAt: p,
lastSignInAt: w
} = P();
return {
user: e,
firstName: h,
lastName: f,
fullName: l,
email: a,
phone: d,
profileImageUrl: t,
username: m,
isEmailVerified: s,
isPhoneVerified: n,
isActive: c,
isBlocked: y,
createdAt: p,
lastSignInAt: w
};
}
function J() {
const {
isEmailVerified: e,
isPhoneVerified: h,
needsEmailVerification: f,
needsPhoneVerification: l,
verifyEmail: a,
verifyPhone: d,
resendEmailVerification: t,
resendPhoneVerification: m,
isLoading: s,
error: n
} = P();
return {
isEmailVerified: e,
isPhoneVerified: h,
needsEmailVerification: f,
needsPhoneVerification: l,
verifyEmail: a,
verifyPhone: d,
resendEmailVerification: t,
resendPhoneVerification: m,
isLoading: s,
error: n,
needsVerification: f || l
};
}
function Q() {
const {
updateProfile: e,
changePassword: h,
deleteAccount: f,
updateEmail: l,
updatePhone: a,
updateProfileImage: d,
removeProfileImage: t,
updateMetadata: m,
isLoading: s,
error: n
} = P();
return {
updateProfile: e,
changePassword: h,
deleteAccount: f,
updateEmail: l,
updatePhone: a,
updateProfileImage: d,
removeProfileImage: t,
updateMetadata: m,
isLoading: s,
error: n
};
}
export {
P as useUser,
Q as useUserActions,
H as useUserProfile,
J as useUserVerification
};
//# sourceMappingURL=use-user.js.map