Nextjs server component 401 #1189

Closed
opened 2026-03-13 08:27:12 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @gyanendrasng on GitHub (May 10, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Using express backend with drizzle

my auth middleware
import { NextFunction, Request, Response } from 'express';
import { auth } from '../lib/auth';
import { fromNodeHeaders } from 'better-auth/node';

export const authMiddleware = async (
req: Request,
res: Response,
next: NextFunction
): Promise => {
const session = await auth.api.getSession({
headers: fromNodeHeaders(req.headers),
});

if (!session || !session.user) {
res.status(401).json({ message: 'Unauthorized' });
return;
}

req.user_id = session?.user?.id;
next();
};

My apis arent being sent from frontend to backend, in nextjs, tried using with crenetials also

export const axiosInstance = axios.create({
baseURL,
headers: {
'Content-Type': 'application/json',
},
withCredentials: true,
});

By default my page is a server component-
export default async function DashboardPage() {

const videos = await getVideosFetcher();
console.log('videos', videos);

return (


<Button className={cn(styles.addVideoButton, 'button-primary')}>
+ Create Video


{/* {videos.map((video, idx) => (

))} */}


);
}

Current vs. Expected behavior

To validate request and return variable, but gets 401 unautrized

What version of Better Auth are you using?

1.2.7

Provide environment information

Macos chrome
node v20.17.0

Which area(s) are affected? (Select all that apply)

Client

Auth config (if applicable)

My backend auth setup is
export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: 'pg',
    // debugLogs: true,
    usePlural: true,
  }),
  emailAndPassword: {
    enabled: true,
    autoSignIn: true,
  },
  // emailVerification: {
  //   sendOnSignUp: true,
  // },
  logger: {
    level: 'debug',
    transport: {
      type: 'console',
      options: {
        format: 'json',
      },
    },
  },
  trustedOrigins: ['https://localhost:3000'],
  advanced: {
    defaultCookieAttributes: {
      sameSite: 'none',
      secure: true,
    },
  },
  crossSubDomainCookies: {
    enabled: true,
    domain: 'https://localhost:3000', // Domain with a leading period
  },
});

Additional context

No response

Originally created by @gyanendrasng on GitHub (May 10, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Using express backend with drizzle my auth middleware import { NextFunction, Request, Response } from 'express'; import { auth } from '../lib/auth'; import { fromNodeHeaders } from 'better-auth/node'; export const authMiddleware = async ( req: Request, res: Response, next: NextFunction ): Promise<void> => { const session = await auth.api.getSession({ headers: fromNodeHeaders(req.headers), }); if (!session || !session.user) { res.status(401).json({ message: 'Unauthorized' }); return; } req.user_id = session?.user?.id; next(); }; My apis arent being sent from frontend to backend, in nextjs, tried using with crenetials also export const axiosInstance = axios.create({ baseURL, headers: { 'Content-Type': 'application/json', }, withCredentials: true, }); By default my page is a server component- export default async function DashboardPage() { const videos = await getVideosFetcher(); console.log('videos', videos); return ( <section className={styles.dashboardPage}> <Button className={cn(styles.addVideoButton, 'button-primary')}> + Create Video </Button> <section className={styles.videosGrid}> {/* {videos.map((video, idx) => ( <VideoCard key={idx} data={video}></VideoCard> ))} */} </section> </section> ); } ### Current vs. Expected behavior To validate request and return variable, but gets 401 unautrized ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash Macos chrome node v20.17.0 ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript My backend auth setup is export const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'pg', // debugLogs: true, usePlural: true, }), emailAndPassword: { enabled: true, autoSignIn: true, }, // emailVerification: { // sendOnSignUp: true, // }, logger: { level: 'debug', transport: { type: 'console', options: { format: 'json', }, }, }, trustedOrigins: ['https://localhost:3000'], advanced: { defaultCookieAttributes: { sameSite: 'none', secure: true, }, }, crossSubDomainCookies: { enabled: true, domain: 'https://localhost:3000', // Domain with a leading period }, }); ``` ### Additional context _No response_
Author
Owner

@Kinfe123 commented on GitHub (May 12, 2025):

the trustedOrigins here on your config is showing - https://localhost:3000 can you change it to http

@Kinfe123 commented on GitHub (May 12, 2025): the `trustedOrigins` here on your config is showing - `https://localhost:3000` can you change it to `http`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1189