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

Unable to navigate on afterCallback #1600

Closed
6 tasks done
adminCitify opened this issue Dec 6, 2023 · 3 comments · Fixed by #1602
Closed
6 tasks done

Unable to navigate on afterCallback #1600

adminCitify opened this issue Dec 6, 2023 · 3 comments · Fixed by #1602
Labels
documentation Improvements or additions to documentation

Comments

@adminCitify
Copy link

adminCitify commented Dec 6, 2023

Checklist

Description

Hi!

(APP router) I've tried everything to be able to navigate on the afterCallback.

import { initAuth0 } from '@auth0/nextjs-auth0';
import { NextRequest, NextResponse } from 'next/server';
import { redirect } from 'next/navigation';

     /*...*/

    const handleAuth = auth0.handleAuth({
        login: async (req: any, res: any) => {
            try {
                const result = await auth0.handleLogin(req, res, {
                    authorizationParams: {
                        /* PROMPT: LOGIN WAS ADDED SO THAT WHEN THE USER IS LOGGED OUT AND TRIES TO LOGIN,
                        THE CREDENTIALS ARE ASKED EVEN IF THE APP SESSION IS ACTIVE */
                        prompt: 'login',
                        //httpTimeout: 40000,
                    },
                });
                return result
            }
            catch (error: any) {
                console.error("login error: ", error);
                return new NextResponse(error.message, { status: error.status || 500 });
            }
        },
        logout: async (req: any, res: any) => {
            const returnTo = `${Config.AUTH0_ISSUER_BASE_URL}/v2/logout?returnTo=${BASE_URL}&client_id=${Config.AUTH0_CLIENT_ID}`
            try {
                return await auth0.handleLogout(req, res, {
                    returnTo: returnTo
                });
            } catch (error: any) {
                console.error(error);
                return new NextResponse(error.message, { status: error.status || 500 });
                //return res.status(500).send({ error: 'An error occurred during logout' });
            }
        },
        callback: auth0.handleCallback({ afterCallback })
    });



const afterCallback = async (request: NextRequest, session: any) => {
      /*
      NAVIGATE  TO  /home HERE
     */
};

I tried:
from 'next/navigation';
redirect('/home')

as the docs suggest (https://auth0.github.io/nextjs-auth0/types/handlers_callback.AfterCallbackAppRoute.html) with no success.

I tried:
const url = request.nextUrl.clone();
url.pathname = '/home';
return NextResponse.redirect(url);

..and much more. Nothing works. Please help!

Reproduction

Trying login, and afterCallback is being called succesfsully, but unable to navigate.

Additional context

Im omitting some code before the navigation as it doesn't affect.

nextjs-auth0 version

3.3.0

Next.js version

14.0.1

Node.js version

20.4.0

@adamjmcgrath
Copy link
Contributor

Hi @adminCitify - thanks for raising this.

Will raise a PR to fix those docs, essentially you want something like:

import {
  handleAuth,
  handleCallback,
  AppRouteHandlerFnContext,
  Session,
  getSession,
  AfterCallbackAppRoute
} from '@auth0/nextjs-auth0';
import { NextRequest, NextResponse } from 'next/server';

const afterCallback: AfterCallbackAppRoute = (req: NextRequest, session: Session) => {
  if (session.user) {
    return session;
  }
};

export const GET = handleAuth({
  async callback(req: NextRequest, ctx: AppRouteHandlerFnContext) {
    const res = (await handleCallback(req, ctx, { afterCallback })) as NextResponse;
    const session = await getSession(req, res);
    if (session) {
      return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/success`, res);
    } else {
      return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/fail`, res);
    }
  },
  onError(req: Request, error: Error) {
    console.error(error);
  }
});

@adamjmcgrath adamjmcgrath added the documentation Improvements or additions to documentation label Dec 7, 2023
@adminCitify
Copy link
Author

Worked, thanks!

@trevisw
Copy link

trevisw commented May 9, 2024

Hi @adminCitify - thanks for raising this.

Will raise a PR to fix those docs, essentially you want something like:

import {
  handleAuth,
  handleCallback,
  AppRouteHandlerFnContext,
  Session,
  getSession,
  AfterCallbackAppRoute
} from '@auth0/nextjs-auth0';
import { NextRequest, NextResponse } from 'next/server';

const afterCallback: AfterCallbackAppRoute = (req: NextRequest, session: Session) => {
  if (session.user) {
    return session;
  }
};

export const GET = handleAuth({
  async callback(req: NextRequest, ctx: AppRouteHandlerFnContext) {
    const res = (await handleCallback(req, ctx, { afterCallback })) as NextResponse;
    const session = await getSession(req, res);
    if (session) {
      return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/success`, res);
    } else {
      return NextResponse.redirect(`${process.env.AUTH0_BASE_URL}/fail`, res);
    }
  },
  onError(req: Request, error: Error) {
    console.error(error);
  }
});

@adamjmcgrath kindly update the docs also for this https://auth0.github.io/nextjs-auth0/types/handlers_callback.AfterCallbackAppRoute.html it took me long to find this solution

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

Successfully merging a pull request may close this issue.

3 participants