xamarot
xamarot
Explore posts from servers
BABetter Auth
Created by Acro on 2/9/2025 in #help
SvelteKit Exsmple
The main branch is simply "hiding" the entire app behind github or MS SSO, but there is a branch which has sign up enabled instead.
9 replies
BABetter Auth
Created by Acro on 2/9/2025 in #help
SvelteKit Exsmple
Hello, I have an example doing something like this here https://github.com/zicho/betterauth-sso Feel free to fork it or ask questions or whatever
9 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
Sorry for spam but I think it's working now
import { auth } from '$lib/server/auth'; // path to your auth file
import { redirect, type Handle } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';
import { svelteKitHandler } from 'better-auth/svelte-kit';

const setSessionHook: Handle = async ({ event, resolve }) => {
// this hooks needs to run first in order to set session properly before running the next hook
return svelteKitHandler({ auth, event, resolve })
};

const checkAuthHook: Handle = async ({ event, resolve }) => {
// if user is signed in, session should be here if the setSessionHook hook has been run
const headers = event.request.headers;
const session = await auth.api.getSession({
headers
});

if (!session) {
// redirect if no session is found
const res = await auth.api.signInSocial({
body: {
provider: 'github',
callbackURL: '/'
}
});

redirect(302, res.url!);
}

// resolve as usual
return await resolve(event);
}

export function handleError({ error }) {
console.error(error);
}

export const handle = sequence(setSessionHook, checkAuthHook);
import { auth } from '$lib/server/auth'; // path to your auth file
import { redirect, type Handle } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';
import { svelteKitHandler } from 'better-auth/svelte-kit';

const setSessionHook: Handle = async ({ event, resolve }) => {
// this hooks needs to run first in order to set session properly before running the next hook
return svelteKitHandler({ auth, event, resolve })
};

const checkAuthHook: Handle = async ({ event, resolve }) => {
// if user is signed in, session should be here if the setSessionHook hook has been run
const headers = event.request.headers;
const session = await auth.api.getSession({
headers
});

if (!session) {
// redirect if no session is found
const res = await auth.api.signInSocial({
body: {
provider: 'github',
callbackURL: '/'
}
});

redirect(302, res.url!);
}

// resolve as usual
return await resolve(event);
}

export function handleError({ error }) {
console.error(error);
}

export const handle = sequence(setSessionHook, checkAuthHook);
I would like to submit this example to the docs if possible? Might be good for future users of sveltekit and betterauth
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
Is it possible i need a special case for when the server request is aimed at the auth callback?
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
Shouldnt the [...all] callback handle setting the session before the app is again redirected back from the OAuth sign in?
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
Seems to me like something like this should work?
import { auth } from '$lib/server/auth'; // path to your auth file
import { redirect } from '@sveltejs/kit';
import { svelteKitHandler } from 'better-auth/svelte-kit';

export async function handle({ event, resolve }) {
const headers = event.request.headers;
const session = await auth.api.getSession({
headers
});

if (session) {
return svelteKitHandler({ auth, event, resolve })
} else {
const res = await auth.api.signInSocial({
body: {
provider: 'github',
callbackURL: '/'
}
});

redirect(302, res.url!);
}
}

export function handleError({ error }) {
console.error(error);
}
import { auth } from '$lib/server/auth'; // path to your auth file
import { redirect } from '@sveltejs/kit';
import { svelteKitHandler } from 'better-auth/svelte-kit';

export async function handle({ event, resolve }) {
const headers = event.request.headers;
const session = await auth.api.getSession({
headers
});

if (session) {
return svelteKitHandler({ auth, event, resolve })
} else {
const res = await auth.api.signInSocial({
body: {
provider: 'github',
callbackURL: '/'
}
});

redirect(302, res.url!);
}
}

export function handleError({ error }) {
console.error(error);
}
But session is always null. However, if I disable the redirect, I can see that I get the session and all that. I just need to check it before checking if i should be redirecting? But I do not understand how
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
Ok, now it works:
import { auth } from "$lib/server/auth";
import { toSvelteKitHandler } from "better-auth/svelte-kit";

const handler = toSvelteKitHandler(auth);
export { handler as GET, handler as POST }
import { auth } from "$lib/server/auth";
import { toSvelteKitHandler } from "better-auth/svelte-kit";

const handler = toSvelteKitHandler(auth);
export { handler as GET, handler as POST }
However, I am still confused. How do I handle redirecting users to login if not in hooks?
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
It throws an error, saying "TypeError: Cannot read properties of undefined (reading 'get')". Server endpoint looks like
import { auth } from '$lib/server/auth';

const handler = auth.handler;
export { handler as GET, handler as POST }
import { auth } from '$lib/server/auth';

const handler = auth.handler;
export { handler as GET, handler as POST }
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
And if I want to redirect all users to sign up (don't want any unauthorized users reach any page of my app) how should I do it if not redirecting to the sign in?
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
What do you mean? Are login and signin different pages? And I can't see any 200 coming from my localhost, only 302s are being returned
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
it still just keeps redirecting
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
it seems like session should at least be present in hooks after the first callback to the +server endpoint?
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
So now my hooks is this:
export async function handle({ event }) {
const headers = event.request.headers;

console.dir(headers);

const session = await auth.api.getSession({
headers
});

console.dir(session);

const res = await auth.api.signInSocial({
body: {
provider: 'github',
callbackURL: '/'
}
});

redirect(302, res.url!);
}
export async function handle({ event }) {
const headers = event.request.headers;

console.dir(headers);

const session = await auth.api.getSession({
headers
});

console.dir(session);

const res = await auth.api.signInSocial({
body: {
provider: 'github',
callbackURL: '/'
}
});

redirect(302, res.url!);
}
and /api/auth/callback/github/+server.ts
import { auth } from '$lib/server/auth';
const handler = auth.handler;
export { handler as GET, handler as POST}
import { auth } from '$lib/server/auth';
const handler = auth.handler;
export { handler as GET, handler as POST}
session and headers are still null and I keep just getting redirected to my login
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
in the next docs it tells you to do export const { GET, POST } = toNextJsHandler(auth.handler); but SvelteKit has no examples and this const { GET, POST } = toSvelteKitHandler(auth.handler); does not seem to work
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
how do I do that in a server endpoint? AFAIK "resolve" does not exist there
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
so, returning ´return svelteKitHandler({ event, resolve, auth });' in hooks and auth endpoint?
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
Or something else?
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
like an /api/auth/callback/github?
37 replies
BABetter Auth
Created by xamarot on 1/20/2025 in #help
Setting session/cookie after OAuth redirect
am I using the svelteKitHandler correctly? because I feel like i should use it if I have the session, otherwise redirect
37 replies