|
| 1 | +# oAuth plugin for Payload CMS |
| 2 | + |
| 3 | +<a href="LICENSE"> |
| 4 | + <img src="https://img.shields.io/badge/license-MIT-brightgreen.svg" alt="Software License" /> |
| 5 | +</a> |
| 6 | +<a href="https://github.com/thgh/payload-plugin-oauth2/issues"> |
| 7 | + <img src="https://img.shields.io/github/issues/thgh/payload-plugin-oauth2.svg" alt="Issues" /> |
| 8 | +</a> |
| 9 | +<a href="https://npmjs.org/package/payload-plugin-oauth2"> |
| 10 | + <img src="https://img.shields.io/npm/v/payload-plugin-oauth2.svg?style=flat-squar" alt="NPM" /> |
| 11 | +</a> |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- Configures passport-oauth2 |
| 16 | +- Mounts authorize & callback route |
| 17 | +- Adds sign in button on login page |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +``` |
| 22 | +npm install payload-plugin-oauth2 |
| 23 | +# or |
| 24 | +yarn add payload-plugin-oauth2 |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```js |
| 30 | +// payload.config.ts |
| 31 | +import { oAuthPlugin } from 'payload-plugin-oauth2' |
| 32 | + |
| 33 | +export default buildConfig({ |
| 34 | + serverURL: process.env.SERVER_URL, |
| 35 | + collections: [Users], |
| 36 | + plugins: [ |
| 37 | + oAuthPlugin({ |
| 38 | + clientID: process.env.CLIENT_ID, |
| 39 | + clientSecret: process.env.CLIENT_SECRET, |
| 40 | + authorizationURL: process.env.OAUTH_SERVER + '/oauth/authorize', |
| 41 | + tokenURL: process.env.OAUTH_SERVER + '/oauth/token', |
| 42 | + callbackURL: process.env.SERVER_URL + CALLBACK_PATH, |
| 43 | + scope: 'basic', |
| 44 | + async userinfo(accessToken) { |
| 45 | + const { data: user } = await axios.get(OAUTH_SERVER + '/oauth/me', { |
| 46 | + params: { access_token: accessToken }, |
| 47 | + }) |
| 48 | + return { |
| 49 | + sub: user.ID, |
| 50 | + |
| 51 | + // Custom fields to fill in if user is created |
| 52 | + name: user.display_name || user.user_nicename || 'Naamloos', |
| 53 | + email: user.user_email, |
| 54 | + role: user.capabilities?.administrator ? 'admin' : 'user', |
| 55 | + } |
| 56 | + }, |
| 57 | + }), |
| 58 | + ], |
| 59 | +}) |
| 60 | +``` |
| 61 | +
|
| 62 | +## Changelog |
| 63 | +
|
| 64 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 65 | +
|
| 66 | +## Contributing |
| 67 | +
|
| 68 | +Contributions and feedback are very welcome. |
| 69 | +
|
| 70 | +To get it running: |
| 71 | +
|
| 72 | +1. Clone the project. |
| 73 | +2. `npm install` |
| 74 | +3. `npm run build` |
| 75 | +
|
| 76 | +## Credits |
| 77 | +
|
| 78 | +- [Thomas Ghysels](https://github.com/thgh) |
| 79 | +- [All Contributors][link-contributors] |
| 80 | +
|
| 81 | +## License |
| 82 | +
|
| 83 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
| 84 | +
|
| 85 | +[link-contributors]: ../../contributors |
0 commit comments