Better Auth Fastify Integration Guide

https://www.better-auth.com/docs/integrations/fastify How exactly is this supposed to work? First of all, the documentation is very limited, it tells you to create the catch-all route, but it doesn't list what routes the handler even supports. Are you just supposed to guess or something? What's the idea here?
17 Replies
sebastian
sebastian4mo ago
it supports all routes provided by better-auth. That's why there's a catch handler for all routes. Also, what is exactly not documented? For me everything seems clear.
JROCBABY
JROCBABYOP4mo ago
Well where are these routes listed?
sebastian
sebastian4mo ago
Why would you need them? You have auth-client and auth.api. You don't need to know the names of the routes. /api/auth/get-session is built in
maxkabechani
maxkabechani4mo ago
Kept getting 404 on fastify so I had to make my own
sebastian
sebastian4mo ago
Weird. Probably some cookie issue.
JROCBABY
JROCBABYOP4mo ago
Well how am I going to call auth.api in the frontend if the auth config is moved to the backend? So I was building my own getSession method to call the backend. I tried /api/auth/session but that didn't work, apparently it's get-session, it would help if that was documented
sebastian
sebastian4mo ago
await authClient.getSession()
JROCBABY
JROCBABYOP4mo ago
But that only works in client components?
sebastian
sebastian4mo ago
Works both on client and server
JROCBABY
JROCBABYOP4mo ago
Oh really? I didn't know that, thanks for the info. The name is kind of misleading though.
sebastian
sebastian4mo ago
But i do think if you have a server route then you should call a endpoint to check for the session. Would stay with authClient for client-side routes. It works on the server, but i think that it should be used on the client.
JROCBABY
JROCBABYOP4mo ago
Alright, and something else, is there any type I can use for my own getSession method? Because Session is just only the session table, the the session should have the user as well?
sebastian
sebastian4mo ago
I don't really know if fastify is simmilar to hono, but i infered the types like this:
import { auth } from "./auth";
import { Env } from "hono";

export const Session = auth.$Infer.Session
export const User = auth.$Infer.Session.user

export interface AuthSession extends Env {
Variables: {
session: typeof Session | null
user: typeof User | null
}
}
import { auth } from "./auth";
import { Env } from "hono";

export const Session = auth.$Infer.Session
export const User = auth.$Infer.Session.user

export interface AuthSession extends Env {
Variables: {
session: typeof Session | null
user: typeof User | null
}
}
JROCBABY
JROCBABYOP4mo ago
Yeah I did that as well, I did export type Session = typeof authClient.$Infer.Session, but I hoped there was a more elegant way, anyways, awesome, thanks for the help apparently this doesn't work await authClient.getSession()
deeDOF
deeDOF4mo ago
You can find most of the route and endpoints in the reference documentation available at endpoint_url/api/auth/reference. provided you’re using the default base path.

Did you find this page helpful?