Skip to content

Commit d7ad41a

Browse files
committed
Initial commit
0 parents  commit d7ad41a

File tree

9 files changed

+10033
-0
lines changed

9 files changed

+10033
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
cjs

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Thomas Ghysels
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

package.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "payload-plugin-oauth2",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"main": "cjs/index.ts",
6+
"types": "cjs/index.d.ts",
7+
"scripts": {
8+
"clean": "rm -rf cjs",
9+
"build": "tsc",
10+
"esbuild": "esbuild ./src/index.ts --bundle --outdir=cjs --format=cjs --target=node18 --platform=node --external:react --external:axios --external:passport --external:payload --external:express-session"
11+
},
12+
"exports": {
13+
".": {
14+
"types": "./cjs/index.d.ts",
15+
"default": "./cjs/index.js"
16+
},
17+
"./cjs/*": "./cjs/*"
18+
},
19+
"files": [
20+
"cjs"
21+
],
22+
"dependencies": {
23+
"@bothrs/util": "^3",
24+
"@types/express-session": "^1",
25+
"@types/passport-oauth2": "^1",
26+
"@types/passport": "^1",
27+
"axios": "^1",
28+
"express-session": "^1",
29+
"passport-oauth2": "^1"
30+
},
31+
"devDependencies": {
32+
"passport": "^0.6",
33+
"payload": "^1",
34+
"react": "^18",
35+
"typescript": "^4"
36+
},
37+
"peerDependencies": {
38+
"passport": "^0.6",
39+
"payload": "^1",
40+
"react": "^18"
41+
},
42+
"author": "Thomas Ghysels <[email protected]>",
43+
"homepage": "https://github.com/thgh/payload-plugin-oauth2",
44+
"bugs": {
45+
"url": "https://github.com/thgh/payload-plugin-oauth2/issues"
46+
},
47+
"repository": {
48+
"type": "git",
49+
"url": "https://github.com/thgh/payload-plugin-oauth2"
50+
}
51+
}

src/OAuthButton.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Button from 'payload/dist/admin/components/elements/Button'
2+
import { useEffect } from 'react'
3+
4+
export default function OAuthButton() {
5+
useEffect(() => {
6+
setTimeout(() => {
7+
// window.location.href = '/a'
8+
}, 2000)
9+
}, [])
10+
return (
11+
<div style={{ marginBottom: 40 }}>
12+
<Button el="anchor" url="/oauth2/authorize">
13+
Sign in with oAuth
14+
</Button>
15+
</div>
16+
)
17+
}

0 commit comments

Comments
 (0)