atinux / nuxt-auth-utils

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

fix: narrowed session type passed to `fetch` session hook #71

Closed Gerbuuun closed 7 months ago

Gerbuuun commented 8 months ago

The return value of requireUserSession is guaranteed to have the user component defined. Previously, the return type was changed to reflect that fact but the session type passed to the fetch hook was not. (It was not immediately clear to me, but the result of requireUserSession is passed into the fetch hook) This fixes that and should avoid users to do an unnecessary check when hooking into fetch

Previously:

export default defineNitroPlugin(() => {
  sessionHooks.hook('fetch', (session, event) => {
    // session.user is possibly undefined, so we add a guard
    if (!session.user)
      // Do something

    // Do what you actually wanted to do with 'session.user'
  });
});

With this PR:

export default defineNitroPlugin(() => {
  sessionHooks.hook('fetch', (session, event) => {
    // session.user is defined so you can immediately do things without the need for a check
  });
});

Also, ActiveUserSession is not exposed because the type is completely defined by UserSession and User.

atinux commented 7 months ago

Thanks!