Skip to content

Commit 15570b7

Browse files
committed
feat: allow to return string in signIn callback (#1019)
1 parent a5187b6 commit 15570b7

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

src/server/lib/callbacks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
* @param {object} profile User profile (e.g. user id, name, email)
1313
* @param {object} account Account used to sign in (e.g. OAuth account)
1414
* @param {object} metadata Provider specific metadata (e.g. OAuth Profile)
15-
* @return {boolean|object} Return `true` (or a modified JWT) to allow sign in
15+
* @return {boolean|string} Return `true` to allow sign in
1616
* Return `false` to deny access
17+
* Return `string` to redirect to (eg.: "/unauthorized")
1718
*/
1819
const signIn = async (profile, account, metadata) => {
1920
const isAllowedToSignIn = true

src/server/routes/callback.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,16 @@ export default async (req, res, options, done) => {
7171
const signInCallbackResponse = await callbacks.signIn(userOrProfile, account, OAuthProfile)
7272
if (signInCallbackResponse === false) {
7373
return redirect(`${baseUrl}${basePath}/error?error=AccessDenied`)
74+
} else if (typeof signInCallbackResponse === 'string') {
75+
return redirect(signInCallbackResponse)
7476
}
7577
} catch (error) {
7678
if (error instanceof Error) {
7779
return redirect(`${baseUrl}${basePath}/error?error=${encodeURIComponent(error)}`)
78-
} else {
79-
return redirect(error)
8080
}
81+
// TODO: Remove in a future major release
82+
logger.warn('SIGNIN_CALLBACK_REJECT_REDIRECT')
83+
return redirect(error)
8184
}
8285

8386
// Sign user in
@@ -162,13 +165,16 @@ export default async (req, res, options, done) => {
162165
const signInCallbackResponse = await callbacks.signIn(profile, account, { email })
163166
if (signInCallbackResponse === false) {
164167
return redirect(`${baseUrl}${basePath}/error?error=AccessDenied`)
168+
} else if (typeof signInCallbackResponse === 'string') {
169+
return redirect(signInCallbackResponse)
165170
}
166171
} catch (error) {
167172
if (error instanceof Error) {
168173
return redirect(`${baseUrl}${basePath}/error?error=${encodeURIComponent(error)}`)
169-
} else {
170-
return redirect(error)
171174
}
175+
// TODO: Remove in a future major release
176+
logger.warn('SIGNIN_CALLBACK_REJECT_REDIRECT')
177+
return redirect(error)
172178
}
173179

174180
// Sign user in

www/docs/configuration/callbacks.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ callbacks: {
4444
* @param {object} user User object
4545
* @param {object} account Provider account
4646
* @param {object} profile Provider profile
47-
* @return {boolean} Return `true` (or a modified JWT) to allow sign in
47+
* @return {boolean|string} Return `true` to allow sign in
4848
* Return `false` to deny access
49+
* Return `string` to redirect to (eg.: "/unauthorized")
4950
*/
5051
signIn: async (user, account, profile) => {
5152
const isAllowedToSignIn = true
@@ -54,9 +55,8 @@ callbacks: {
5455
} else {
5556
// Return false to display a default error message
5657
return false
57-
// You can also Reject this callback with an Error or with a URL:
58-
// throw new Error('error message') // Redirect to error page
59-
// return '/path/to/redirect' // Redirect to a URL
58+
// Or you can return a URL to redirect to:
59+
// return '/unauthorized'
6060
}
6161
}
6262
}

www/docs/warnings.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,23 @@ You can use [node-jose-tools](https://www.npmjs.com/package/node-jose-tools) to
4848

4949
#### JWT_AUTO_GENERATED_ENCRYPTION_KEY
5050

51+
#### SIGNIN_CALLBACK_REJECT_REDIRECT
52+
53+
You returned something in the `signIn` callback, that is being deprecated.
54+
55+
You probably had something similar in the callback:
56+
```js
57+
return Promise.reject("/some/url")
58+
```
59+
60+
or
61+
62+
```js
63+
throw "/some/url"
64+
```
65+
66+
To remedy this, simply return the url instead:
67+
68+
```js
69+
return "/some/url"
70+
```

0 commit comments

Comments
 (0)