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: allow to return string in signIn callback #1019

Merged
merged 3 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/server/lib/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
* @param {object} profile User profile (e.g. user id, name, email)
* @param {object} account Account used to sign in (e.g. OAuth account)
* @param {object} metadata Provider specific metadata (e.g. OAuth Profile)
* @return {boolean|object} Return `true` (or a modified JWT) to allow sign in
* @return {boolean|string} Return `true` to allow sign in
* Return `false` to deny access
* Return `string` to redirect to (eg.: "/unauthorized")
*/
const signIn = async (profile, account, metadata) => {
const isAllowedToSignIn = true
Expand Down
14 changes: 10 additions & 4 deletions src/server/routes/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ export default async (req, res, options, done) => {
const signInCallbackResponse = await callbacks.signIn(userOrProfile, account, OAuthProfile)
if (signInCallbackResponse === false) {
return redirect(`${baseUrl}${basePath}/error?error=AccessDenied`)
} else if (typeof signInCallbackResponse === 'string') {
return redirect(signInCallbackResponse)
}
} catch (error) {
if (error instanceof Error) {
return redirect(`${baseUrl}${basePath}/error?error=${encodeURIComponent(error)}`)
} else {
return redirect(error)
}
// TODO: Remove in a future major release
logger.warn('SIGNIN_CALLBACK_REJECT_REDIRECT')
return redirect(error)
}

// Sign user in
Expand Down Expand Up @@ -162,13 +165,16 @@ export default async (req, res, options, done) => {
const signInCallbackResponse = await callbacks.signIn(profile, account, { email })
if (signInCallbackResponse === false) {
return redirect(`${baseUrl}${basePath}/error?error=AccessDenied`)
} else if (typeof signInCallbackResponse === 'string') {
return redirect(signInCallbackResponse)
}
} catch (error) {
if (error instanceof Error) {
return redirect(`${baseUrl}${basePath}/error?error=${encodeURIComponent(error)}`)
} else {
return redirect(error)
}
// TODO: Remove in a future major release
logger.warn('SIGNIN_CALLBACK_REJECT_REDIRECT')
return redirect(error)
}

// Sign user in
Expand Down
8 changes: 4 additions & 4 deletions www/docs/configuration/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ callbacks: {
* @param {object} user User object
* @param {object} account Provider account
* @param {object} profile Provider profile
* @return {boolean} Return `true` (or a modified JWT) to allow sign in
* @return {boolean|string} Return `true` to allow sign in
* Return `false` to deny access
* Return `string` to redirect to (eg.: "/unauthorized")
*/
signIn: async (user, account, profile) => {
const isAllowedToSignIn = true
Expand All @@ -54,9 +55,8 @@ callbacks: {
} else {
// Return false to display a default error message
return false
// You can also Reject this callback with an Error or with a URL:
// throw new Error('error message') // Redirect to error page
// return '/path/to/redirect' // Redirect to a URL
// Or you can return a URL to redirect to:
// return '/unauthorized'
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions www/docs/warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,23 @@ You can use [node-jose-tools](https://www.npmjs.com/package/node-jose-tools) to

#### JWT_AUTO_GENERATED_ENCRYPTION_KEY

#### SIGNIN_CALLBACK_REJECT_REDIRECT

You returned something in the `signIn` callback, that is being deprecated.

You probably had something similar in the callback:
```js
return Promise.reject("/some/url")
```

or

```js
throw "/some/url"
```

To remedy this, simply return the url instead:

```js
return "/some/url"
```