What info does server getSession need?
Hi folks, I am trying to use Elysia with my Vite app. All the "provided" endpoints that pass through the basic middleware like sign in work:
Without using bearer token, is the below pattern possible, or does bearer need to be enabled?
If possible without bearer token, how do we call custom endpoints but sending session info?
Config, for reference:
async function betterAuthMiddleware(context: Context) {
return await auth.handler(context.request);
}
export const app = new Elysia({ prefix: "/api" })
.state(state)
.use(cors())
.all("/*", ({ request }) => {
console.log("request", request);
})
.all("/auth/*", betterAuthMiddleware)
async function betterAuthMiddleware(context: Context) {
return await auth.handler(context.request);
}
export const app = new Elysia({ prefix: "/api" })
.state(state)
.use(cors())
.all("/*", ({ request }) => {
console.log("request", request);
})
.all("/auth/*", betterAuthMiddleware)
// client
const serverClient = treaty<App>("http://localhost:7505", {
onRequest: async (_path, options) => {
const session = await authClient.getSession();
if (session.data?.session) {
return {
headers: {
...options.headers,
// IS THIS RIGHT? i'm starting to think no?
Authorization: `${session.data.session.token}`,
},
};
}
return options.headers;
},
});
// server
new Elysia()
.derive(async ({ request }) => {
const session = await auth.api.getSession({ headers: request.headers });
// session.data is always null/undefined here even with a valid auth token
if (!session || !session.user || !session.session) {
...
}
// client
const serverClient = treaty<App>("http://localhost:7505", {
onRequest: async (_path, options) => {
const session = await authClient.getSession();
if (session.data?.session) {
return {
headers: {
...options.headers,
// IS THIS RIGHT? i'm starting to think no?
Authorization: `${session.data.session.token}`,
},
};
}
return options.headers;
},
});
// server
new Elysia()
.derive(async ({ request }) => {
const session = await auth.api.getSession({ headers: request.headers });
// session.data is always null/undefined here even with a valid auth token
if (!session || !session.user || !session.session) {
...
}
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
usePlural: true,
}),
secret: process.env.BETTER_AUTH_SECRET,
trustedOrigins: ["http://localhost:3001"],
emailAndPassword: {
enabled: true,
},
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async (
{ user, newEmail, url, token },
request,
) => {
console.log("sendChangeEmailVerification", user, newEmail, url, token);
},
},
},
plugins: [
organization({
...
}),
],
});
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
usePlural: true,
}),
secret: process.env.BETTER_AUTH_SECRET,
trustedOrigins: ["http://localhost:3001"],
emailAndPassword: {
enabled: true,
},
user: {
changeEmail: {
enabled: true,
sendChangeEmailVerification: async (
{ user, newEmail, url, token },
request,
) => {
console.log("sendChangeEmailVerification", user, newEmail, url, token);
},
},
},
plugins: [
organization({
...
}),
],
});
0 Replies