Skip to content

Commit 39378c8

Browse files
Update README.md after commits bff756b and 7709f45 (#259)
* Update README.md after commits bff756b and 7709f45 * Lint code
1 parent 6e6cbfe commit 39378c8

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ npm i @fastify/oauth2
1717

1818
## Usage
1919

20+
Two separate endpoints need to be created when using the fastify-oauth2 module, one for the callback from the OAuth2 service provider (such as Facebook or Discord) and another for initializing the OAuth2 login flow.
21+
2022
```js
2123
const fastify = require('fastify')({ logger: { level: 'trace' } })
2224
const oauthPlugin = require('@fastify/oauth2')
@@ -30,26 +32,43 @@ fastify.register(oauthPlugin, {
3032
},
3133
auth: oauthPlugin.FACEBOOK_CONFIGURATION
3234
},
33-
// register a fastify url to start the redirect flow
35+
// register a fastify url to start the redirect flow to the service provider's OAuth2 login
3436
startRedirectPath: '/login/facebook',
35-
// facebook redirect here after the user login
37+
// service provider redirects here after user login
3638
callbackUri: 'http://localhost:3000/login/facebook/callback'
3739
// You can also define callbackUri as a function that takes a FastifyRequest and returns a string
3840
// callbackUri: req => `${req.protocol}://${req.hostname}/login/facebook/callback`,
3941
})
4042

43+
// This is the new endpoint that initializes the OAuth2 login flow
44+
fastify.get('/login/facebook', {}, (req, reply) => {
45+
fastify.facebookOAuth2.generateAuthorizationUri(
46+
req,
47+
reply,
48+
(err, authorizationEndpoint) => {
49+
if (err) console.error(err)
50+
reply.redirect(authorizationEndpoint)
51+
}
52+
);
53+
});
54+
55+
// The service provider redirect the user here after successful login
4156
fastify.get('/login/facebook/callback', async function (request, reply) {
4257
const { token } = await this.facebookOAuth2.getAccessTokenFromAuthorizationCodeFlow(request)
43-
58+
4459
console.log(token.access_token)
4560

46-
// if later you need to refresh the token you can use
61+
// if later need to refresh the token this can be used
4762
// const { token: newToken } = await this.getNewAccessTokenUsingRefreshToken(token)
4863

4964
reply.send({ access_token: token.access_token })
5065
})
5166
```
5267

68+
In short, it is necessary to initially navigate to the `/login/facebook` endpoint manually in a web browser. This will redirect to the OAuth2 service provider's login screen. From there, the service provider will automatically redirect back to the `/login/facebook/callback` endpoint where the access token can be retrieved and used. The `CLIENT_ID` and `CLIENT_SECRET` need to be replaced with the ones provided by the service provider.
69+
70+
A complete example is provided at [fastify-discord-oauth2-example](https://github.com/fastify/fastify-oauth2/blob/master/examples/discord.js)
71+
5372
### Usage with `@fastify/cookie`
5473

5574
Since v7.2.0, `@fastify/oauth2` requires the use of cookies to securely implement the OAuth2 exchange. Therefore, if you need `@fastify/cookie` yourself,

examples/discord.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const fastify = require('fastify')({ logger: { level: 'trace' } })
4+
const oauthPlugin = require('..')
5+
6+
fastify.register(oauthPlugin, {
7+
name: 'discordOAuth2',
8+
credentials: {
9+
client: {
10+
id: '<CLIENT_ID>',
11+
secret: '<CLIENT_SECRET>'
12+
},
13+
auth: oauthPlugin.DISCORD_CONFIGURATION
14+
},
15+
startRedirectPath: '/login/facebook',
16+
callbackUri: 'http://localhost:3000/login/discord/callback'
17+
})
18+
19+
fastify.get('/login/discord/callback', async function (request, reply) {
20+
try {
21+
const token =
22+
await this.discordOAuth2.getAccessTokenFromAuthorizationCodeFlow(request)
23+
return reply.send(token)
24+
} catch (error) {
25+
return reply.send(error)
26+
}
27+
})
28+
29+
fastify.get('/login/discord', {}, (req, reply) => {
30+
fastify.discordOAuth2.generateAuthorizationUri(
31+
req,
32+
reply,
33+
(err, authorizationEndpoint) => {
34+
if (err) console.error(err)
35+
reply.redirect(authorizationEndpoint)
36+
}
37+
)
38+
})
39+
40+
fastify.listen({ port: 3000 })

0 commit comments

Comments
 (0)