Skip to content

Instantly share code, notes, and snippets.

@Nospamas
Last active February 23, 2017 02:48
Show Gist options
  • Save Nospamas/1694cb57fffb171cdf5d89d2856bb4b4 to your computer and use it in GitHub Desktop.
Save Nospamas/1694cb57fffb171cdf5d89d2856bb4b4 to your computer and use it in GitHub Desktop.
GraphQL authentication via facebook-token
export class PassportService {
static async authFacebook(accessToken: string, context: IKoaContext) {
// seems to be the easiest way to get the facebook token into the auth library
// could also be passed as a faked body or query string parameter
context.request.header.access_token = accessToken
try {
let returnUser: any = null;
// its easier to use the user returned in a callback than try to promisfiy
// this function
await Passport.authenticate(
'facebook-token',
{session: false},
(user: any) => {
// returned user object has 'token' field which contains a JWT we'll use
// from here on out
returnUser = user
// tried faking the context with the necessary parameters but
// haven't narrowed what is actually needed yet
})(context, (): any => {
// faked next function
})
return returnUser
} catch (err) {
debug('E_AUTH_BAD_FACEBOOK_TOKEN', err)
throw err
}
}
}
export const authFacebook: GraphQLFieldConfig<typeof UserType, any> = {
type: UserType,
args: {
accessToken: {type: GraphQLString}
},
async resolve(parentValue, args: IAuthFacebookArgs, context: IKoaContext) {
return await PassportService.authFacebook(args.accessToken, context)
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment