sidebase / nuxt-auth

Authentication built for Nuxt 3! Easily add authentication via OAuth providers, credentials or Email Magic URLs!
https://auth.sidebase.io
MIT License
1.26k stars 162 forks source link

Invalid type of `SignInResult` when using `redirect: false` #493

Open mubaidr opened 1 year ago

mubaidr commented 1 year ago

Environment

Nuxt project info:

------------------------------
- Operating System: Windows_NT
- Node Version:     v20.4.0
- Nuxt Version:     3.6.5
- Nitro Version:    2.5.2
- Package Manager:  pnpm@8.6.8
- Builder:          vite
- User Config:      runtimeConfig, ssr, app, telemetry, imports, modules, vite, build, postcss, typescript, vue, experimental, nitro
- Runtime Modules:  vuetify-nuxt-module@0.5.3, @sidebase/nuxt-auth@0.6.0-beta.3, @sidebase/nuxt-session@0.2.7, @pinia/nuxt@0.4.11, @vueuse/nuxt@10.2.1, nuxt-security@0.14.2, @nuxt/devtools@0.7.1
- Build Modules:    -
------------------------------

Reproduction

Describe the bug

Typescript does not recognize type of SignInResult type, therefore unabel to extract error or url from result without specifying // @ts-ignore

Additional context

const { error, url } = await useAuth().signIn('credentials', {
      username: username.value,
      password: password.value,
      redirect: false,
    })

Logs

No response

P.S. Great job on this module! This was the missing puzzle piece for Nuxt 3. 🥳

Frallen commented 1 year ago

@mubaidr In my case error is always undefined, but request is 200. Do you have the same?

mubaidr commented 1 year ago

@mubaidr In my case error is always undefined, but request is 200. Do you have the same?

The issue is about types not result. Result is fine.

mathiasrando commented 1 year ago

Is there any progress on this? The issue is still present in the newest version even if redirect is true.

zoey-kaiser commented 11 months ago

The issue currently consists of the signIn function returning either:

{ error: string | null, status: number, ok: boolean, url: any } or void, based on the input variables.

Ideally, I would like to type the return of the function, based on the input values. However this causes some more issues:

One "dirty" way to ensure we have better type support is to always return { error: string | null, status: number, ok: boolean, url: any }, but keep the values undefined, in the case a void would be returned. I had a look at Conditional Types, to make it more pretty, but I feel like our setup is too complex to properly integrate conditional types to support all possible returns.

The current ways this issue impacts developer is:

const result = await signIn('credentials', {
    callbackUrl: '/',
    username: 'jsmith',
    password: 'hunter2',
    redirect: false,
})

This does pose some problems, when trying to further manage the result

BracketJohn commented 11 months ago

What's the second solution to this you mentioned in our talk?

and: which solution do you prefer? (:

zoey-kaiser commented 11 months ago

What's the second solution to this you mentioned in our talk?

The two solutions were:

FollowJack commented 3 months ago

Could you please provide an example of how to get the proper type here when using the signIn method?

That void gets to me.