vick25
vick25
BABetter Auth
Created by vz on 4/4/2025 in #help
useSession returns null
@Ping 😆 Awesome. It works. You made my day. This is my implementation. Any refactoring?
async function handleSubmit(formdata: FormData) {
'use server'
const code = formdata.get('code')
const email = loggedInEmail

// Sign in with emailed OTP
const data = await auth.api.signInEmailOTP(
{
asResponse: true,
body: {
email: (email as string).trim(),
otp: (code as string).trim(),
},
headers: await headers(),
}
)
console.log(data)
if (!data) {
console.error("Error verifying OTP:")
if (code === '')
redirect(`/historical`)
return
}
if (data) {
// console.log("OTP verified successfully")
redirect(`/historical?signup=success`)
}
redirect(`/historical`)
}
async function handleSubmit(formdata: FormData) {
'use server'
const code = formdata.get('code')
const email = loggedInEmail

// Sign in with emailed OTP
const data = await auth.api.signInEmailOTP(
{
asResponse: true,
body: {
email: (email as string).trim(),
otp: (code as string).trim(),
},
headers: await headers(),
}
)
console.log(data)
if (!data) {
console.error("Error verifying OTP:")
if (code === '')
redirect(`/historical`)
return
}
if (data) {
// console.log("OTP verified successfully")
redirect(`/historical?signup=success`)
}
redirect(`/historical`)
}
30 replies
BABetter Auth
Created by vz on 4/4/2025 in #help
useSession returns null
Thanks @Ping ! I'm working on it and will be back if it works
30 replies
BABetter Auth
Created by vz on 4/4/2025 in #help
useSession returns null
Hello! I have a similar issue. The OTP is sent and the sign in works. The user and a session is created in the database but no cookies and the useSession is null. I'm using a server action to sign-in that looks like :
async function handleSubmit(formdata: FormData) {
'use server'
const code = formdata.get('code')
const email = loggedInEmail //props

// Sign in with emailed OTP
const { data, error } = await authClient.signIn.emailOtp({
email: (email as string).trim(),
otp: (code as string).trim()
})
if (error) {
console.error("Error verifying OTP:", error)
if (code === '')
redirect(`/historical`)
return
}
if (data.token) {
// console.log("OTP verified successfully")
redirect(`/historical?signup=success`)
}
redirect(`/historical`)
}
async function handleSubmit(formdata: FormData) {
'use server'
const code = formdata.get('code')
const email = loggedInEmail //props

// Sign in with emailed OTP
const { data, error } = await authClient.signIn.emailOtp({
email: (email as string).trim(),
otp: (code as string).trim()
})
if (error) {
console.error("Error verifying OTP:", error)
if (code === '')
redirect(`/historical`)
return
}
if (data.token) {
// console.log("OTP verified successfully")
redirect(`/historical?signup=success`)
}
redirect(`/historical`)
}
My auth.ts looks like :
export const auth = betterAuth({
database: prismaAdapter(Prisma, {
provider: "postgresql",
}),
advanced: {
useSecureCookies: process.env.NODE_ENV === "production",
},
session: {
expiresIn: 60 * 60 * 24 * 7,
updateAge: 60 * 60 * 24,
cookieCache: {
enabled: true,
maxAge: 5 * 60 // Cache duration in seconds
}
},
emailAndPassword: {
enabled: true,
// requireEmailVerification: true
},
plugins: [
emailOTP({
expiresIn: 600,
async sendVerificationOTP({ email, otp, type }) {
}),
openAPI(),
nextCookies(),
],
});
export type Session = typeof auth.$Infer.Session;
export const auth = betterAuth({
database: prismaAdapter(Prisma, {
provider: "postgresql",
}),
advanced: {
useSecureCookies: process.env.NODE_ENV === "production",
},
session: {
expiresIn: 60 * 60 * 24 * 7,
updateAge: 60 * 60 * 24,
cookieCache: {
enabled: true,
maxAge: 5 * 60 // Cache duration in seconds
}
},
emailAndPassword: {
enabled: true,
// requireEmailVerification: true
},
plugins: [
emailOTP({
expiresIn: 600,
async sendVerificationOTP({ email, otp, type }) {
}),
openAPI(),
nextCookies(),
],
});
export type Session = typeof auth.$Infer.Session;
And the authClient as :
export const authClient = createAuthClient({
baseURL: process.env.BETTER_AUTH_URL,
plugins: [
emailOTPClient()
]
});
export const authClient = createAuthClient({
baseURL: process.env.BETTER_AUTH_URL,
plugins: [
emailOTPClient()
]
});
30 replies