pilcrowOnPaper / lucia-adapter-astrodb

Astro DB adapter for Lucia.
MIT License
95 stars 6 forks source link

Examples #2

Closed zanhk closed 3 months ago

zanhk commented 3 months ago

Hi, did you made an example project for test this package?

In that case is it open source?

thanks

theDeal commented 3 months ago

Here is the setup:

db/config.ts

const User = defineTable({
  columns: {
    id: column.text({ primaryKey: true }),
    username: column.text({ unique: true }),
    hashed_password: column.text(),
  }
});

const Session = defineTable({
    columns: {
        id: column.text({
            primaryKey: true
        }),
        expiresAt: column.date(),
        userId: column.text({
            references: () => User.columns.id
        })
    }
});
@/lib/auth.ts

import { Lucia } from "lucia";
import { AstroDBAdapter } from "lucia-adapter-astrodb";
import { db, User, Session } from "astro:db";

const adapter = new AstroDBAdapter(db, Session, User);

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

declare module "lucia" {
    interface Register {
        Lucia: typeof lucia;
        DatabaseUserAttributes: DatabaseUserAttributes;
    }
}

interface DatabaseUserAttributes {
    username: string;
}

Login and SignUp / SignOut explained here

pilcrowOnPaper commented 3 months ago

There are examples for Astro in the Lucia org repo. All you need to do is to replace the better-sqlite3 with Astro DB, which should be straightforward.

https://github.com/lucia-auth/examples