oscarama
oscarama•2y ago

cookies, headers and authentication

in express I can do something like res.cookie("name", "value") for example. alternatively I can do res.set"set-cookie", "name=value"). how can I do something one of the two above with trpc? or is there another, standardized way to do auth? I'm using trpc v10 and sveltekit
9 Replies
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
oscarama
oscarama•2y ago
ok, thank you! I'll try it out my create context stuff is ran in hooks.server.ts like:
export const handle: Handle = async ({ event, resolve }) => {
if (event.url.pathname.startsWith(`${trpcPathBase}/`)) {
const response = await fetchRequestHandler({
// ...
createContext: () => {
return createContext(event);
},
});

return response;
}

// ...
};
export const handle: Handle = async ({ event, resolve }) => {
if (event.url.pathname.startsWith(`${trpcPathBase}/`)) {
const response = await fetchRequestHandler({
// ...
createContext: () => {
return createContext(event);
},
});

return response;
}

// ...
};
but a res object is nowhere to be found. any idea how I can access it?
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
SmoothNanners
SmoothNanners•2y ago
I tried to pass the SvelteKit cookies object into the context, but it didn't work since SvelteKit resolves the response separately. I was able to set cookies directly in the response (not using SvelteKit's cookies object) in responseMeta by checking the response and paths, but that's kinda messy. I recommend keeping the auth logic on a +page.server.js or +server.js endpoint. tRPC for everything else.
SmoothNanners
SmoothNanners•2y ago
There is the Lucia auth lib for SvelteKit: https://lucia-auth.vercel.app/ It sets some locals, it might be able to be passed to tRPC, but I haven't used it. I have a repo for a custom auth with GitHub OAuth using endpoints as well and passes the session to the tRPC context, feel free to check it out https://github.com/austins/sveltekit-customauth
GitHub
GitHub - austins/sveltekit-customauth: An experiment.
An experiment. Contribute to austins/sveltekit-customauth development by creating an account on GitHub.
oscarama
oscarama•2y ago
that helps, thank you a bit annoying to do in the +page.server.ts but works 🤷‍♂️
oscarama
oscarama•2y ago
Balázs Orbán (@balazsorban44)
Announcing Auth.js! 🔒Authentication for the Web. - Brand new @​auth/core package, - Runtime/framework agnostic - Web standard APIs - Builds on NextAuth.js conventions/API - Decoupled from Next.js & Node.js. We're adding official support for new frameworks...
Likes
412
Twitter
oscarama
oscarama•2y ago
arrived right on time
SmoothNanners
SmoothNanners•2y ago
That's awesome, thanks for sharing!