Skip to content

Commit 42667fa

Browse files
committed
feat(auth): magic link with better-auth
1 parent e9b2a9d commit 42667fa

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

web/src/lib/auth-client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
import { createAuthClient } from 'better-auth/react'
2+
import { magicLinkClient } from 'better-auth/client/plugins'
3+
4+
export const authClient = createAuthClient({
5+
plugins: [
6+
magicLinkClient()
7+
]
8+
})
29

3-
export const authClient = createAuthClient()

web/src/lib/auth.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import { betterAuth } from 'better-auth'
2+
import { magicLink } from 'better-auth/plugins'
23
import { tanstackStartCookies } from 'better-auth/tanstack-start'
34

45
export const auth = betterAuth({
6+
secret: process.env.BETTER_AUTH_SECRET || "dev-secret-key-at-least-32-chars-long-exemplai",
57
emailAndPassword: {
68
enabled: true,
79
},
8-
plugins: [tanstackStartCookies()],
10+
plugins: [
11+
tanstackStartCookies(),
12+
magicLink({
13+
sendMagicLink: async ({ email, url }) => {
14+
console.log(`\n==================================================`)
15+
console.log(`[Dev Mailer] Magic Link for: ${email}`)
16+
console.log(`URL: ${url}`)
17+
console.log(`==================================================\n`)
18+
},
19+
}),
20+
],
921
})
22+

web/src/routes/auth.tsx

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,15 @@ function AuthPage() {
5656
// Log magic link request to the terminal/console
5757
console.log(`[Auth] Magic Link Sign In initiated for email: "${value.email}"`);
5858

59-
if (
60-
authClient.signIn &&
61-
"magicLink" in authClient.signIn &&
62-
typeof (authClient.signIn as any).magicLink === "function"
63-
) {
64-
const { error } = await (authClient.signIn as any).magicLink({
65-
email: value.email,
66-
callbackURL: "/",
67-
});
68-
if (error) {
69-
setGlobalError(error.message || "Failed to send magic link");
70-
} else {
71-
setSuccessMessage("Magic link sent! Please check your email inbox.");
72-
}
59+
const { error } = await authClient.signIn.magicLink({
60+
email: value.email,
61+
callbackURL: "/",
62+
});
63+
if (error) {
64+
setGlobalError(error.message || "Failed to send magic link");
7365
} else {
74-
// Log warning & simulate success response to developer/terminal
75-
console.warn(
76-
"[Auth] better-auth magicLink plugin is not configured. Simulating success."
77-
);
7866
setSuccessMessage(
79-
`Magic link has been simulated & logged to terminal! (Email: ${value.email})`
67+
"Magic link sent! Please check your email inbox."
8068
);
8169
}
8270
} else {
@@ -369,6 +357,7 @@ function AuthPage() {
369357
</form.Subscribe>
370358
</form>
371359
</AuthCard>
360+
372361
</div>
373362
);
374363
}

0 commit comments

Comments
 (0)