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

feat(events): use named params for all event callbacks #2342

Merged
merged 9 commits into from
Jul 11, 2021

Conversation

balazsorban44
Copy link
Member

@balazsorban44 balazsorban44 commented Jul 10, 2021

Unified API for all of our user-facing methods.

NOTE: events.error has been removed. This method has never been called in the core, so it did actually nothing. If you want to log errors to a third-party, check out the logger option instead.

BREAKING CHANGE:

Two event signatures changed to use named params, signOut and updateUser:

// [...nextauth].js
...
events: {
- signOut(tokenOrSession),
+ signOut({ token, session }), // token if using JWT, session if DB persisted sessions.
- updateUser(user)
+ updateUser({ user })
}

@vercel
Copy link

vercel bot commented Jul 10, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nextauthjs/next-auth/2g2yHviWujcRmRooJL6AuU63YCrW
✅ Preview: https://next-auth-git-feat-events-named-params-nextauthjs.vercel.app

@github-actions github-actions bot added core Refers to `@auth/core` hacktoberfest-docs Relates to documentation TypeScript Issues relating to TypeScript labels Jul 10, 2021
@vercel vercel bot temporarily deployed to Preview July 10, 2021 11:02 Inactive
@github-actions github-actions bot added the test Related to testing label Jul 10, 2021
@codecov-commenter
Copy link

codecov-commenter commented Jul 10, 2021

Codecov Report

Merging #2342 (0e89f70) into next (acc9393) will decrease coverage by 0.02%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             next    #2342      +/-   ##
==========================================
- Coverage   10.89%   10.87%   -0.03%     
==========================================
  Files          83       82       -1     
  Lines        1395     1398       +3     
  Branches      389      389              
==========================================
  Hits          152      152              
- Misses       1026     1028       +2     
- Partials      217      218       +1     
Impacted Files Coverage Δ
src/adapters/error-handler.js 0.00% <0.00%> (ø)
src/lib/errors.js 0.00% <0.00%> (ø)
src/server/index.js 0.00% <ø> (ø)
src/server/lib/callback-handler.js 0.00% <0.00%> (ø)
src/server/lib/default-events.js 0.00% <0.00%> (ø)
src/server/lib/oauth/callback.js 0.00% <0.00%> (ø)
src/server/lib/signin/oauth.js 0.00% <0.00%> (ø)
src/server/routes/callback.js 0.00% <0.00%> (ø)
src/server/routes/session.js 0.00% <0.00%> (ø)
src/server/routes/signout.js 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update acc9393...0e89f70. Read the comment docs.

@vercel vercel bot temporarily deployed to Preview July 10, 2021 11:37 Inactive
@github-actions github-actions bot added the adapters Changes related to the core code concerning database adapters label Jul 10, 2021
Copy link
Collaborator

@ubbe-xyz ubbe-xyz left a comment

Choose a reason for hiding this comment

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

Awesome 👏🏽 💯

balazsorban44 added a commit that referenced this pull request Jul 11, 2021
Similar to #2342, this aims to unify the user-facing API and provide an easier way to extend in the future.

In addition, this PR also solves the problem when the `logger.error` method sometimes did not print results, because `Error` instances are not serializable and will be printed as empty objects `"{}"`.

After this PR, we make any `Error` instances serializable as described here: https://iaincollins.medium.com/error-handling-in-javascript-a6172ccdf9af

Closes #1602
Achieved by adding a `client: true` flag when logs are coming from the frontend.

BREAKING CHANGE:

The main change is that instead of an unknown number of parameters, the log events have at most two, where the second parameter is usually an object. In the case of the `error` event, it can also be an `Error` instance (that is serializable by `JSON.stringify`). If it is an object, an `Error` instance will be available on `metadata.error`, and `message` will default to `metadata.error.message`. This is done so that an error event always provides some kind of a stack to see where the error happened

```diff
// [...nextauth.js]
import log from "some-logger-service"
...
logger: {
- error(code, ...message) {},
+ error(code, metadata) {},
- warn(code, ...message) {},
+ warn(code) {}
- debug(code, ...message) {}
+ debug(code, metadata) {}
}
```
@balazsorban44 balazsorban44 marked this pull request as ready for review July 11, 2021 22:23
mnphpexpert added a commit to mnphpexpert/next-auth that referenced this pull request Sep 2, 2024
Similar to nextauthjs#2342, this aims to unify the user-facing API and provide an easier way to extend in the future.

In addition, this PR also solves the problem when the `logger.error` method sometimes did not print results, because `Error` instances are not serializable and will be printed as empty objects `"{}"`.

After this PR, we make any `Error` instances serializable as described here: https://iaincollins.medium.com/error-handling-in-javascript-a6172ccdf9af

Closes nextauthjs#1602
Achieved by adding a `client: true` flag when logs are coming from the frontend.

BREAKING CHANGE:

The main change is that instead of an unknown number of parameters, the log events have at most two, where the second parameter is usually an object. In the case of the `error` event, it can also be an `Error` instance (that is serializable by `JSON.stringify`). If it is an object, an `Error` instance will be available on `metadata.error`, and `message` will default to `metadata.error.message`. This is done so that an error event always provides some kind of a stack to see where the error happened

```diff
// [...nextauth.js]
import log from "some-logger-service"
...
logger: {
- error(code, ...message) {},
+ error(code, metadata) {},
- warn(code, ...message) {},
+ warn(code) {}
- debug(code, ...message) {}
+ debug(code, metadata) {}
}
```
mnphpexpert added a commit to mnphpexpert/next-auth that referenced this pull request Sep 2, 2024
Unified API for all of our user-facing methods.

NOTE: `events.error` has been removed. This method has never been called in the core, so it did actually nothing. If you want to log errors to a third-party, check out the [`logger`](https://next-auth.js.org/configuration/options#logger) option instead.

BREAKING CHANGE:

Two event signatures changed to use named params, `signOut` and `updateUser`:
```diff
// [...nextauth].js
...
events: {
- signOut(tokenOrSession),
+ signOut({ token, session }), // token if using JWT, session if DB persisted sessions.
- updateUser(user)
+ updateUser({ user })
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters Changes related to the core code concerning database adapters core Refers to `@auth/core` hacktoberfest-docs Relates to documentation test Related to testing TypeScript Issues relating to TypeScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants