[GH-ISSUE #1429] How do I access user session from another server? #8748

Closed
opened 2026-04-13 03:56:46 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @Rakhsan on GitHub (Feb 13, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1429

I am using express for the api server and nextjs for the frontend server. I need to get the user session in SSR nextjs how do I get that?

Originally created by @Rakhsan on GitHub (Feb 13, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1429 I am using express for the api server and nextjs for the frontend server. I need to get the user session in SSR nextjs how do I get that?
GiteaMirror added the locked label 2026-04-13 03:56:46 -05:00
Author
Owner

@jslno commented on GitHub (Feb 13, 2025):

Just call getSession and make sure you have the correct trustedOrigins set. You may also have to enable crossSubDomainCookie and maybe have to define a domain for production.

Define the betterAuth() inside the express server and createAuthClient() inside the nextjs app. Also make sure to set the correct baseURL

<!-- gh-comment-id:2656462431 --> @jslno commented on GitHub (Feb 13, 2025): Just call `getSession` and make sure you have the correct `trustedOrigins` set. You may also have to enable `crossSubDomainCookie` and maybe have to define a domain for production. Define the `betterAuth()` inside the express server and `createAuthClient()` inside the nextjs app. Also make sure to set the correct `baseURL`
Author
Owner

@Rakhsan commented on GitHub (Feb 13, 2025):

Just call getSession and make sure you have the correct trustedOrigins set. You may also have to enable crossSubDomainCookie and maybe have to define a domain for production.

Define the betterAuth() inside the express server and createAuthClient() inside the nextjs app. Also make sure to set the correct baseURL

import React from 'react';
import Link from 'next/link';
import { authClient } from "@/lib/auth-client"
import { toast } from 'sonner';

const handleSignOut = async () => {
try {
await authClient.signOut()
toast.success("Signed out successfully")
window.location.reload()
} catch {
console.error("Something bad happended")
}
}

const AuthPage = async () => {
const session = await authClient.getSession()
console.log(session)

return (
<div className="min-h-screen bg-gray-900 text-white flex flex-col items-center justify-center p-4">
<div className="text-center">
<h1 className="text-4xl font-bold mb-4">Welcome to Our Platform</h1>
<p className="text-gray-400 mb-8">Choose an option to get started</p>
</div>
<div className="space-y-4">
{!session ? (
<button
onClick={handleSignOut}
className="bg-red-600 hover:bg-red-700 text-white font-semibold py-3 px-6 rounded-lg shadow-lg transition duration-300 ease-in-out transform hover:scale-105 cursor-pointer text-center">
Sign out
</button>
) : (
<>
<Link href="/auth/login">
<div className="bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 px-6 rounded-lg shadow-lg transition duration-300 ease-in-out transform hover:scale-105 cursor-pointer text-center">
Login
</div>
</Link>
<Link href="/auth/register">
<div className="mt-3 bg-purple-600 hover:bg-purple-700 text-white font-semibold py-3 px-6 rounded-lg shadow-lg t

);
};

export default AuthPage;

THIS THING IS NOT WORKING?

<!-- gh-comment-id:2656687699 --> @Rakhsan commented on GitHub (Feb 13, 2025): > Just call `getSession` and make sure you have the correct `trustedOrigins` set. You may also have to enable `crossSubDomainCookie` and maybe have to define a domain for production. > > Define the `betterAuth()` inside the express server and `createAuthClient()` inside the nextjs app. Also make sure to set the correct `baseURL` import React from 'react'; import Link from 'next/link'; import { authClient } from "@/lib/auth-client" import { toast } from 'sonner'; const handleSignOut = async () => { try { await authClient.signOut() toast.success("Signed out successfully") window.location.reload() } catch { console.error("Something bad happended") } } const AuthPage = async () => { const session = await authClient.getSession() console.log(session) return ( &lt;div className="min-h-screen bg-gray-900 text-white flex flex-col items-center justify-center p-4"&gt; &lt;div className="text-center"&gt; &lt;h1 className="text-4xl font-bold mb-4"&gt;Welcome to Our Platform&lt;/h1&gt; &lt;p className="text-gray-400 mb-8"&gt;Choose an option to get started&lt;/p&gt; &lt;/div&gt; &lt;div className="space-y-4"&gt; {!session ? ( &lt;button onClick={handleSignOut} className="bg-red-600 hover:bg-red-700 text-white font-semibold py-3 px-6 rounded-lg shadow-lg transition duration-300 ease-in-out transform hover:scale-105 cursor-pointer text-center"&gt; Sign out &lt;/button&gt; ) : ( &lt;&gt; &lt;Link href="/auth/login"&gt; &lt;div className="bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 px-6 rounded-lg shadow-lg transition duration-300 ease-in-out transform hover:scale-105 cursor-pointer text-center"&gt; Login &lt;/div&gt; &lt;/Link&gt; &lt;Link href="/auth/register"&gt; &lt;div className="mt-3 bg-purple-600 hover:bg-purple-700 text-white font-semibold py-3 px-6 rounded-lg shadow-lg t ); }; export default AuthPage; **THIS THING IS NOT WORKING?**
Author
Owner

@jslno commented on GitHub (Feb 13, 2025):

Whats the error?

<!-- gh-comment-id:2656784887 --> @jslno commented on GitHub (Feb 13, 2025): Whats the error?
Author
Owner

@Rakhsan commented on GitHub (Feb 13, 2025):

there is no error just that the session is empty { session: null, user: null }

<!-- gh-comment-id:2656796305 --> @Rakhsan commented on GitHub (Feb 13, 2025): there is no error just that the session is empty { session: null, user: null }
Author
Owner

@jslno commented on GitHub (Feb 13, 2025):

Can you try passing the headers to fetchOptions

<!-- gh-comment-id:2656818624 --> @jslno commented on GitHub (Feb 13, 2025): Can you try passing the headers to fetchOptions
Author
Owner

@Rakhsan commented on GitHub (Feb 13, 2025):

wow thanks man it just work this way:

import { authClient } from "@/lib/auth-client"
import { headers } from 'next/headers';
const session = await authClient.getSession({ fetchOptions: { headers: await headers() } })

<!-- gh-comment-id:2656841149 --> @Rakhsan commented on GitHub (Feb 13, 2025): wow thanks man it just work this way: import { authClient } from "@/lib/auth-client" import { headers } from 'next/headers'; const session = await authClient.getSession({ fetchOptions: { headers: await headers() } })
Author
Owner

@mrinmay7875 commented on GitHub (Mar 2, 2025):

Thanks both, I was able facing the same and it works for me now.

<!-- gh-comment-id:2692623327 --> @mrinmay7875 commented on GitHub (Mar 2, 2025): Thanks both, I was able facing the same and it works for me now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8748