File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66 oneTapClient ,
77 organizationClient ,
88} from 'better-auth/client/plugins' ;
9+ import { nextCookiesRequest } from './next-cookies-request' ;
910
1011const getAuthBaseURL = ( ) => {
1112 const apiUrl =
@@ -17,6 +18,7 @@ const getAuthBaseURL = () => {
1718export const authClient = createAuthClient ( {
1819 baseURL : getAuthBaseURL ( ) ,
1920 plugins : [
21+ nextCookiesRequest ,
2022 inferAdditionalFields ( {
2123 user : {
2224 profile : { type : 'json' , required : false } ,
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change @@ -2,6 +2,14 @@ import type { NextConfig } from 'next';
22
33const 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 {
You can’t perform that action at this time.
0 commit comments