CWA 2 is a prerelease (2.0.0-alpha), heading for a beta. It runs production sites; configuration can still change between releases, always with upgrade notes. View versions and support
Extending the CWA

Sign-In and Sign-Out Events

The cwa:auth:signed-in and cwa:auth:signed-out hooks, when each one is called, and what happens when a handler throws.

An extension often keeps something for the signed-in user, such as a basket it merges when they sign in or clears when they leave. The module calls two Nuxt runtime hooks for this. A site can listen to them too.

Listening From a Plugin

// cwa-shop/runtime/plugin.client.ts
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook('cwa:auth:signed-in', async ({ user }) => {    await mergeGuestBasket(user['@id'])
  })

  nuxtApp.hook('cwa:auth:signed-out', () => {    clearBasket()
  })
})

Both hooks are typed, so user is the CWA user that /me returned.

When Each Is Called

HookCalledNot called
cwa:auth:signed-inAfter a sign-in through $cwa.auth.signIn() (the login page and useLogin use it), once the user has loaded from /meWhen a page loads for someone already signed in, or when the sign-in or the user's load fails
cwa:auth:signed-outOnce, when a signed-in session ends in the browser: signing out, or a /me that fails because the session expiredDuring a server render, and for a visitor who was never signed in

A session that's found to have expired during a server render loads the page signed out without calling cwa:auth:signed-out. To find out who's signed in when a page loads, read Authentication › Reading Auth State instead.

A handler can't stop or fail a sign-in. Sign-in doesn't wait for the handlers, and an error a handler throws is logged to the browser console with console.error. Handle your own errors if the user needs to know.

Next: back to the Extending the CWA overview.