Closed msonowal closed 3 years ago
Same here after update headers are gone. Only quick solution I found just add axios "0.19.2" to your packages.
You are a life saver! I just started my first Nuxt project a few days ago (unknowingly after @nuxtjs/auth updated to 5.12.2) and have spent the last 48 hours banging my head against a wall trying to figure out why the authentication middleware would not work. Thanks to you, I have rolled back to 5.12.1 and sure enough everything works as expected.
I'm clearly no expert, but here is the GitHub diff between 5.12.2 and 5.12.1 for anyone who wants to take a look at what might have caused the issue: https://github.com/nuxt-community/axios-module/compare/v5.12.1...v5.12.2
Can confirm, changed my package.json
from
...
"@nuxtjs/axios": "^5.12.1",
...
to
...
"@nuxtjs/axios": "5.12.1",
...
and it works (notice caret has been removed) . Thanks.
I also can confirm that the issue appears to be explicitly with axios
. For my project all I did was add a resolution to axios@0.19.2
:
// package.json
"resolutions": {
"axios": "0.19.2"
}
@toiiia Я чувствую тебя, дружище. Бля потрачено впустую 4 часа
Thanks god I found this thread while doing random searches in the repository.
I was pretty much banging my wall against the wall for the past day trying to figure out why Nuxt is failing to authenticate the logged-in user in SSR mode. It just kept redirecting all my routes to /login
for no reason.
Downgrading the @nuxtjs/axios
version from 5.12.2
to 5.12.1
did the trick.
@Smidds I never heard of the resolutions
property in package.json. Do you mind explaining what it does and how it is different from changing @nuxtjs/axios
version explicitly ?
Hey @xperator - The resolutions
property is specific to yarn, I suppose I should've clarified. Here's the doc page for it!
@iamroi @toiiia мужики сутки ломал голову, сочувствую вам )
still not working in the middleware
Still broken, just wasted 3 hours. Downgrading to nuxtjs/axios 5.12.1 fixed it.
You have definitely saved me from hours of suffering
I just changed
"@ nuxtjs / axios": "^5.12.2",
to
"@ nuxtjs / axios": "5.12.1",
@pi0 ping as it is broken in latest versions
Is this with v4 or v5 via @nuxtjs/auth-next? Can someone post a working codesandbox repro?
It's possible that this PR fixes the issue, but we need confirmation and ideally a way to repro. Can anyone try it out and verify? https://github.com/nuxt-community/axios-module/issues/411
I did downgraded axios from 5.12.3 to 5.12.1 but still my problem exists when login and hard refresh my nuxtjs application redirect to login page ,
any solution!!!
I run into the same issue. Downgrade to axios 5.12.1 didn't change anything. My current workaround is to disable the cookie feature which works to me, but this has some other downsides.
nuxt.config.js
auth: {
// ...
cookie: false,
strategies: {
local: {
// ...
}
}
},
I tried all of your solution and Still have the same issue, Tired of debugging and having no results , with new release of @nuxtjs/auth-next 5.0.0-1607693598.34d83ea they fxed @nuxtjs/axios 5.12.1 version but I it didn't help me at all and every time I refresh the page it goes directly to login page causing me to have a really hard time developing my application
Please help if you have a solution !!
My suggestion to @mohsenMJ and @Milchreis is to search the yarn.lock
for all mentions of axios
to make sure that all of the sub-dependencies are resolved into the version you want. As someone mentioned above, you can explicitly force sub-dependencies to resolve into a specific version like so:
"dependencies": {
"@nuxtjs/axios": "5.12.1",
},
"resolutions": {
"**/@nuxtjs/axios": "5.12.1"
},
It works for me, and I didn't have to change anything in the nuxt.config.js
.
P.S. Note no ^ character before the version number. I forgot to remove it at first and couldn't figure out why I still got 5.12.3 🤦
The issue was fixed in v5.12.4
of @nuxtjs/axios
. Please, update to latest version of @nuxtjs/auth-next
Installed a new nuxt Projext along with the latest version of @nuxtjs/auth-next v5., @nuxtjs/axios but still have the same issue, when I refresh on a protected route it returns to login page
package.json
"@nuxtjs/auth-next": "5.0.0-1607967355.df8f01e",
"@nuxtjs/axios": "^5.12.4",
nuxt.config.js
axios: {
baseURL : 'http://localhost:8000',
credentials : true,
},
auth:{
strategies: {
'laravelSanctum': {
provider: 'laravel/sanctum',
url: '/',
endpoints: {
csrf : {url: '/sanctum/csrf-cookie', method: 'get'},
login : {url: '/admin/login', method: 'post', propertyName: 'token' },
logout: {url: '/admin/logout', method: 'post' /*, propertyName : 'token'*/ },
user : {url: '/api/v1/admin/user', method: 'post', propertyName: false }
},
},
}
},
login.vue
this.$auth.loginWith('laravelSanctum', {
data: {
email: 'test@test.com',
password: 'password',
}
}).then(res => {
console.log(res);
this.$router.push('/dashboard');
})
Thanks for reporting @mohsenMJ. Can you post a repro?
On December 15, 2020 at 15:09:27, mohsenMJ (notifications@github.com) wrote:
Installed a new nuxt Projext along with the latest version of @nuxtjs/auth-next , @nuxtjs/axios but still have the same issue, when I refresh on a protected route it returns to login page
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/nuxt-community/auth-module/issues/809#issuecomment-745308128, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB4PIMG3WOAAPGM4C7LL63SU5UZPANCNFSM4QMANIZQ .
I am also experiencing the same login redirect issue on protected routes after updating both @nuxtjs/auth-next and @nuxtjs/axios.
package.json
"@nuxtjs/auth-next": "^5.0.0-1608461079.ae1e5db",
"@nuxtjs/axios": "^5.12.4"
I have the same problem:
"@nuxtjs/auth-next": "^5.0.0-1608568767.2fe2217",
"@nuxtjs/axios": "^5.12.5",
after a long time suffering from this problem , I found out that if you don't set the right setting on your sever it won't work, I am using Laravel/Sanctum and here is what I did ( I was using my custom login method):
$request->validate([
'email' => 'required|email',
'password' => 'required'
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
$request->session()->regenerate(); // this line is important
return new Response('', 204); // make sure you return 204
}
throw ValidationException::withMessages([
'email' => [__('auth.failed')],
]);
I successfully logged in and kept me on the page when refreshing any protected page.
Hope it could help you guys!
@mohsenMJ Do you know which "right setting on your server" made the difference?
Even if @mohsenMJ's solution is viable, I am not using Laravel/Sanctum, so it will not help everyone who is experiencing this issue. I am using a .NET Core API to provide the JWT and refresh tokens upon login as well as the user object. Please let me know what other information, if any, is needed to help trouble-shoot this problem.
Thanks in advance!
after a long time suffering from this problem , I found out that if you don't set the right setting on your sever it won't work, I am using Laravel/Sanctum and here is what I did ( I was using my custom login method):
$request->validate([ 'email' => 'required|email', 'password' => 'required' ]); $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { $request->session()->regenerate(); // this line is important return new Response('', 204); // make sure you return 204 } throw ValidationException::withMessages([ 'email' => [__('auth.failed')], ]);
I successfully logged in and kept me on the page when refreshing any protected page.
Hope it could help you guys!
I had the same issue using Nuxt + Laravel Sanctum - My problem was the SESSION_DOMAIN variable that comes from the .env file. I'm using it on a subdomain and the SESSION_DOMAIN was pointing to ".domain.com". When I change it to "sub.domain.com" it worked great.
I am having the same issue as @nuxtjs/auth-next v5.0.0-1624817847.21691f1 requires nuxtjs/axios ^5.13.0.. my setup is as follows:
Laravel 8 w/ Sanctum api: http://localhost:8000 client: http://localhost:3000
nuxt/axios -> 5.13.6
I have "@nuxtjs/axios": "5.12.1",
in my package.json
Debugging the outgoing requests sent from nuxt when I refresh the page, it is sending a request to /api/user, but not /sanctum/csrf-cookie beforehand. I think this has something to do with it.
Anyone have any ideas? I am about to try editing nuxt/axios versions in package-lock but that feels wrong.
Same problem persist until today...
Facing same
Same issue
Fastest way to get this fixed is to dig into the code and figure out what's happening and why.
I've fixed this by tidying up my async
functions. The problem in my case was that after I made a request to a server, after that setting tting a bunch of stuff, which were async functions, so I had to make sure that the request function was async
and that everything had await
so that one step could finish before next one would start and for example set the user using auth module.
To note here, I also have a custom graphql auth schema, and after I tidied up all the methods to use async
await
where relevant, the errors have disasapeared.
I recommend looking into your middleware if you have any, and make sure the code is fine.
I am using laravel sanctum as a authentication strategy, which is running fine with both ssr and client it was sending the correct headers/payloads so thus getting 200 response in user info api call. Either in full page reload(ssr) or client side.
But today I have updated the axios modue to the latest which is v5.12.2 and was previously v5.12.1.
So after update If I do full page reload then when it calls the user info api to get the details of the user and determines if logged in or not based on the status code 200 or 402. In that it does not pass the necessary cookies i.e. session cookies that was set already. So the api returns 401 resulting as unauthenticated. And then it again makes the call from the client side and now rechieves 200 as status as it is passing the correct headers. I have tried putting the previous version and it works.
Kindly let me know if you need anything.
correct headers when it is sent
missing headers when buggy
nuxt.config.js
Reproduce the issue by following the readme
https://github.com/msonowal/nuxt-auth-411