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

async afterCallback signature should support terminating request #952

Closed
7 tasks done
alexmalev opened this issue Dec 7, 2022 · 2 comments
Closed
7 tasks done

async afterCallback signature should support terminating request #952

alexmalev opened this issue Dec 7, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@alexmalev
Copy link
Contributor

alexmalev commented Dec 7, 2022

Checklist

  • The issue can be reproduced in the sample app (or N/A).
  • I have looked into the README and have not found a suitable solution or answer.
  • I have looked into the examples and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Description

In version 2.0 the lib now supports terminating requests in afterCallback.

The contract is that afterCallback should not return session and instead do a res.end()

the problem is that the signature of afterCallback should change from:

export type AfterCallback = ( req: NextApiRequest, res: NextApiResponse, session: Session, state?: { [key: string]: any } ) => Promise<Session> | Session | undefined;

to
export type AfterCallback = ( req: NextApiRequest, res: NextApiResponse, session: Session, state?: { [key: string]: any } ) => Promise<Session | undefined> | Session | undefined;

otherwise we need to //@ts-ignore an async afterCallback that terminates instead of returning a session promise

Reproduction

const afterCallback = async (
  req: NextApiRequest,
  res: NextApiResponse,
  session: Session,
  state?: { [key: string]: any }
) => {
  if (session.user.isAdmin) {
    await doSomeAsyncStuff();
    return session;
  }
  res.status(401).end('User is not admin');
};
const handler = handleAuth({
  callback: handleCallback({ afterCallback }),
  })

throws: Type 'Promise<Session | undefined>' is not assignable to type 'Session | Promise | undefined'.
Type 'Promise<Session | undefined>' is not assignable to type 'Promise'

SDK version

2.0

Next.js version

13

Node.js version

16

@adamjmcgrath
Copy link
Contributor

Thanks for raising this @alexmalev - will take a look at fixing this shortly, also happy to accept a PR if you want to expedite it

@adamjmcgrath adamjmcgrath added the bug Something isn't working label Dec 7, 2022
alexmalev added a commit to alexmalev/nextjs-auth0 that referenced this issue Dec 7, 2022
@adamjmcgrath
Copy link
Contributor

Fixed by #954 - will be released in 2.0.1

Thanks @alexmalev!

FPiety0521 pushed a commit to FPiety0521/Auth0-Next.js-SDK that referenced this issue Apr 28, 2023
redsky030428 added a commit to redsky030428/autho_next.js that referenced this issue May 13, 2024
luckyshinystar7 added a commit to luckyshinystar7/nextjs-crysdyazandco that referenced this issue Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants