vijayatk3
vijayatk3
BABetter Auth
Created by vijayatk3 on 4/17/2025 in #help
How to set Bearer token in Next.js
Hello Everyone,
import { createAuthClient } from 'better-auth/react';

export const {
signIn,
signUp,
signOut,
useSession,
sendVerificationEmail,
forgetPassword,
resetPassword,
} = createAuthClient({
fetchOptions: {
auth: {
type: "Bearer",
token: () => localStorage.getItem("bearer_token") || ""
},
onSuccess: (ctx) => {
const authToken = ctx.response.headers.get('set-auth-token');
if (authToken) {
localStorage.setItem('bearer_token', authToken);
}
},
},
});
import { createAuthClient } from 'better-auth/react';

export const {
signIn,
signUp,
signOut,
useSession,
sendVerificationEmail,
forgetPassword,
resetPassword,
} = createAuthClient({
fetchOptions: {
auth: {
type: "Bearer",
token: () => localStorage.getItem("bearer_token") || ""
},
onSuccess: (ctx) => {
const authToken = ctx.response.headers.get('set-auth-token');
if (authToken) {
localStorage.setItem('bearer_token', authToken);
}
},
},
});
But I'm getting this error:
ReferenceError: localStorage is not defined
ReferenceError: localStorage is not defined
I believe it's because auth-client.ts is being run on the server side, where localStorage is unavailable. Any help is appreciated!
11 replies
BABetter Auth
Created by vijayatk3 on 4/8/2025 in #help
forgot password with Email OTP
Hi everyone! I'm working on a forgot password feature using Next.js, Prisma, and PostgreSQL. When I submit the email on the forgot password page, and the email doesn't exist in the database, it doesn't show an error like "User email doesn't exist." It just silently fails. Here's the code snippet I'm using:
const { data, error } = await emailOtp.sendVerificationOtp({
email: values.email,
type: 'forget-password',
fetchOptions: {
onSuccess: (ctx) => {
console.log(ctx.data, 'Success');
},
onError: (ctx) => {
console.log(ctx.error.message, 'Error');
},
}
});
const { data, error } = await emailOtp.sendVerificationOtp({
email: values.email,
type: 'forget-password',
fetchOptions: {
onSuccess: (ctx) => {
console.log(ctx.data, 'Success');
},
onError: (ctx) => {
console.log(ctx.error.message, 'Error');
},
}
});
Does anyone know how I can handle this and show an appropriate error message when the email is not found? Any help would be appreciated!
2 replies