Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextjs pages trpc with lucia auth examples #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

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