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

[Docs]: Clarify that user attributes need to match their database column names #1246

Closed
dallenbaldwin opened this issue Nov 4, 2023 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@dallenbaldwin
Copy link

Description

I was following the docs to put together a SvelteKit project using DrizzleORM and all was going super well until I started adding additional attributes to my users.

While I can see the quick solution is to just make the column names match the property names, I've seen instances where people expect those to be independent, especially when the ORM allows you to customize them independently.

It would probably be a good idea to update the docs to mention this behavior in the Users documentation

my user

export const users = sqliteTable('users', {
  /**
   * generated by lucia
   *
   * @see https://lucia-auth.com/basics/users/
   */
  id: text('id').primaryKey(),
  username: text('username').notNull(),
  /**
   * @default 'user'
   * @see {@link roles}
   */
  role: text('role', { enum: roles }).notNull(),
  /**
   * @default 'light'
   * @see {@link lightThemes}
   */
  lightTheme: text('light_theme', { enum: lightThemes }).notNull(),
  /**
   * @default 'dark'
   * @see {@link darkThemes}
   */
  darkTheme: text('dark_theme', { enum: darkThemes }).notNull(),
})

does not work

type User = import('$lib/server').User
...
  namespace Lucia {
    type Auth = import('$lib/server').Auth
    type DatabaseUserAttributes = Omit<User, 'id'>
    type DatabaseSessionAttributes = NonNullable<unknown>
  }
// create user with username method
const user = await auth.createUser({
  key: {
    providerId: 'username',
    password,
    // lowercase to ensure uniqueness
    providerUserId: username.toLowerCase(),
  },
  attributes: {
    username,
    role: 'user',
    darkTheme: 'dark',
    lightTheme: 'light',
  },
})

works

type User = import('$lib/server').User
...
  namespace Lucia {
    type Auth = import('$lib/server').Auth
    /**
     * these have to match the _database_ column names, not what you called them
     * in Drizzle's config
     */
    type DatabaseUserAttributes = {
      username: User['username']
      light_theme: User['lightTheme']
      dark_theme: User['darkTheme']
      role: User['role']
    }
    type DatabaseSessionAttributes = NonNullable<unknown>
  }
// create user with username method
const user = await auth.createUser({
  key: {
    providerId: 'username',
    password,
    // lowercase to ensure uniqueness
    providerUserId: username.toLowerCase(),
  },
  attributes: {
    username,
    role: 'user',
    dark_theme: 'dark',
    light_theme: 'light',
  },
})
@dallenbaldwin dallenbaldwin added the documentation Improvements or additions to documentation label Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants