Last active
February 23, 2017 02:48
-
-
Save Nospamas/1694cb57fffb171cdf5d89d2856bb4b4 to your computer and use it in GitHub Desktop.
GraphQL authentication via facebook-token
This file contains hidden or 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
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 | |
} | |
} | |
} |
This file contains hidden or 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
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