Skip to content

Conversation

@BimaAdi
Copy link

@BimaAdi BimaAdi commented Oct 15, 2023

Here examples to integrate lucia auth with nextjs pages + trpc. The code are mostly same with nextjs pages username and password examples but I integrated lucia auth session on trpc context.

import { auth } from "@/auth/lucia";
import { CreateNextContextOptions } from "@trpc/server/adapters/next";

export const createContext = async (opts: CreateNextContextOptions) => {
	const authRequest = auth.handleRequest({ req: opts.req, res: opts.res });
	const lucia_session = await authRequest.validate();
	if (!lucia_session) {
		return {
			session: null
		};
	}

	return {
		session: lucia_session
	};
};

export type typeCreateContext = typeof createContext;

So you can access it on any trpc router through context. Let me know what you guys think on this approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant