eidellev / inertiajs-adonisjs

279 stars 17 forks source link

If auth.attempt fails, a response is not a valid Inertia response #42

Closed grohjy closed 2 years ago

grohjy commented 2 years ago

I'm trying to use await auth.attempt(email, password); in my login controller. If authentication fails, adonis sends automatically a response, which is plain json and therefore a popup window appears saying "All Inertia requests must receive a valid Inertia response, however a plain JSON response was received."

It seems that for some reason the automatic response removes the "X-Inertia: true" -header.

I've got a failing authentication working by using try-catch and in catch:

session.flash({
    errors: {message: error.message}
});
return response.redirect().back();
eidellev commented 2 years ago

Thanks for reporting. I will check it out :-)

eidellev commented 2 years ago

@grohjy I fixed this in v.5.0.0 please note that this is a breaking change and you will need register an additional middleware (see readme for details). Let me know if you run into any issues and thanks again for reporting this problem.

madebylydia commented 2 years ago

Hey @eidellev, I hope you won't mind me using this issue to hear my voice, but it seems that Inertia still have a bad time with the Auth module... It is the same issue, using auth.attempt raise an error into the browser's console.

Here's all the code that should be relevant to the issue:

Server-side code (AdonisJS, of course) ```ts public async login({ auth, request, response }: HttpContextContract) { const { username, password } = await request.validate(UserLoginValidator) await auth.use('web').attempt(username, password) return response.redirect('/') } ```
Client-side code (Vue 3) ```vue ```
Error in the console when login with wrong username/password ![image](https://user-images.githubusercontent.com/61093863/157746054-7ca3c192-e9da-483b-b561-7cb0afe7eb17.png)

When making the request, the client does send the X-Inertia header, and the answer too, however we still receive a non valid Inertia response: msedge_Tk1LVWKgty

As you can tell, yes, I'm using the middleware as required. And before you ask me, if I use correct credentials, the request will be just fine, and we'll connect smoothly!

Is there anything wrong I'm doing here? I think not... Hope to hear back from you soon 😄

eidellev commented 2 years ago

Thank you @Predeactor for reporting this issue. I don't mind at all and I am grateful when other developers are using this thing I am building in my spare time. I will try to reproduce the issue and let you know.

eidellev commented 2 years ago

I updated the readme with some additional information under the Advanced section. Let me know how that works for you and feel free to re-open this issue.

madebylydia commented 2 years ago

And... we're there! image Thank for your quick response! One problem fixed today, and will most probably save other's time too!

Just also would like to give you an advice, but please, don't take it bad nor too seriously, but AdonisJS is built to make the end-developer write the less code as possible (At least, to me, that's what confidence refers to in AdonisJS), so maybe if your provider could handle it, it would really embrace the Adonis's philosophy and greatly improve it! But again, I doubt you can do it that easily... So I understand if that's to the end-developer to handle such things! Again, and in all case, thank!