-
Notifications
You must be signed in to change notification settings - Fork 924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not redirecting after login when going directly on the route #134
Comments
Hi, I am having the same issue myself on a project I am currently working on. When an un-authenticated user visits I see that this issue was raised since 22 days ago and not responded to. Is there a work-around I can implement before this issue is resolved? Thanks! |
I raised this issue on nuxt community but no one responded. @pi0 would you shed some light on this issue? |
After failing to find any easy/reliable solution I came up with this one:
and then in callback/redirect route
Although everything works just fine but after redirecting to desired link it is redirected back to home page which i don't understand why. |
@Lahori-Jawan,The problem with relying on LocalStorage is that it is client side which can change at any point in time. |
yeah, for now I am just looking for a work around but the problem is that after redirecting to the page it redirects back to homepage, can you guess Why @stevepop ? |
@Lahori-Jawan I think this happens because the It is surely possible to prevent this through a custom middleware where you set But i think the solution using a cookie or the solution @belak came up with using a query would be better. |
@SnooHD Thank you for the input. Actually I am avoiding those solutions for now bec. they both seem pretty daunting for this simple scenario. Could you give me a bit more insight to your suggestion? do you mean like |
@Lahori-Jawan Obviously because its a vuex store property, you will have to use a commit. |
It obviously needs to be cleaned up, but I think the query param is better because it allows you to handle multiple redirects... like if you open a browser that's been closed for a while and a number of tabs open on the same site - with the localStorage method, you can only track one of those (as it's stored in a single key). Once a user logs in to one tab, they lose the redirects on all the other pages, but with query params, they can still be redirected. Unfortunately, while it's better for that situation, it's a bit less flexible. It would be nice to maintain state between redirects, but I feel like it would be a lot of complication for minimal benefit. |
Is someone working on a fix for this bug? |
@nitescent If you truly want to solve this right now, there are 2 ways to do it until a better solution is integrated. |
Please, take a look at my workaround Create plugins/auth.js file |
Thanks @zolotyx, I'm using your solution now |
@zolotyx The workaround continually results in |
@RediJedi not sure if you figured it out by now, but you need to add the plugin to the
|
@joshink Thanks, but that still doesn't work. It is only in the auth block but I get
|
@joshink Actually, it was my fault. I was rewriting the |
@RediJedi Yeah, I think all that code actually does is pass the redirect to the login page for you. It was not redirecting automatically for me either. You should be able to do something like this in your login page's methods. async handleLogin () {
await this.$auth.login( { data } )
this.$router.go(decodeURIComponent(this.$route.query.redirect || "/"))
} Hope that helps! |
The workaround provided by @zolotyx resolves the problem for me. As @SnooHD says, the problem is that when Vuex is used, the redirect is stored in Vuex but after the redirect, the store is lost and so it forgot where to redirect to. The workarounds referenced in #165 may be "better" (I don't really know), but they seem to be more effort than the above workaround, and so I prefer the simpler workaround until something is integrated into the system itself. |
@shawnlauzon It's just that the localStorage will be cleared when the session ends (when tab / browser closes). |
I have a related question here: I think writing a Plugin is really boring. |
Does anyone know if any of these options work with an Identity Provider/auth0 protocol? @SnooHD or @zolotyx do either of you know if your solutions would work with an identity provider in which the user is redirected off the nuxt site to the identity provider for verification and brought back to the callback url? I have tried both solutions to no avail as the redirects are not working (even though they are stored as cookies in @SnooHD solution). Any ideas on this would be greatly appreciated! |
I've ran into this issue as well. My mistake was having <script>
export default {
auth: false
}
</script> |
Thank you. This fixed my issue with the Auth Module. |
This is not working properly. It fixes the problem temporary. @MathiasCiarlo |
It affects us too :/ |
Should be fixed in v4.6.0. Sorry for this late update. |
Is it just me, or is anybody else still having this exact issue? Even after v4.6.0.. |
Still got this issue, can't set redirect in auth store after v4.6.0 @pi0 |
Yes, same here... |
got the same issue here, tried many work arounds I found on this issue feedback but none of them worked. |
I wonder if newly reported issues are exactly the same. Please create new issue with reproduction code (or at least URL) + steps |
I just found out that redirect issue will be solved when disable vuex-persist plugin that I added before. |
Btw, the plugin will be conflict a little bit with some vuex persist plugin. Please update the doc to tell people do not include |
I've answered in another issue, but I'll put here too: The thing is that is seems that the nuxt auth plugin waits this property to be defined to redirect to home. There is no need to make workarounds, but a PR may be welcome. Here is my code:
|
RETURNING TO THE REQUEST PAGE AFTER LOGINBuild in a system for keeping track of what page was originally requested and redirect to it after a successful login can be done in 2 steps.As @belak said:
However, for me his solution didn’t work as I have problems when trying to import the helpers from the library. Nevertheless, there is a most simple solution:
pages/login.vue const handleSubmit = async () => {
try {
errorMsg.value = undefined
await form.submit()
let redirect_url = root.$route.query.redirect || '/dashboard'
root.$router.push({
path: redirect_url,
})
} catch (error) {
errorMsg.value = t(`auth.login.error.${error.code}`)
}
} |
I was fighting with this error too and I solve it adding this line before redirect to the login
|
Hi @caroait can you show me your code where did you put that ? Thanks |
Hi @renztoygwapo I use it before do |
i resolve this add [strictMode: true] in vuex-persist.js plugin:
|
I had a similar problem. I had to redirect the user depending on the Role (User or admin), so, the solution of just writing one common redirection did not work for me. I did the following, I have created a vue page called redirect.vue and there I wrote a middleware that checks the role of the user in order to redirect to the correct page (getting the users info from the vuex store), this solution may not be the best one but works. I hope this help someone! redirect.vue (page):
redirect_middleware.js (middleware)
Note: For this to work, make sure that in the users table is a field called "role" auth object inside nuxt.config.js
From Colombia to the world! :) |
To make it dynamic. You can update the links to look like this. export default function ({ $auth, route, redirect }: any) {
if (route.path !== $auth.options.redirect.login) {
if (!$auth.loggedIn) {
redirect($auth.options.redirect.login, { redirect: route.path })
}
}
} |
I tried to follow the idea of @raquelriobo but it seemed to me little bit "too manual". But it gave me idea of this workaround since (maybe only in my case) there is the redirect path kept in localStorage (but redirect is not happening (just sometimes...). I disabled home redirect in nuxt.config.js auth settings:
And then, when user tries to login I do redirect manually:
So only manual |
Refer to #205 (comment) in case login logout redirection doesn't work for someone. I managed to get it working as mentioned in that comment. Might be helpful for someone. |
why I not start with next!!!!??? |
[Axios] Request error: TypeError: Cannot read property 'url' of undefined |
That was the thing! |
Store the redirect in local storage, if local storage have a the redirect then go to the spacific redirect otherwise /. after redirect clear local storage |
Thank a lot man!! |
Version
v4.1.0
Reproduction link
https://nuxt-auth.herokuapp.com/secure
Steps to reproduce
Hi,
When accessing directly on a secured page, the redirect key is not set in the state, and there is not redirection after login.
This is visible in the demo: access directly this page without be logged, and you will not have the
$auth.$state.redirect
message: https://nuxt-auth.herokuapp.com/secureWhat is expected ?
And redirection after login
What is actually happening?
And no redirection after login.
Additional comments?
Thanks!
The text was updated successfully, but these errors were encountered: