Last active
February 9, 2024 15:35
-
-
Save daffl/d0dee9cda7eee3d270bc99a1f1c67d9b to your computer and use it in GitHub Desktop.
Multi client oAuth redirect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const querystring = require('qs'); | |
class MultiClientOauth extends OAuthStrategy { | |
async getRedirect (data, params) { | |
const redirectUrl = (params && params.redirect) || '/'; | |
const { redirect } = this.authentication.configuration.oauth; | |
if (!redirect) { | |
return null; | |
} | |
if(!redirect.some(url => redirectUrl.startsWith(url))) { | |
throw new Error('Invalid redirect URL'); | |
} | |
const authResult = data; | |
const query = authResult.accessToken ? { | |
access_token: authResult.accessToken | |
} : { | |
error: data.message || 'OAuth Authentication not successful' | |
}; | |
return `${redirectUrl}#${querystring.stringify(query)}`; | |
} | |
} | |
class GitHubStrategy extends MultiClientOauth { | |
async getEntityData(profile) { | |
const baseData = await super.getEntityData(profile); | |
return { | |
...baseData, | |
// You can also set the display name to profile.name | |
name: profile.login, | |
// The GitHub profile image | |
avatar: profile.avatar_url, | |
// The user email address (if available) | |
email: profile.email | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment