-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/nextauthjs/next-auth/2g2yHviWujcRmRooJL6AuU63YCrW |
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome 👏🏽 💯
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) {} } ```
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) {} } ```
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 }) } ```
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 thelogger
option instead.BREAKING CHANGE:
Two event signatures changed to use named params,
signOut
andupdateUser
: