Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1aeca00

Browse files
authoredJan 21, 2021
docs: clear things up around using access_token [skip release]
#1078
1 parent cf2f899 commit 1aeca00

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed
 

‎www/docs/configuration/callbacks.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,15 @@ e.g. `getSession()`, `useSession()`, `/api/auth/session`
126126
callbacks: {
127127
/**
128128
* @param {object} session Session object
129-
* @param {object} user User object (if using database sessions)
129+
* @param {object} token User object (if using database sessions)
130130
* JSON Web Token (if not using database sessions)
131131
* @return {object} Session that will be returned to the client
132132
*/
133-
async session(session, user) {
134-
session.foo = 'bar' // Add property to session
133+
async session(session, token) {
134+
if(token?.access_token) {
135+
// Add property to session, like an access_token from a provider
136+
session.access_token = token.access_token
137+
}
135138
return session
136139
}
137140
}
@@ -140,7 +143,11 @@ callbacks: {
140143
141144
:::tip
142145
When using JSON Web Tokens the `jwt()` callback is invoked before the `session()` callback, so anything you add to the
143-
JSON Web Token will be immediately available in the session callback.
146+
JSON Web Token will be immediately available in the session callback, like for example an `access_token` from a provider.
147+
:::
148+
149+
:::tip
150+
To better represent its value, when using a JWT session, the second parameter should be called `token` (This is the same thing you return from the `jwt` callback). If you use a database, call it `user`.
144151
:::
145152
146153
:::warning
@@ -175,15 +182,25 @@ callbacks: {
175182
* @return {object} JSON Web Token that will be saved
176183
*/
177184
async jwt(token, user, account, profile, isNewUser) {
178-
const isSignIn = (user) ? true : false
179-
// Add auth_time to token on signin in
180-
if (isSignIn) { token.auth_time = Math.floor(Date.now() / 1000) }
185+
// Add access_token to the token on signin in
186+
if (account?.access_token) {
187+
token.access_token = account.access_token
188+
}
181189
return token
182190
}
183191
}
184192
...
185193
```
186194
195+
:::tip
196+
Use an if branch in jwt with checking for existence of any other params than token. If any of those exist, you call jwt for the first time.
197+
This is a good place to add for example an `access_token` to your jwt, if you want to.
198+
:::
199+
200+
:::tip
201+
Check out the content of all the params in addition `token`, to see what info you have available on signin.
202+
:::
203+
187204
:::warning
188205
NextAuth.js does not limit how much data you can store in a JSON Web Token, however a ~**4096 byte limit** for all cookies on a domain is commonly imposed by browsers.
189206

0 commit comments

Comments
 (0)
Please sign in to comment.