lucia-auth / examples

A collection of example projects built with Lucia
MIT License
171 stars 41 forks source link

wondering about the User type in examples/hono/github-oauth/routes/index.ts #38

Open bkerin opened 4 days ago

bkerin commented 4 days ago

I'm trying to get this example working with Deno and Hono while being pretty new to typescript etc. so sorry if this issue is obvious.

I first changed some tiny things in lib to get it compiling with Deno. Then I started on routes/, and I now get this warning from deno check:

error: TS2339 [ERROR]: Property 'username' does not exist on type 'User'.
    const html = template.replaceAll("%username%", user.username).replaceAll("%user_id%", user.id)

From that it seems like deno is picking up at least some of the types for Lucia but the User type is not as expected or is not correct somehow. I am wondering what is the mechanism by which the username property is expected to be pulled in? I see that username is a db field but don't understand the mechanism by which it becomes a property of User in this context. I think maybe in this context User is actually known to be of a descendant type and I also need to tell typescript about this somehow?

Any advice appreciated

pilcrowOnPaper commented 3 days ago

Did it work before you switched to Deno? I've never used Deno so I'm not sure if I can help

bkerin commented 2 days ago

I've never tried it not on deno. https://lucia-auth.com says it works on deno but admittedly I'm pretty new to deno (as well as javascript generally) so maybe I'm missing something that normally required to get the types imported right

pilcrowOnPaper commented 2 days ago

Did you properly rewrite declare module? Should be npm:lucia instead of lucia.

declare module "npm:lucia" {
    interface Register {
        Lucia: typeof lucia;
        DatabaseUserAttributes: Omit<DatabaseUser, "id">;
    }
}
bkerin commented 2 days ago

Yes, Following the definition chain (via :ALEGoToDefinition) I eventually hit this chunk in my version of lib/auth.ts:

declare module "npm:lucia" {
    interface Register {
        Lucia: typeof lucia;
        DatabaseUserAttributes: Omit<DatabaseUser, "id">;
    }
}

There were a couple other places in my code where as a result of following the examples on the lucia doc pages I had this though:

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

ALE offered them as options when following the definition chain from User type (with :ALEGoToDefinition). My understanding of what declare modules is doing is pretty poor but I guess it's declaring ambient types so I guess there's really only supposed to be one of these. So I went through and nuked the other ones in my code, and that make the error vanish from ALE, but it still shows up in deno check. There's still the root definition from node_modules/.deno/lucia@3.2.0/node_modules/lucia/dist/index.d.ts offered:

export interface Register {
}

So maybe deno is for some reason stuck on that one?

I really appreciate your taking time to look at this, I haven't fealt this noob at software in a long time as I have trying to get a grip on the modern javascript/typescript scene. The sad thing is deno looks a heck of a lot easier and cleaner from new person perspective, but I get a little bit of a scary lonely feeling on it. I'm curious how many people you know have lucia working on deno?

pilcrowOnPaper commented 2 days ago

Yeah I'm not sure if I can help further, especially since I got mine working in Deno after fixing the module declaration

bkerin commented 2 days ago

I'll try the example exactly

bkerin commented 2 days ago

Is your working setup on better-sqlite3? I couldn't get that to work on deno even independently: https://github.com/WiseLibs/better-sqlite3/issues/1205

If you ever get a chance to tar up your working setup I'd love to look at it

pilcrowOnPaper commented 2 days ago

better-sqlite3 only works in Node