Skip to content

Instantly share code, notes, and snippets.

@zaosoula
Created December 17, 2023 17:46
Show Gist options
  • Save zaosoula/300abdc39b9e0dfb90414796afc6f19f to your computer and use it in GitHub Desktop.
Save zaosoula/300abdc39b9e0dfb90414796afc6f19f to your computer and use it in GitHub Desktop.
FeatherJS OAuth Strategy for Microsoft
import { AuthenticationRequest } from '@feathersjs/authentication'
import { OAuthProfile, OAuthStrategy } from '@feathersjs/authentication-oauth'
import { GeneralError } from '@feathersjs/errors'
import { Params } from '@feathersjs/feathers'
import axios from 'axios'
import { logger } from '../../../logger'
export class MicrosoftStrategy extends OAuthStrategy {
async getProfile(authResult: AuthenticationRequest, _params: Params) {
const { accessToken } = authResult
try {
return (
await axios.get('https://graph.microsoft.com/v1.0/me', {
headers: {
authorization: `Bearer ${accessToken}`
}
})
).data
} catch (error: unknown) {
logger.error(error)
throw new GeneralError('An error occurred while retrieving Microsoft user data')
}
}
async getEntityQuery(profile: OAuthProfile, _params: Params) {
return {
[`${this.name}Id`]: profile.sub || profile.id
}
}
async getEntityData(profile: OAuthProfile, _existingEntity: any, _params: Params) {
const baseData = await super.getEntityData(profile, _existingEntity, _params)
return {
...baseData,
name: profile.displayName,
email: profile.mail
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment