Skip to content
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

feat: Add onRedirect hook to pre-process URLs before redirection #185

Merged
merged 4 commits into from
May 21, 2018

Conversation

paulgv
Copy link
Member

@paulgv paulgv commented May 18, 2018

This is another proposal to address #162
It adds an onRedirect hook that can be defined in a plugin in order to pre-process the URL before the redirect happens.
To use this with nuxt-i18n, you could do something like this:

// ~/plugins/auth.js

export default ({ app }) => {
  $auth.onRedirect(to => {
    return app.localePath(to)
  })
})

@pi0
Copy link
Member

pi0 commented May 18, 2018

Nice enhancement. Can we support more than one listener (array) or maybe wouldn't be better to rename the hook to something like setOnRedirect to make it more clear that calling overrides previous value?

@paulgv
Copy link
Member Author

paulgv commented May 18, 2018

@pi0 I changed it so it supports multiple listeners like onError :)

@pi0
Copy link
Member

pi0 commented May 21, 2018

Sorry for the late check. Thanks :)

@pi0 pi0 merged commit 7138202 into nuxt-community:dev May 21, 2018
@paulgv paulgv deleted the feature/redirect-url-hook branch May 21, 2018 12:34
@d0whc3r
Copy link

d0whc3r commented May 22, 2018

I don't think having multiple events for redirect is a good idea, if the last defined event hasn't a return it could mess it up all

In case of callOnError have more sense because the executed function is not assigned to any variable, but is not the case for callOnRedirect

Or check if the executed function have some vale to set to redirect destination...

What do you think?

@pi0
Copy link
Member

pi0 commented May 22, 2018

@d0whc3r I agree. But we can think of them like middleware functions :)

@liuhuanhui
Copy link

hello $auth undefiend

@anheru
Copy link

anheru commented Feb 6, 2019

app.$auth.onRedirect is not a function

@paulgv
Copy link
Member Author

paulgv commented Feb 6, 2019

This hasn't been released yet, it's pending into another PR with other unreleased commits: #192

@pi0 @manniL any updates?

@ghost
Copy link

ghost commented Mar 26, 2019

It's still is not released.
For googlers: I had a problem nuxt-auth and nuxt-i18n combination for custom auth redirects (I could not change language on the login page).
Try to use callback instead of just static string in auth.redirect configuration:

In nuxt.config.js -> auth: { ... } config

// old one - does not work switch language on /auth/login
redirect: {
      login: '/auth/login',
      logout: '/',
      callback: '/auth/login',
      home: '/',
}

// new one. Works fine for me
redirect: {
      login: () => '/auth/login',
      logout: '/',
      callback: () => '/auth/login',
      home: '/',
}

@dseidl
Copy link

dseidl commented Apr 2, 2019

when will this be released?

@mrleblanc101
Copy link

I did not define redirect in thenuxt.config.js, I defined them inside a plugin.
If someone have a better way, please let me know

export default ({ app }) => {
    if (app.i18n.locale === 'fr') {
        app.$auth.options.redirect = {
            login: '/infolettre',
            logout: '/infolettre',
            callback: '/infolettre',
            home: '/infolettre',
        };
    } else {
        app.$auth.options.redirect = {
            login: '/en/newsletter',
            logout: '/en/newsletter',
            callback: '/en/newsletter',
            home: '/en/newsletter',
        };
    }
};

@gaelreyrol
Copy link

When this will be released ? 6 months without any major release for a such package, this is not serious...

pi0 pushed a commit that referenced this pull request May 23, 2019
@pi0 pi0 mentioned this pull request May 23, 2019
@pi0
Copy link
Member

pi0 commented May 23, 2019

@paulgv I've added changes to the dev with (aacb191). Will be released with v4.6.0.

-- BTW sorry for not updating this module for a while. I was more than busy.

@stact
Copy link

stact commented May 31, 2019

Regarding https://auth.nuxtjs.org/api/auth.html#onredirect-handler
Something go wrong on my side:
Plugin returns:
Cannot read property 'onRedirect' of undefined

@stact
Copy link

stact commented May 31, 2019

missed the documentation ^^)
https://auth.nuxtjs.org/api/options.html#plugins

@gnncl
Copy link

gnncl commented Dec 17, 2019

Regarding https://auth.nuxtjs.org/api/auth.html#onredirect-handler
Something go wrong on my side:
Plugin returns:
Cannot read property 'onRedirect' of undefined

Try this

auth: {
    plugins: [ '~/plugins/auth.js' ]
}

@salim-dev
Copy link

nuxt.config.js :

.....
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/auth',
    'nuxt-i18n'
  ],
auth: {
    plugins: [ '~/plugins/auth.js' ]
}

plugins/auth.js :

export default function({app, $auth }) {
    $auth.options.redirect = {
      login: app.localePath('login'),
      logout: app.localePath('logout'),
      callback: app.localePath('callback'),
      home: app.localePath('home'),
    };
}

@mercibe
Copy link

mercibe commented Oct 16, 2020

To allow users selecting their locale on the login screen and make their choice follows to the app, I simply added the following plugin (@nuxtjs/auth:4.9.1 and nuxt:2.14.7)

plugins/auth.js :

export default function({app, $auth }) {
  $auth.onRedirect((to, from) => {
    // change `to` by returning a new value
    return app.localePath(to);
  })

}

@jonalxh
Copy link

jonalxh commented Nov 25, 2020

Hi everybody, thanks for your help, I've been able to implement your suggestion, but when redirect is done an error log is shown in console:

Error: Redirected when going from "/profile" to "/es/perfil" via a navigation guard.

I thought it was still using the old redirection, but i changed the redirect endpoint and it is done correctly.

How to avoid the error mesage?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.