lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
BSD Zero Clause License
9.65k stars 500 forks source link

[Bug]: SQLITE_UNKNOWN: SQLite error: no such table: main.user #1335

Closed asafs94 closed 10 months ago

asafs94 commented 10 months ago

Package

lucia-auth

Describe the bug

I am using the following tech stack: Elysia, Lucia, Drizzle and Turso.

This is what my auth module looks like:

import { libSqlClient } from "@/db";
import { TableName } from "@/db/table-names";
import { envVars } from "@/environment-variables";
import { libsql } from "@lucia-auth/adapter-sqlite";
import { github } from "@lucia-auth/oauth/providers";
import { lucia } from "lucia";
import { elysia } from "lucia/middleware";

export const auth = lucia({
  env: envVars.ENVIRONMENT,
  adapter: libsql(libSqlClient, {
    key: TableName.userKeys,
    session: TableName.userSessions,
    user: TableName.users,
  }),
  middleware: elysia(),
  getUserAttributes(databaseUser): { username: string } {
    return {
      // @ts-expect-error Fix this: Typescript is not aware of the username
      username: databaseUser.username,
    };
  }
});

export const githubAuth = github(auth, {
  clientId: envVars.GITHUB_CLIENT_ID,
  clientSecret: envVars.GITHUB_CLIENT_SECRET,
});

export type Auth = typeof auth;

For some reason I am getting the following error when trying to create a user: SQLITE_UNKNOWN: SQLite error: no such table: main.user

My table names do not contain this table name and never did. These are my table names:

export const TableName = {
  users: "users",
  userKeys: "user_keys",
  userSessions: "user_sessions",
  ...
}

I don't understand what the issue is. Would appreciate some help.

This is the user creation code:

  const user = await auth.createUser({
          key: {
            password: body.password,
            providerId: "email",
            providerUserId: body.username,
          },
          attributes: {
            username: body.username,
          },
        });
deadcoder0904 commented 10 months ago

your tablename contains those user on the line in 1st code-block:

    user: TableName.users,

see the key:value pair. the key is user which is probably the table name.

if it helps, check out my old project that shows how you should use tableNames if you want to keep them in 1 place (source of truth) only.

asafs94 commented 10 months ago

your tablename contains those user on the line in 1st code-block:

    user: TableName.users,

see the key:value pair. the key is user which is probably the table name.

if it helps, check out my old project that shows how you should use tableNames if you want to keep them in 1 place (source of truth) only.

Hey, my ‘TableName.users’ is ‘“users”’ and not what the error suggests

deadcoder0904 commented 10 months ago

@asafs94 read what i said again. user is what you pass. check the codeblock. or better yet, see my code.

deadcoder0904 commented 10 months ago

or better yet do it like my tableNames :)

asafs94 commented 10 months ago

Either I don’t understand what you meant or you don’t understand me… Either way, I moved to v3 and it’s much better and works so closing…

deadcoder0904 commented 10 months ago

you are not understanding me. i understood that you think the bug is users but re-read the bug again carefully. it says .user doesn't exist which literally means your key:value pair bcz that is the only place you mention user (singular). every other place its plural users.

glad that you got v3 working. still waiting for a stable release for now.

haruaki07 commented 10 months ago

Sorry for commenting on this closed issue. In case others had the same issue, I just went through it too. Check your relation to users table on sessions table, might there is a typo.

NathanBeddoeWebDev commented 1 month ago

Sorry for commenting on this closed issue. In case others had the same issue, I just went through it too. Check your relation to users table on sessions table, might there is a typo.

Absolute legend mate. This was what I was missing. Thanks for commenting on an otherwise unresolved issue.