SharedData Problem - Inertia + React

here is my inertia.ts

  sharedData: {
    greetings: 'bonjour',
    user: (ctx) => ctx.auth.user,
    errors: (ctx) => ctx.session?.flashMessages.get('errors'),
  },
Copied!


Here is a page:

interface Props extends PageProps {}

const SigninPage = () => {
const { errors, greetings, user } = usePage<Props>().props
console.log({ user, errors, greetings })
}
Copied!


I get the greetings but not errors or user

Join The Discussion! (6 Replies)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 15 days ago

    Hi Michel,

    Are you sure you have an authenticated user and/or errors? Try giving them a console log inside the sharedData just to make sure you actually have a value there. This has been the reason for a few others in the past.

    sharedData: {
      greetings: 'bonjour',
    
      user: (ctx) => {
        const user = ctx.auth?.user
        console.log({ user })
        return user
      },
    
      errors: (ctx) => {
        const errors = ctx.session?.flashMessages.get('errors')
        console.log({ errors })
        return errors
      },
    }
    Copied!

    Also, if you're using v3 of the AdonisJS Inertia adapter or later, you don't need to manually pass the errors via sharedData, the package will now do that automatically.

    0

    Please sign in or sign up for free to reply

    1. Commented 14 days ago

      I've undefined for user and errors.
      here is what I've done in my signin_controller

      try{} catch (error) {
            if (error instanceof errors.E_INVALID_CREDENTIALS) {
              session.flash('errors', { form: 'Invalid credentials' })
              return response.redirect().back()
            }
      
            // Handle other errors
            session.flash('errors', { form: 'An error occurred during login' })
            return response.redirect().back()
          }
      Copied!

      Maybe it can help understand what's going on

      1

      Please sign in or sign up for free to reply

      1. Commented 14 days ago
        // When I do this, I get the errors
        
        sharedData: {
          greetings: 'bonjour',
          user: (ctx) => {
            const errors = ctx.session.flashMessages.get('errors')
            console.log({ ctx: errors })
            return ctx.auth.user
          },
        },
        Copied!
        1

        Please sign in or sign up for free to reply

        1. Commented 14 days ago

          I'm guessing you're probably using v3 or later of the AdonisJS Inertia adapter and errors is already being shared automatically for you.

          Since you're sharing to a custom errors key in your flash messages, that won't be included within those.

          Try writing to the errorsBag instead. This is how the invalid credentials exception does it itself, and this is automatically checked and populated by the AdonisJS Inertia adapter (v3+).

          if (error instanceof errors.E_INVALID_CREDENTIALS) {
            session.flashErrors({ form: 'Invalid credentials' })
            return response.redirect().back()
          }
          
          // Handle other errors
          session.flashErrors({ form: 'An error occurred during login' })
          return response.redirect().back()
          Copied!

          Then, so long as my assumption is correct and you're using v3 or later of the adapter package, you can remove errors from your sharedData

          1

          Please sign in or sign up for free to reply

          1. Commented 14 days ago

            Thank you so much!
            Yeah I'm using "@adonisjs/inertia": "^3.1.0" It all works now. For the user I didn't use auth middleware on my route that's why I was not getting the user

            1

            Please sign in or sign up for free to reply

            1. Commented 14 days ago

              Awesome, glad to hear all is working now!

              Yep, that'll do it! You'll want to either use:

              • The auth middleware (requires an authenticated user)

              • The silent auth middleware (checks for user, but does not require auth)

              • Or, call await auth.check() to check for an authenticated user without one of the two middleware.

              1

              Please sign in or sign up for free to reply