Cookies not saving
Hi,
I was trying to run elysiaJS as backend and then Astro as frontend, I need it like this as then I will implement another things, so I need a backend that is separated from everything. Anyways, I somehow managed to use the authClient.signIn.email and authClient.signUp.email, the thing is that when I log in, the cookies are not stored on the browser.
This is what I did.
On Astro:
auth-client.ts:
index.astro
Login.tsx:
Register.tsx:
I was trying to run elysiaJS as backend and then Astro as frontend, I need it like this as then I will implement another things, so I need a backend that is separated from everything. Anyways, I somehow managed to use the authClient.signIn.email and authClient.signUp.email, the thing is that when I log in, the cookies are not stored on the browser.
This is what I did.
On Astro:
auth-client.ts:
import { createAuthClient } from "better-auth/client"
import { createAuthClient as createAuthClientReact } from "better-auth/react"
const options = {
basePath: '/auth/api',
baseURL: 'https://api.soler.araarastudios.net',
cookieOptions: {
// Match the backend cookie settings
sameSite: 'none', // Allow cross-site cookies
secure: true, // Required for SameSite=None
path: '/',
},
}
export const authClient = createAuthClient({
...options
})
export const authClientReact = createAuthClientReact({
...options
})import { createAuthClient } from "better-auth/client"
import { createAuthClient as createAuthClientReact } from "better-auth/react"
const options = {
basePath: '/auth/api',
baseURL: 'https://api.soler.araarastudios.net',
cookieOptions: {
// Match the backend cookie settings
sameSite: 'none', // Allow cross-site cookies
secure: true, // Required for SameSite=None
path: '/',
},
}
export const authClient = createAuthClient({
...options
})
export const authClientReact = createAuthClientReact({
...options
})index.astro
---
import Layout from "../layouts/Layout.astro";
import "../styles/globals.css";
import Login from "../components/Login";
import Register from "../components/Register";
import { authClient } from "../lib/auth-client";
const session = await fetch("https://api.soler.araarastudios.net/auth/api", {
headers: Astro.request.headers,
});
const session2 = await authClient.getSession();
---
<Layout>
<p>session: {JSON.stringify(session)}</p>
<p>session2: {JSON.stringify(session2.data)}</p>
<Register client:load />
<Login client:load />
</Layout>---
import Layout from "../layouts/Layout.astro";
import "../styles/globals.css";
import Login from "../components/Login";
import Register from "../components/Register";
import { authClient } from "../lib/auth-client";
const session = await fetch("https://api.soler.araarastudios.net/auth/api", {
headers: Astro.request.headers,
});
const session2 = await authClient.getSession();
---
<Layout>
<p>session: {JSON.stringify(session)}</p>
<p>session2: {JSON.stringify(session2.data)}</p>
<Register client:load />
<Login client:load />
</Layout>Login.tsx:
import { authClient, authClientReact } from "../lib/auth-client"
export default function LogIn() {
async function handleClick() {
await authClient.signIn.email({
email: 'mayi@testemail.net',
password: '12345678910##AAaa'
});
const session = await authClient.getSession();
console.log(session);
}
return (
<button onClick={handleClick}>Log In</button>
)
}import { authClient, authClientReact } from "../lib/auth-client"
export default function LogIn() {
async function handleClick() {
await authClient.signIn.email({
email: 'mayi@testemail.net',
password: '12345678910##AAaa'
});
const session = await authClient.getSession();
console.log(session);
}
return (
<button onClick={handleClick}>Log In</button>
)
}Register.tsx:
import { authClientReact } from "../lib/auth-client"
export default function Register() {
async function handleClick() {
const session = await authClientReact.signUp.email({
email: 'mayi@testemail.net',
password: '12345678910##AAaa',
name: 'John Doe'
})
}
return (
<button onClick={handleClick}>Register</button>
)
}import { authClientReact } from "../lib/auth-client"
export default function Register() {
async function handleClick() {
const session = await authClientReact.signUp.email({
email: 'mayi@testemail.net',
password: '12345678910##AAaa',
name: 'John Doe'
})
}
return (
<button onClick={handleClick}>Register</button>
)
}