最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

inertiajs - shared user in adonis inertia config is not coming into vue props - Stack Overflow

programmeradmin5浏览0评论

Out of the box, the adonis inertia.js config file had this section of code:

sharedData: {
    user: (ctx) => ctx.inertia.always(() => ctx.auth.user),
},

But in my vue components, when I add user to defineProps it is undefined.

I am using Ally to authenticate with microsoft and then creating a user to login with. Here is my controller that does that:

import type { HttpContext } from '@adonisjs/core/http'
import User from '#models/user'

export default class MicrosoftLoginsController {
public async redirect({ally}: HttpContext) {
    return ally.use('microsoft').redirect()
}

public async handleCallback ({ally, auth, response}: HttpContext) {
    const ms = ally.use('microsoft')

    if (ms.accessDenied()) {
       return 'You have cancelled the login process'
    }

    if (ms.stateMisMatch()) {
       return 'We are unable to verify the request. Please try again'
    }
    
    if (ms.hasError()) {
        return ms.getError()
    }

    const user = await ms.user()

    const findUser = {
        email: user.original.mail
    }

    const userDetails = {
        fullName: user.original.displayName,
        email: user.original.mail,
        providerId: user.original.id,
        provider: 'microsoft'
    }

    const newUser = await User.firstOrCreate(findUser, userDetails)

    await auth.use('web').login(newUser)

    return response.redirect('/')
  }
}

What am I missing? The documentation on the AdonisJS site doesn't go into much detail.

Out of the box, the adonis inertia.js config file had this section of code:

sharedData: {
    user: (ctx) => ctx.inertia.always(() => ctx.auth.user),
},

But in my vue components, when I add user to defineProps it is undefined.

I am using Ally to authenticate with microsoft and then creating a user to login with. Here is my controller that does that:

import type { HttpContext } from '@adonisjs/core/http'
import User from '#models/user'

export default class MicrosoftLoginsController {
public async redirect({ally}: HttpContext) {
    return ally.use('microsoft').redirect()
}

public async handleCallback ({ally, auth, response}: HttpContext) {
    const ms = ally.use('microsoft')

    if (ms.accessDenied()) {
       return 'You have cancelled the login process'
    }

    if (ms.stateMisMatch()) {
       return 'We are unable to verify the request. Please try again'
    }
    
    if (ms.hasError()) {
        return ms.getError()
    }

    const user = await ms.user()

    const findUser = {
        email: user.original.mail
    }

    const userDetails = {
        fullName: user.original.displayName,
        email: user.original.mail,
        providerId: user.original.id,
        provider: 'microsoft'
    }

    const newUser = await User.firstOrCreate(findUser, userDetails)

    await auth.use('web').login(newUser)

    return response.redirect('/')
  }
}

What am I missing? The documentation on the AdonisJS site doesn't go into much detail.

Share Improve this question edited Mar 14 at 17:29 RyanJMP asked Mar 14 at 12:03 RyanJMPRyanJMP 134 bronze badges 1
  • I have disabled ssr in my project. Would that make a difference? – RyanJMP Commented Mar 14 at 12:14
Add a comment  | 

1 Answer 1

Reset to default 0

Found the answer on another forum. I was not using the auth middleware. To fix it, in my routes file all I had to do was add:

import { middleware } from '#start/kernel'

router.get('/', [HomeController, 'index']).use(middleware.auth())
发布评论

评论列表(0)

  1. 暂无评论