using nexjs as a frontend and expressjs as a backend

I have my auth.ts on my expressjs backend and my auth-client.ts in my nextjs frontend but I want to get user info in my nextjs on server side can I add a second auth.ts file in my nextjs frontend so I will be able to use auth.api.getSession() in server side on my nextjs frontend while keeping the principal auth.ts in my beckend (expressjs) ?
8 Replies
Voltab
VoltabOP2mo ago
@nikatune do you know the answer ?
nikatune
nikatune2mo ago
is it expressjs is neccesary for your project? maybe you should use api routes on Next.js
Voltab
VoltabOP2mo ago
the thing is that I have a mobile app with expo that use the expressjs backend too
Gagan Kumar
Gagan Kumar2mo ago
Yep mine too. Let's Say have a laravel API using Sanctum for authentication, and my frontend is built with Next.js using Better Auth, How does Better Auth handle API credentials in this case, especially the Sanctum token? Is there a way to store and manage the token so it's available across all Better Auth sessions, for example during SSR or protected routes? I'd really appreciate any guidance or best practices for this setup. Thanks!
nikatune
nikatune2mo ago
It would be better if @Ping answers he knows better than me
Gagan Kumar
Gagan Kumar2mo ago
Hey @Ping 👋🏻, Need your expertise on the above questions. Please you can shed some light on this, would help a lot. 🙏🏻🙏🏻
sebastian
sebastian2mo ago
import { cookies } from "next/headers";
import { Session } from "./auth-client";

const getServerSession = async (): Promise<Session | null> => {
try {
const cookieHeader = (await cookies()).toString();

const res = await fetch(`${process.env.BETTER_AUTH_URL}/v1/auth/get-session`, {
credentials: "include",
headers: {
Cookie: cookieHeader,
},
});

return res.json();

} catch (error) {
console.error(error);
return null;
}
};

export default getServerSession;
import { cookies } from "next/headers";
import { Session } from "./auth-client";

const getServerSession = async (): Promise<Session | null> => {
try {
const cookieHeader = (await cookies()).toString();

const res = await fetch(`${process.env.BETTER_AUTH_URL}/v1/auth/get-session`, {
credentials: "include",
headers: {
Cookie: cookieHeader,
},
});

return res.json();

} catch (error) {
console.error(error);
return null;
}
};

export default getServerSession;
this is the code to get the session on server stop spamming (making 6 threads) for no reason at all @Gab. Just read the docs, it's not that hard
Voltab
VoltabOP2mo ago
thank's ! sorry didn't know this was the solutions

Did you find this page helpful?