Can we bind relationship to user auth?

I don’t know if it’s possible to bind relation to auth. Let say I have another table storing one to one relationship with user. I want to access those information via auth platform. Is it possible? How?

Anyway I tried to bind model from provider but async doesn’t seem to work fine in boot() so I can’t access global information.

boot() {
  const View = this.app.use('Adonis/Src/View')
  const Merchant = this.app.use('App/Models/Merchant')
 
  View.global('isMerchant', async(userId) => {
     const res = await Merchant.findBy('user_id', userId)
     return res && res.is_merchant ? true : false
  })
}

In view I tried to access

isMerchant(1) // [Object Promise]

I know that we generally have to use await before the function but in the view I don’t know how to do that.

1 Like

Now I fixed it by injecting merchant information from middleware.

if (await auth.user) {
  const merchant = await Database.table('merchant_table').where('user_id', auth.user.id).first()
  auth.user.is_merchant = merchant ? true : false
}

Assign middleware(public middleware :wink: ) to route (group)

And I can check if current user has a merchant account in the view as below:

@if(auth.user.is_merchant)
  // Write something
@endif

Any possible way pls let me know.

1 Like