skip to Main Content

here’s my issue, I’m using sveltekit + Lucia v3 + MongoDB. followed the instructions for email + password authentication. signup works fine, user and session are added in database, sign in also works fine, createSession and createSessionCookie both return correct values on sign in. cookies are also stored and I can see it in devtools , however the validateSession always returns null.
hooks.server.js :

import { lucia } from '$lib/server/auth';

export async function handle({ event, resolve }) {
   const sessionId = event.cookies.get(lucia.sessionCookieName);

   if (!sessionId) {
       event.locals.user = null;
       event.locals.session = null;
       return resolve(event);
   }

   const { session, user } = await lucia.validateSession(sessionId);
   //console.log(session, sessionId);
   if (session && session.fresh) {
       const sessionCookie = lucia.createSessionCookie(session.id);

       event.cookies.set.bind(event.cookies)(sessionCookie.name, sessionCookie.value, {
           path: '.',
           ...sessionCookie.attributes
       });
   }

   // if (!session) {
   //     const sessionCookie = lucia.createBlankSessionCookie();
   //     event.cookies.set.bind(event.cookies)(sessionCookie.name, sessionCookie.value, {
   //         path: '.',
   //         ...sessionCookie.attributes
   //     });
   // }
   event.locals.user = user;
   event.locals.session = session;
   return resolve(event);
}

sessionId is also correct. the only problem is with validateSession

auth.js

import { Lucia } from 'lucia';
import { dev } from '$app/environment';
import { adapter } from './MongoClient';

export const lucia = new Lucia(adapter, {
    sessionCookie: {
        attributes: {
            secure: !dev
        }
    },
    getUserAttributes: (attributes) => {
        return {
            // attributes has the type of DatabaseUserAttributes
            username: attributes.username
        };
    }
});

MongoClient:

const client = new MongoClient(uri);
await client.connect();

const db = client.db();
const User = db.collection('user');
const Session = db.collection('sessions');
const adapter = new MongodbAdapter(Session, User);

export { db, adapter };

2

Answers


  1. Chosen as BEST ANSWER

    When I was using Lucia Auth with MongoDB, I encountered an issue while creating a database entry. I initially followed the solution provided in the Lucia docs, which is:

    await db.collection('user').insertOne({
                id: userId,
                username,
                hashed_password: hashedPasword
            });
    

    However, this didn’t work for me. So, I found a solution in pull requests which said to replace id with _id, as MongoDB uses _id as the default field for the unique identifier in the database. The corrected code is:

    await db.collection('user').insertOne({
        _id: userId,
        username,
        hashed_password: hashedPassword
    });
    

  2. i have the same problem here, wondering if someone solved it!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search