0.0.5 • Published 5 years ago

nextjs-auth v0.0.5

Weekly downloads
4
License
ISC
Repository
github
Last release
5 years ago

A fork of next-auth library.

In the original library, logging in from anywhere in the app redirects the user to the index page.

Added the routes field in next-auth.providers. E.g. if for the Facebook provider, a the routes array was added:

providers.push({
  providerName: 'Facebook',
  providerOptions: {
    scope: ['email', 'public_profile']
  },
  Strategy: require('passport-facebook').Strategy,
  ...
  routes: [
    {
      from: '/myPage',
      to: '/myPage2'
    }
  ]
})

Then if user logs in from /myPage, the package will login with Facebook then redirect to /auth/callback with the query destination=/myPage2

Now in the /auth/callback component, if you have the following code:

static async getInitialProps({ req, query: { destination } }) {
  return {
    session: await NextAuth.init({ force: true, req: req }),
    destination
  }
}
 
async componentDidMount() {
  const session = await NextAuth.init({ force: true })
  if (this.props.destination) {
    Router.push(this.props.destination)
  } else {
    Router.push('/')
  }
}

Then the callback component will redirect to the route in the destination query.

Thus with this configuration, it becomes possible to login from /myPage and be redirected to /myPage2