Skip to content

Commit

Permalink
docs: update with links to standard and address examples/userinfo review
Browse files Browse the repository at this point in the history
  • Loading branch information
big-kahuna-burger committed Nov 30, 2023
1 parent f89b37d commit e912bec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,13 @@ fastify.googleOAuth2.revokeAllToken(currentAccessToken, undefined, (err) => {
- `userinfo(tokenOrTokenSet)`: A function to retrieve userinfo data from Authorization Provider. Both token (as object) or `access_token` string value can be passed.

Important note:
Userinfo will only work when `discovery` option is used. For a statically configured plugin, you need to make a HTTP call yourself.
Userinfo will only work when `discovery` option is used and such endpoint is advertised by identity provider.

For a statically configured plugin, you need to make a HTTP call yourself.

See more on OIDC standard definition for [Userinfo endpoint](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo)

See more on `userinfo_endpoint` property in [OIDC Discovery Metadata](https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata) standard definition.

```js
fastify.googleOAuth2.userinfo(currentAccessToken, (err, userinfo) => {
Expand Down
24 changes: 6 additions & 18 deletions examples/userinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,16 @@ fastify.register(oauthPlugin, {
// using async/await (promises API) ->
// 1. simple one with async
fastify.get('/interaction/callback/google', async function (request, reply) {
try {
const tokenResponse = await this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply)
const userinfo = await this.googleOAuth2.userinfo(tokenResponse.token /* or tokenResponse.token.access_token */)
reply.send(userinfo)
} catch (error) {
// you want to handle error here better
console.error(error)
reply.send(error.message)
}
const tokenResponse = await this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply)
const userinfo = await this.googleOAuth2.userinfo(tokenResponse.token /* or tokenResponse.token.access_token */)
return userinfo
})

// 2. custom params one with async
// fastify.get('/interaction/callback/google', { method: 'GET', params: { /* custom parameters to be added */ } }, async function (request, reply) {
// try {
// const tokenResponse = await this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply)
// const userinfo = await this.googleOAuth2.userinfo(tokenResponse.token /* or tokenResponse.token.access_token */)
// reply.send(userinfo)
// } catch (error) {
// // you want to handle error here better
// console.error(error)
// reply.send(error.message)
// }
// const tokenResponse = await this.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply)
// const userinfo = await this.googleOAuth2.userinfo(tokenResponse.token /* or tokenResponse.token.access_token */)
// return userinfo
// })

// OR with a callback API
Expand Down

0 comments on commit e912bec

Please sign in to comment.