@better-auth/expo
Version:
Better Auth integration for Expo and React Native applications.
46 lines (45 loc) • 1.42 kB
JavaScript
//#region src/plugins/last-login-method.ts
const paths = [
"/callback/",
"/oauth2/callback/",
"/sign-in/email",
"/sign-up/email"
];
const defaultResolveMethod = (url) => {
const { pathname } = new URL(url.toString(), "http://localhost");
if (paths.some((p) => pathname.includes(p))) return pathname.split("/").pop();
if (pathname.includes("siwe")) return "siwe";
if (pathname.includes("/passkey/verify-authentication")) return "passkey";
};
const lastLoginMethodClient = (config) => {
const resolveMethod = config.customResolveMethod || defaultResolveMethod;
const lastLoginMethodName = `${config.storagePrefix || "better-auth"}_last_login_method`;
const storage = config.storage;
return {
id: "last-login-method-expo",
fetchPlugins: [{
id: "last-login-method-expo",
name: "Last Login Method",
hooks: { onResponse: async (ctx) => {
const lastMethod = await resolveMethod(ctx.request.url);
if (!lastMethod) return;
await storage.setItem(lastLoginMethodName, lastMethod);
} }
}],
getActions() {
return {
getLastUsedLoginMethod: () => {
return storage.getItem(lastLoginMethodName);
},
clearLastUsedLoginMethod: async () => {
await storage.deleteItemAsync(lastLoginMethodName);
},
isLastUsedLoginMethod: (method) => {
return storage.getItem(lastLoginMethodName) === method;
}
};
}
};
};
//#endregion
export { lastLoginMethodClient };