Skip to content

Commit 50a3e39

Browse files
committed
feat: use built-in BA last login method
1 parent 85e4dee commit 50a3e39

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

apps/dashboard/app/(auth)/login/page.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { signIn } from "@databuddy/auth/client";
3+
import { authClient, signIn } from "@databuddy/auth/client";
44
import {
55
EyeIcon,
66
EyeSlashIcon,
@@ -12,7 +12,7 @@ import {
1212
import Link from "next/link";
1313
import { useRouter } from "next/navigation";
1414
import { parseAsString, useQueryState } from "nuqs";
15-
import { Suspense, useEffect, useState } from "react";
15+
import { Suspense, useState } from "react";
1616
import { toast } from "sonner";
1717
import { Badge } from "@/components/ui/badge";
1818
import { Button } from "@/components/ui/button";
@@ -30,13 +30,9 @@ function LoginPage() {
3030
const [email, setEmail] = useState("");
3131
const [password, setPassword] = useState("");
3232
const [showPassword, setShowPassword] = useState(false);
33-
const [lastUsed, setLastUsed] = useState<string | null>(null);
3433

3534
const defaultCallbackUrl = callback;
36-
37-
useEffect(() => {
38-
setLastUsed(localStorage.getItem("lastUsedLogin"));
39-
}, []);
35+
const lastUsed = authClient.getLastUsedLoginMethod();
4036

4137
const handleSocialLogin = (provider: "github" | "google") => {
4238
setIsLoading(true);
@@ -50,7 +46,6 @@ function LoginPage() {
5046
newUserCallbackURL: "/onboarding",
5147
fetchOptions: {
5248
onSuccess: () => {
53-
localStorage.setItem("lastUsedLogin", provider);
5449
if (callbackUrl) {
5550
router.push(callbackUrl);
5651
}
@@ -80,7 +75,6 @@ function LoginPage() {
8075
callbackURL: defaultCallbackUrl,
8176
fetchOptions: {
8277
onSuccess: () => {
83-
localStorage.setItem("lastUsedLogin", "email");
8478
const callbackUrl = callback;
8579
if (callbackUrl) {
8680
router.push(callbackUrl);
@@ -172,7 +166,7 @@ function LoginPage() {
172166
Sign in with Magic Link
173167
</Link>
174168
</Button>
175-
{lastUsed === "magic" && (
169+
{lastUsed === "magic-link" && (
176170
<Badge
177171
className="-top-3 -right-0.5 absolute z-10 rounded-full px-1 py-0 text-[10px]"
178172
variant="secondary"

packages/auth/src/auth.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { drizzleAdapter } from "better-auth/adapters/drizzle";
1414
import {
1515
customSession,
1616
emailOTP,
17+
lastLoginMethod,
1718
magicLink,
1819
organization,
1920
twoFactor,
@@ -163,6 +164,14 @@ export const auth = betterAuth({
163164
},
164165
},
165166
plugins: [
167+
lastLoginMethod({
168+
customResolveMethod: (ctx) => {
169+
if (ctx.path === "/magic-link/verify" || ctx.path?.includes("/magic-link")) {
170+
return "magic-link";
171+
}
172+
return null;
173+
},
174+
}),
166175
emailOTP({
167176
async sendVerificationOTP({ email, otp, type }) {
168177
const resend = new Resend(process.env.RESEND_API_KEY as string);

packages/auth/src/client/auth-client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
customSessionClient,
44
emailOTPClient,
55
genericOAuthClient,
6+
lastLoginMethodClient,
67
magicLinkClient,
78
multiSessionClient,
89
organizationClient,
@@ -31,6 +32,7 @@ export const authClient = createAuthClient({
3132
genericOAuthClient(),
3233
emailOTPClient(),
3334
magicLinkClient(),
35+
lastLoginMethodClient(),
3436
ssoClient(),
3537
organizationClient({
3638
ac,

0 commit comments

Comments
 (0)