atinux / nuxt-auth-utils

Add Authentication to Nuxt applications with secured & sealed cookies sessions.
MIT License
974 stars 91 forks source link

fix: add missing lang=ts for correct slot props types #120

Closed RobertHaba closed 4 months ago

RobertHaba commented 4 months ago

Problem No type suggestions when use slot props

Solution Add lang="ts" for component script tag

Test Clone demo from readme https://github.com/atinux/nuxt-todos-edge

...
    <AuthState v-slot="userSession">
      {{ userSession}}
    </AuthState>

    <NuxtPage />
...

I also think that the documentation is not entirely correct because you need to use loggedIn.value instead of loggedIn. obraz

RobertHaba commented 4 months ago

Fix for readme

Possible correction to the readme

<template>
  <header>
    <AuthState v-slot="{ loggedIn, clear }">
      <button v-if="loggedIn.value" @click="clear">Logout</button>
      <NuxtLink v-else to="/login">Login</NuxtLink>
    </AuthState>
  </header>
</template>

If the page is cached or prerendered, nothing will be rendered until the user session is fetched on the client-side.

You can use the placeholder slot to show a placeholder on server-side and while the user session is being fetched on client-side for the prerendered pages:

<template>
  <header>
    <AuthState>
      <template #default="{ loggedIn, clear }">
        <button v-if="loggedIn.value" @click="clear">Logout</button>
        <NuxtLink v-else to="/login">Login</NuxtLink>
      </template>
      <template #placeholder>
        <button disabled>Loading...</button>
      </template>
    </AuthState>
  </header>
</template>
atinux commented 4 months ago

Thanks, I fixed the usage in the v-bind and it the example should now work!