Skip to content

Commit fb04de8

Browse files
committed
update docs
1 parent d871b8f commit fb04de8

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

README.md

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,22 @@
1616
- 🔐 Configures OAuth2 with any providers
1717
- ✨ Zero dependencies
1818
- ⚙ Highly customizable
19+
-
20+
21+
# Integrations
22+
23+
Technically this plugin should work with all generic OAuth2 providers. However, here are the list of providers that have been tested:
24+
25+
| Provider | Status | Example |
26+
| -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ |
27+
| Google | [![Test Google OAuth](https://github.com/WilsonLe/payload-oauth2/actions/workflows/test-google-oauth.yml/badge.svg)](https://github.com/WilsonLe/payload-oauth2/actions/workflows/test-google-oauth.yml) | [Config](./examples/google.md) |
1928

2029
# Installation
2130

2231
```
2332
npm install payload-oauth2
2433
yarn install payload-oauth2
25-
```
26-
27-
# Example Usage
28-
29-
Integrating Google OAuth2 to `users` collection.
30-
31-
```ts
32-
export default buildConfig({
33-
// ...
34-
admin: {
35-
importMap: { baseDir: path.resolve(dirname) },
36-
components: {
37-
// A simple button with <a> tag that links to your authorization path
38-
// which defaults to /api/users/oauth/authorize
39-
afterLogin: ["app/components/OAuthLoginButton#OAuthLoginButton"],
40-
},
41-
user: "users", // assuming you already have a users collection with auth enabled
42-
},
43-
// ...
44-
plugins: [
45-
OAuth2Plugin({
46-
enabled: true,
47-
serverURL: process.env.NEXT_PUBLIC_URL || "http://localhost:3000",
48-
authCollection: "users", // assuming you already have a users collection with auth enabled
49-
clientId: process.env.CLIENT_ID || "",
50-
clientSecret: process.env.CLIENT_SECRET || "",
51-
tokenEndpoint: "https://oauth2.googleapis.com/token",
52-
scopes: [
53-
"https://www.googleapis.com/auth/userinfo.email",
54-
"https://www.googleapis.com/auth/userinfo.profile",
55-
"openid",
56-
],
57-
providerAuthorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
58-
getUserInfo: async (accessToken: string) => {
59-
const response = await fetch(
60-
"https://www.googleapis.com/oauth2/v3/userinfo",
61-
{ headers: { Authorization: `Bearer ${accessToken}` } },
62-
);
63-
const user = await response.json();
64-
return { email: user.email, sub: user.sub };
65-
},
66-
successRedirect: () => "/admin",
67-
failureRedirect: () => "/login",
68-
}),
69-
],
70-
// ...
71-
});
34+
pnpm install payload-oauth2
7235
```
7336

7437
# Contributing
@@ -87,4 +50,4 @@ The MIT License (MIT). Please see [License File](LICENSE) for more information.
8750

8851
# Credits
8952

90-
This package was inspired by [Payload Plugin OAuth](https://github.com/thgh/payload-plugin-oauth)
53+
This package was inspired by [Payload Plugin OAuth](https://github.com/thgh/payload-plugin-oauth).

examples/google.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Google OAuth2
2+
3+
```ts
4+
export default buildConfig({
5+
// ...
6+
admin: {
7+
importMap: { baseDir: path.resolve(dirname) },
8+
components: {
9+
// A simple button with <a> tag that links to your authorization path
10+
// which defaults to /api/users/oauth/authorize
11+
afterLogin: ["app/components/OAuthLoginButton#OAuthLoginButton"],
12+
},
13+
user: "users", // assuming you already have a users collection with auth enabled
14+
},
15+
// ...
16+
plugins: [
17+
OAuth2Plugin({
18+
enabled: true,
19+
serverURL: process.env.NEXT_PUBLIC_URL || "http://localhost:3000",
20+
authCollection: "users", // assuming you already have a users collection with auth enabled
21+
clientId: process.env.CLIENT_ID || "",
22+
clientSecret: process.env.CLIENT_SECRET || "",
23+
tokenEndpoint: "https://oauth2.googleapis.com/token",
24+
scopes: [
25+
"https://www.googleapis.com/auth/userinfo.email",
26+
"https://www.googleapis.com/auth/userinfo.profile",
27+
"openid",
28+
],
29+
providerAuthorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
30+
getUserInfo: async (accessToken: string) => {
31+
const response = await fetch(
32+
"https://www.googleapis.com/oauth2/v3/userinfo",
33+
{ headers: { Authorization: `Bearer ${accessToken}` } },
34+
);
35+
const user = await response.json();
36+
return { email: user.email, sub: user.sub };
37+
},
38+
successRedirect: () => "/admin",
39+
failureRedirect: () => "/login",
40+
}),
41+
],
42+
// ...
43+
});
44+
```

test/plugin.spec.ts renamed to test/google.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { runCommand } from "./test-utils";
55
dotenv.config();
66

77
jest.setTimeout(1000 * 60 * 5); // 5 minutes
8-
describe("Plugin tests", () => {
8+
describe("Google OAuth2 Integration", () => {
99
let _stopServer: (() => void) | null = null;
1010
let _serverResult: Promise<string | null> | null = null;
1111
let _browser: Browser | null = null;
@@ -37,7 +37,7 @@ describe("Plugin tests", () => {
3737
console.info("Browser started");
3838
});
3939

40-
it("Google OAuth", async () => {
40+
it("Should work as expected", async () => {
4141
const GOOGLE_TEST_EMAIL = process.env.GOOGLE_TEST_EMAIL;
4242
if (typeof GOOGLE_TEST_EMAIL !== "string") {
4343
throw "GOOGLE_TEST_EMAIL not set";

0 commit comments

Comments
 (0)