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

Can axios not redirect to /login on 401 response #41

Closed
ImadBoumzaoued opened this issue Jun 22, 2017 · 5 comments
Closed

Can axios not redirect to /login on 401 response #41

ImadBoumzaoued opened this issue Jun 22, 2017 · 5 comments

Comments

@ImadBoumzaoued
Copy link

ImadBoumzaoued commented Jun 22, 2017

Hello I'm using a route called /sign-in for user authentication

when the api returns a 401 status code I get redirected to /login which doesn't exists
it would be nice if we could customize the 401 redirect route.

This question is available on Nuxt.js community (#c45)
@PMK
Copy link

PMK commented Jul 5, 2017

@pi0
Copy link
Member

pi0 commented Jul 5, 2017

Will be enhanced soon allow customizing behaviour :)

@silverbackdan
Copy link
Contributor

Is it going to be as simple as changing that 401 if statement to this?

if (error.response.status === 401) {
    <% if(options.loginRedirect) { %>
        return this.redirect(process.env.LOGIN_URL || '<%= options.loginURL %>' || '/login' )
    <% } else { %>
        error.statusCode = 401
        error.message = (process.env.UNAUTHORIZED_ERROR_MESSAGE || '<%= options.unauthorizedErrorMessage %>' || 'Unauthorized' )
    <% } %>
} else {
    error.statusCode = error.statusCode || parseInt(error.response.status) || 500
    error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)')
}

And perhaps adding some docs about the environment vars, options and 401 handling?

@silverbackdan
Copy link
Contributor

this.redirect should be this.app.router.go now as well.

This works but I'm wondering how useful the automatic redirecting is anyway, perhaps leave this up to the user? Happy to make a PR with or without the redirect feature if you'd like me to (unless you're already had more features in mind)

// Nuxt friendly error handler
function errorHandler(error) {
  if (error.response) {
    // Error from backend (non 2xx status code)
    if (error.response.status === 401) {
      <% if(options.loginRedirect) { %>
        return this.app.router.go(process.env.LOGIN_URL || '<%= options.loginURL %>' || '/login' )
      <% } else { %>
        error.statusCode = 401
        error.message = (process.env.UNAUTHORIZED_ERROR_MESSAGE || '<%= options.unauthorizedErrorMessage %>' || 'Unauthorized' )
      <% } %>
    } else {
      error.statusCode = error.statusCode || parseInt(error.response.status) || 500
      error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)')
    }
    error.statusCode = error.statusCode || parseInt(error.response.status) || 500
    error.message = error.message || error.response.statusText || (error.statusCode + ' (Internal Server Error)')
  } else if (error.request) {
    // Error while making request
    error.statusCode = error.statusCode || 500
    error.message = error.message || 'request error'
  } else {
    // Something happened in setting up the request that triggered an Error
    error.statusCode = 500
    error.message = error.message || 'axios error'
  }
  // Display error page on unhandled promises
  if(process.browser) {
    return Promise.reject(error)
  } else {
    // Don't throw unhandled promises in SSR context
    return this.app._nuxt.error.call({$options: this.app}, {
      message: error.message,
      statusCode: error.statusCode
    })
  }
}

@silverbackdan
Copy link
Contributor

As of PR #74 - you can now disable the error handler which may be enough to close this issue

@pi0 pi0 closed this as completed Aug 29, 2017
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

No branches or pull requests

4 participants