M_Cavus
M_Cavus
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
advanced: {
defaultCookieAttributes:
process.env.NODE_ENV === "production"
? {
sameSite: "none",
secure: true,
httpOnly: true,
partitioned: true, // That
}
: undefined,
},
advanced: {
defaultCookieAttributes:
process.env.NODE_ENV === "production"
? {
sameSite: "none",
secure: true,
httpOnly: true,
partitioned: true, // That
}
: undefined,
},
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
I found this not working because I had not added partitioned: true in defaultCookieAttributes
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
Yes, I've already configured trustedOrigins here's what I've put
trustedOrigins: [`${CLIENT_SIDE_URL}`],
trustedOrigins: [`${CLIENT_SIDE_URL}`],
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
Meaning?
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
Then I think it's more of a server-side problem, but I'll give it a try with useSession.
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
and my auth-client.ts
import { adminClient } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import {nextCookies} from "better-auth/next-js";

const CLIENT_SIDE_URL = process.env.NEXT_PUBLIC_CLIENT_SIDE_URL;

export const authClient = createAuthClient({
/** the base url of the server (optional if you're using the same domain) */
baseURL: CLIENT_SIDE_URL,
plugins: [adminClient(), nextCookies()],
});
import { adminClient } from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import {nextCookies} from "better-auth/next-js";

const CLIENT_SIDE_URL = process.env.NEXT_PUBLIC_CLIENT_SIDE_URL;

export const authClient = createAuthClient({
/** the base url of the server (optional if you're using the same domain) */
baseURL: CLIENT_SIDE_URL,
plugins: [adminClient(), nextCookies()],
});
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
This is my protected page
import { Header } from "@/components/dashboard/header/Header";
import { Call_Table } from "@/components/dashboard/table/Call_Table";
import { Footer } from "@/components/navigations/footer/Footer";
import { Navigation_Container } from "@/components/navigations/Navigation_Container";
import { authClient } from "@/lib/auth-client";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";

const BACKEN_URL = process.env.NEXT_PUBLIC_CLIENT_SIDE_URL;

interface User {
id: string;
name: string;
}

export default function Dashboard() {
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState(true);
const router = useRouter();

const fetchUserData = async () => {
try {
const response = await fetch(`${BACKEN_URL}/api/auth/get-session`, {
credentials: "include",
headers: {
"Content-Type": "application/json",
},
});

const data = await response.json();
console.log("Données utilisateur récupérées :", data);
setUser(data.user);
} catch (error) {
console.error("Erreur lors de la récupération des données.", error);
} finally {
setLoading(false);
}
};

useEffect(() => {
if (!user && !loading) {
router.push("/");
} else if (user && !loading) {
setLoading(false);
}
}, [user, loading]);

useEffect(() => {
if (!user) {
fetchUserData();
}
}, [user]);

const signOut = async () => {
try {
setLoading(true);
await authClient.signOut();
router.push("/");
} catch (error) {
console.error("Sign out error:", error);
}
};

return (
<section>
<Navigation_Container
userName={user?.name || "NaN"}
logOut={signOut}
loading={loading}
/>
<Header />
<Call_Table userId={user?.id ?? ""} />
<Footer />
</section>
);
}
import { Header } from "@/components/dashboard/header/Header";
import { Call_Table } from "@/components/dashboard/table/Call_Table";
import { Footer } from "@/components/navigations/footer/Footer";
import { Navigation_Container } from "@/components/navigations/Navigation_Container";
import { authClient } from "@/lib/auth-client";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";

const BACKEN_URL = process.env.NEXT_PUBLIC_CLIENT_SIDE_URL;

interface User {
id: string;
name: string;
}

export default function Dashboard() {
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState(true);
const router = useRouter();

const fetchUserData = async () => {
try {
const response = await fetch(`${BACKEN_URL}/api/auth/get-session`, {
credentials: "include",
headers: {
"Content-Type": "application/json",
},
});

const data = await response.json();
console.log("Données utilisateur récupérées :", data);
setUser(data.user);
} catch (error) {
console.error("Erreur lors de la récupération des données.", error);
} finally {
setLoading(false);
}
};

useEffect(() => {
if (!user && !loading) {
router.push("/");
} else if (user && !loading) {
setLoading(false);
}
}, [user, loading]);

useEffect(() => {
if (!user) {
fetchUserData();
}
}, [user]);

const signOut = async () => {
try {
setLoading(true);
await authClient.signOut();
router.push("/");
} catch (error) {
console.error("Sign out error:", error);
}
};

return (
<section>
<Navigation_Container
userName={user?.name || "NaN"}
logOut={signOut}
loading={loading}
/>
<Header />
<Call_Table userId={user?.id ?? ""} />
<Footer />
</section>
);
}
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
by adding sub-domains even the pc version on arc no longer works
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
here's my auth.js config on the backend
const { betterAuth } = require("better-auth");
const { admin } = require("better-auth/plugins");
const { prismaAdapter } = require("better-auth/adapters/prisma");
const prisma = require("../db/prisma");
const { nextCookies } = require("better-auth/next-js");

const CLIENT_SIDE_URL = process.env.BETTER_AUTH_URL;

const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
emailAndPassword: {
enabled: true,
},
advanced: {
defaultCookieAttributes:
process.env.NODE_ENV === "production"
? {
sameSite: "none",
secure: true,
}
: undefined,
},
trustedOrigins: [`${CLIENT_SIDE_URL}`],
plugins: [admin(), nextCookies()],
secret: process.env.BETTER_AUTH_SECRET,
baseURL: process.env.BETTER_AUTH_URL,
});

module.exports = { auth };
const { betterAuth } = require("better-auth");
const { admin } = require("better-auth/plugins");
const { prismaAdapter } = require("better-auth/adapters/prisma");
const prisma = require("../db/prisma");
const { nextCookies } = require("better-auth/next-js");

const CLIENT_SIDE_URL = process.env.BETTER_AUTH_URL;

const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
emailAndPassword: {
enabled: true,
},
advanced: {
defaultCookieAttributes:
process.env.NODE_ENV === "production"
? {
sameSite: "none",
secure: true,
}
: undefined,
},
trustedOrigins: [`${CLIENT_SIDE_URL}`],
plugins: [admin(), nextCookies()],
secret: process.env.BETTER_AUTH_SECRET,
baseURL: process.env.BETTER_AUTH_URL,
});

module.exports = { auth };
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
Yes I tried with incognito and even on other mobile but still no and strangely on pc with arc it works but not with safari
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
ah bas I got excited too early it works on ARC PC but when I switch to mobile it doesn't work the same way as safari on PC.
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
Thank you very much, you should have added the advenced parameter so that cookies can reach the frontend.
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
ok thanks for your help, i'll try to solve it if i can i'll give you the solution
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
what I find suspicious is that when I'm local and I go to the get-session api link, it displays the session correctly, whereas in production it returns null, and even worse, there's no error that could facilitate debugging.
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
no it says everything is ok
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
No description
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
No description
45 replies
BABetter Auth
Created by M_Cavus on 5/8/2025 in #help
api/auth/get-session returns null in prod but works locally
when I go to the url localhost:8080/api/auth/get-session it sends me back
{
“session": {
“id": ‘h8BWGy8cuJK5ymrdvkaYOj8DqKwKdkZ4’,
“expiresAt": ‘2025-05-15T17:52:49.661Z’,
“token": ‘lnQcBgIeRif5T5kM1FEPjd2Lf6Sg4dKdf’,
“createdAt": ‘2025-05-08T17:52:49.661Z’,
“updatedAt": ‘2025-05-08T17:52:49.661Z’,
“ipAddress": ‘’,
“userAgent": ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36’,
“userId": ”vWOE8LJWO5oKiPY5KIwVWjbGiVyc4t9q”
},
“user": {
.....
}
}
{
“session": {
“id": ‘h8BWGy8cuJK5ymrdvkaYOj8DqKwKdkZ4’,
“expiresAt": ‘2025-05-15T17:52:49.661Z’,
“token": ‘lnQcBgIeRif5T5kM1FEPjd2Lf6Sg4dKdf’,
“createdAt": ‘2025-05-08T17:52:49.661Z’,
“updatedAt": ‘2025-05-08T17:52:49.661Z’,
“ipAddress": ‘’,
“userAgent": ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36’,
“userId": ”vWOE8LJWO5oKiPY5KIwVWjbGiVyc4t9q”
},
“user": {
.....
}
}
but when I'm in production it gives me a blank page. Of course with the https link instead of localhost
45 replies