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

Add support for configuring the default handlers [SDK-3566] #840

Merged
merged 21 commits into from
Oct 6, 2022

Conversation

Widcket
Copy link
Contributor

@Widcket Widcket commented Oct 6, 2022

  • All new/changed/fixed functionality is covered by tests (or N/A)
  • I have added documentation for all new/changed functionality (or N/A)

📋 Changes

Previously it was not possible to configure the default handlers. For example, to pass a connection parameter to the login handler, it had to be overridden.

export default handleAuth({
  login: async (req, res) => {
    try {
      await handleLogin(req, res, {
        authorizationParams: { connection: 'github' },
      });
    } catch (error) {
      // ...
    }
  }
});

This PR allows to configure default handlers by passing an options object to them. This is the same options object that can be passed when overriding a handler.

export default handleAuth({
  // Works with all default handlers: login, logout, callback, profile
  login: handleLogin({
    authorizationParams: { connection: 'github' }
  })
});

Alternatively, it is possible to pass a function that receives the request and returns an options object.

export default handleAuth({
  login: handleLogin((req) => {
    return {
      authorizationParams: { connection: 'github' }
    };
  })
});

This PR also adds the ability to create new handlers by configuring the default ones, which is useful e.g. for easily creating a signup handler.

export default handleAuth({
  // Creates /api/auth/signup
  signup: handleLogin({
    authorizationParams: { screen_hint: 'signup' }
  })
});

📎 References

Fixes #379, #381, #400, #471, #701

🎯 Testing

Besides adding unit tests, the changes were tested manually using the kitchen sink example app.

@Widcket Widcket requested a review from a team as a code owner October 6, 2022 04:25
@vercel
Copy link

vercel bot commented Oct 6, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
nextjs-auth0 ⬜️ Ignored (Inspect) Oct 6, 2022 at 2:45PM (UTC)

@adamjmcgrath
Copy link
Contributor

Nice 👏

Can you update the examples, I'm thinking these ones:

  • Customize handlers behavior
  • Access an External API from an API Route
  • Add a signup handler

export type Handlers = ApiHandlers | ErrorHandlers;

type ApiHandlers = {
[key: string]: NextApiHandler;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added types to this in b02f9b4

Also noticed that the login, logout etc types weren't HandleLogin, HandleLogout but NextApiHandler.

image

Copy link
Contributor

@adamjmcgrath adamjmcgrath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 👍 just the examples updates

@adamjmcgrath adamjmcgrath self-requested a review October 6, 2022 14:45
@adamjmcgrath adamjmcgrath merged commit f594c3a into vNext Oct 6, 2022
@adamjmcgrath adamjmcgrath deleted the feature/custom-handler-config branch October 6, 2022 14:47
@adamjmcgrath adamjmcgrath mentioned this pull request Nov 30, 2022
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

Successfully merging this pull request may close these issues.

2 participants