mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
Some of our user-facing callbacks come with a bunch of parameters, and it is not always the case that a user needs all of them. Picking out certain parameters from the end of the list would require the user to define params that they wouldn't even need.
Therefore this PR changes such callbacks so the user can only pick the necessary parameters.
This comes with the bonus of better TS support on the `session` and `signIn` callbacks, where some parameters historically could have been different types.
In the `session` callback, the second param could have been `token` (when using JWT sessions) or `user` (when using DB persisted sessions). Now they are separate parameters.
In the `signIn` callback, we now separate `profile` (OAuth), `email` (Email) and `credentials` (Credentials) provider params.
BREAKING CHANGE:
The `callbacks` method signatures are changing the following way:
```diff
- signIn(user, account, profileOrEmailOrCredentials)
+ signIn({ user, account, profile, email, credentials })
```
```diff
- redirect(url, baseUrl)
+ redirect({ url, baseUrl })
```
```diff
- session(session, tokenOrUser)
+ session({ session, token, user })
```
```diff
- jwt(token, user, account, OAuthProfile, isNewUser)
+ jwt({ token, user, account, profile, isNewUser })
```
> NOTE: You only need to define the params that you actually need (no more need for `_` params.)
This way, if you only need `token` and `account` in the `jwt` callback, you can write:
```js
jwt({ token, account }) {
if(account) {
token.accessToken = account.access_token
}
return token
}
```
25 lines
516 B
JavaScript
25 lines
516 B
JavaScript
// @ts-check
|
|
|
|
/** @type {import("types").CallbacksOptions["signIn"]} */
|
|
export function signIn() {
|
|
return true
|
|
}
|
|
|
|
/** @type {import("types").CallbacksOptions["redirect"]} */
|
|
export function redirect({ url, baseUrl }) {
|
|
if (url.startsWith(baseUrl)) {
|
|
return url
|
|
}
|
|
return baseUrl
|
|
}
|
|
|
|
/** @type {import("types").CallbacksOptions["session"]} */
|
|
export function session({ session }) {
|
|
return session
|
|
}
|
|
|
|
/** @type {import("types").CallbacksOptions["jwt"]} */
|
|
export function jwt({ token }) {
|
|
return token
|
|
}
|