romandev
romandev10mo ago

Lucia V3 with convex

Hi, I'm using lucia-auth to authenticate the users and I saw many repos like this and this other one and I even found an issue referencing this (the repos are using an older version of lucia). Actually my adapter looks like this:
import type { DatabaseReader, DatabaseWriter } from './_generated/server';
import {
type Adapter,
Lucia,
type DatabaseUser,
type DatabaseSession,
} from 'lucia';

const convexAdapter = (db: DatabaseWriter): Adapter => { /* stuff */ }
export const auth = (db: DatabaseWriter) => new Lucia(convexAdapter(db));
import type { DatabaseReader, DatabaseWriter } from './_generated/server';
import {
type Adapter,
Lucia,
type DatabaseUser,
type DatabaseSession,
} from 'lucia';

const convexAdapter = (db: DatabaseWriter): Adapter => { /* stuff */ }
export const auth = (db: DatabaseWriter) => new Lucia(convexAdapter(db));
but I don't know how to use it out of the convex functions context, because I need to use it in a action:
// actions/sign-up.ts
'use server';

import { api } from '@convex/_generated/api';
import { fetchMutation } from 'convex/nextjs';
import { cookies as nextCookies } from 'next/headers';

type SignUpProps = {
email: string;
password: string;
};

export async function signup({ email, password }: SignUpProps) {
const { userId } = await fetchMutation(api.users.insert, {
email,
password,
});

const session = await lucia.createSession(userId, {});
const cookies = lucia.createSessionCookie(session.id);

nextCookies().set(cookies.name, cookies.value, cookies.attributes);
}
// actions/sign-up.ts
'use server';

import { api } from '@convex/_generated/api';
import { fetchMutation } from 'convex/nextjs';
import { cookies as nextCookies } from 'next/headers';

type SignUpProps = {
email: string;
password: string;
};

export async function signup({ email, password }: SignUpProps) {
const { userId } = await fetchMutation(api.users.insert, {
email,
password,
});

const session = await lucia.createSession(userId, {});
const cookies = lucia.createSessionCookie(session.id);

nextCookies().set(cookies.name, cookies.value, cookies.attributes);
}
I don't know if this is a convex related issue or I need to open an issue in lucia-auth
GitHub
GitHub - get-convex/convex-lucia-auth: Convex database adapter for ...
Convex database adapter for Lucia Auth. Contribute to get-convex/convex-lucia-auth development by creating an account on GitHub.
GitHub
GitHub - get-convex/convex-lucia-auth-demo: Demo showing authentica...
Demo showing authentication powered by Convex and Lucia - get-convex/convex-lucia-auth-demo
4 Replies
Michal Srb
Michal Srb10mo ago
Hey. For now I would recommend in order: 1. Use Clerk or Auth0 2. If you must use custom auth, check out this repo, but it is very much work in progress. It side-steps lucia because lucia doesn't really give much over handrolling auth, but it uses the new libraries Lucia is built on top of: https://github.com/xixixao/convex-auth We're working on auth this quarter so we'll have more to share in the coming weeks.
GitHub
GitHub - xixixao/convex-auth: Demonstration of authentication purel...
Demonstration of authentication purely via Convex. Contribute to xixixao/convex-auth development by creating an account on GitHub.
romandev
romandevOP10mo ago
Thank you, I’m going to follow him to be up to date
David
David10mo ago
Can't wait for this. I really need email/password auth in Convex that doesn't rely on Clerk, Auth0, Auth.js etc. Thanks for working on this and looking forward to testing this out. @Michal Srb Will this new work implement auth via sessions or JWT?
Michal Srb
Michal Srb10mo ago
@David you can check out the code. It uses a cookie for long term session and exchanges it for a JWT.

Did you find this page helpful?