Skip to content

Commit 2d13e71

Browse files
committed
feat: enhance authentication and cookie handling
- Added rewrites in next.config.ts to proxy API requests to the backend. - Introduced authCookies utility for forwarding cookies from Next.js to the backend for authentication. - Implemented nextCookiesRequest plugin to manage cookie headers during requests. - Updated auth-client.ts to include nextCookiesRequest for improved session management.
1 parent 6351108 commit 2d13e71

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

lib/auth-client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
oneTapClient,
77
organizationClient,
88
} from 'better-auth/client/plugins';
9+
import { nextCookiesRequest } from './next-cookies-request';
910

1011
const getAuthBaseURL = () => {
1112
const apiUrl =
@@ -17,6 +18,7 @@ const getAuthBaseURL = () => {
1718
export const authClient = createAuthClient({
1819
baseURL: getAuthBaseURL(),
1920
plugins: [
21+
nextCookiesRequest,
2022
inferAdditionalFields({
2123
user: {
2224
profile: { type: 'json', required: false },

lib/auth-cookies.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Cookie forwarding utilities for Better Auth with separate backend setup
3+
*
4+
* This module provides helpers to forward cookies from Next.js server-side requests
5+
* to your backend API for proper authentication handling.
6+
*
7+
* Usage in server components/actions:
8+
* ```typescript
9+
* const { data: session } = await authClient.getSession(await authCookies());
10+
* ```
11+
*
12+
* Backend Configuration Requirements:
13+
* Your backend must include your frontend in `trustedOrigins`:
14+
* ```typescript
15+
* export const auth = betterAuth({
16+
* trustedOrigins: ['http://localhost:3000'] // Your frontend URL
17+
* });
18+
* ```
19+
*
20+
* OAuth Configuration:
21+
* Configure redirectURI to point to your backend:
22+
* ```typescript
23+
* socialProviders: {
24+
* google: {
25+
* redirectURI: "http://localhost:3001/api/auth/callback/google" // Backend URL
26+
* }
27+
* }
28+
* ```
29+
*
30+
* When calling authClient.signIn.social, set callbackURL to your frontend URL.
31+
*/
32+
33+
import { cookies } from 'next/headers';
34+
35+
export async function authCookies() {
36+
const cookie = await cookies();
37+
return {
38+
fetchOptions: {
39+
headers: {
40+
Cookie: cookie.toString(),
41+
},
42+
},
43+
};
44+
}

lib/next-cookies-request.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const nextCookiesRequest = {
2+
id: 'next-cookies-request',
3+
fetchPlugins: [
4+
{
5+
id: 'next-cookies-request-plugin',
6+
name: 'next-cookies-request-plugin',
7+
hooks: {
8+
async onRequest(ctx: any) {
9+
if (typeof window === 'undefined') {
10+
const { cookies } = await import('next/headers');
11+
const headers = await cookies();
12+
ctx.headers.set('cookie', headers.toString());
13+
}
14+
},
15+
},
16+
},
17+
],
18+
};

next.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import type { NextConfig } from 'next';
22

33
const nextConfig: NextConfig = {
44
outputFileTracingRoot: __dirname,
5+
async rewrites() {
6+
return [
7+
{
8+
source: '/api/:path*',
9+
destination: process.env.NEXT_PUBLIC_API_URL + '/api/:path*', // Proxy to Backend
10+
},
11+
];
12+
},
513
images: {
614
remotePatterns: [
715
{

0 commit comments

Comments
 (0)