auth0 / nextjs-auth0

Next.js SDK for signing in with Auth0
MIT License
2.03k stars 382 forks source link

returnTo not working for me? #339

Closed tylernix closed 3 years ago

tylernix commented 3 years ago

Is there a specific format that the returnTo parameter is looking for in the LoginOptions being passed into the handleLogin function?

It seems straightforward, but no matter what I do, it always redirects after a successful login to the AUTH0_BASE_URL.

My code:

import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';

let returnTo = process.env.AUTH0_BASE_URL + "/profile";

export default handleAuth({
    async loginHandler(req, res) {
        try {
            await handleLogin(req, res, { returnTo });
        } catch (error) {
            res.status(error.status || 500).end(error.message);
        }
    }
});
adamjmcgrath commented 3 years ago

Hi @tylernix - thanks for raising this

Can you try let returnTo = "/profile"; - you don't need to specify a fully qualified url. Security measures should make sure you don't pass in a full URL.

tylernix commented 3 years ago

@adamjmcgrath I did try that too. Still the same on both localhost and deployed on my own domain.

adamjmcgrath commented 3 years ago

@tylernix - could you also share what version of Next.js, what version of Node and what version of this SDK you are using?

tylernix commented 3 years ago

Node v14.15.0

"dependencies": {
    "@auth0/nextjs-auth0": "^1.2.0",
    "@tailwindcss/typography": "^0.3.1",
    "autoprefixer": "^10.1.0",
    "bootstrap": "^4.5.3",
    "classnames": "2.2.6",
    "date-fns": "^2.16.1",
    "gray-matter": "4.0.2",
    "next": "latest",
    "postcss": "^8.2.2",
    "react": "^16.13.0",
    "react-bootstrap": "^1.4.0",
    "react-dom": "^16.13.0",
    "remark": "11.0.2",
    "remark-html": "10.0.0",
    "tailwindcss": "^2.0.2"
  },
adamjmcgrath commented 3 years ago

@tylernix Hmmm.. that looks fine. How are you triggering login? Via a link or the withPageAuthRequired helper?

adamjmcgrath commented 3 years ago

Ah, actually - I've spotted the issue

export default handleAuth({
    async loginHandler(req, res) {

handleAuth takes a login property to override the login handler, eg

export default handleAuth({
    async login(req, res) {
tylernix commented 3 years ago

Ahhh! Beautiful! It worked.

I copied and pasted that code from somewhere, but I just went back to handlers/login, and it does have the right code. 👍 Could this possibly have changed from the beta documentation to the latest 1.2.0 release? I have been working with this since the beta, so that is the only way I could explain things. Thanks again for your help!

adamjmcgrath commented 3 years ago

Cool, glad you got it working @tylernix

Could this possibly have changed from the beta documentation to the latest 1.2.0 release?

The behaviour hasn't changed - but there could be a bad example out there somewhere, if you spot it let me know

tylernix commented 3 years ago

I'll keep an eye out. But I haven't been able to find it in my last few minutes of searching, so maybe I am just crazy. I know at some point I had imported loginHandler from back during the beta (//import { loginHandler } from '@auth0/nextjs-auth0/dist/auth0-session';) because it is still in my comments, but I can't find a single reference to it anymore, so I guess that is a good thing.

Thanks again for your help! I have enjoyed working with nextjs-auth0.