Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add gitee provider #12475

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/public/img/providers/gitee.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 94 additions & 0 deletions packages/core/src/providers/gitee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* <div class="provider" style={{backgroundColor: "#24292f", display: "flex", justifyContent: "space-between", color: "#fff", padding: 16}}>
* <span>Built-in <b>Gitee</b> integration.</span>
* <a href="https://gitee.com">
* <img style={{display: "block"}} src="https://authjs.dev/img/providers/gitee.svg" height="48" width="48"/>
* </a>
* </div>
*
* @module providers/gitee
*/
import type { OAuthConfig, OAuthUserConfig } from "./index.js"
import type { TokenSet } from "../types.js"
export interface GiteeProfile {
id: number;
name?: string;
login: string;
email?: string;
avatar_url?: string;
}

export default function Gitee(
config: OAuthUserConfig<GiteeProfile>
): OAuthConfig<GiteeProfile> {
const baseUrl = "https://gitee.com";
const apiBaseUrl = "https://gitee.com/api/v5";
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to use these two variables, since apiBaseUrl only adds the path /api/v5 to baseUrl


return {
id: "gitee",
name: "Gitee",
type: "oauth",
authorization: {
url: `${baseUrl}/oauth/authorize`,
params: {
scope: "",
},
},
Comment on lines +31 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no additional configuration is needed, there’s no need to use the object. Simply use the API route directly.

token: {
url: `${baseUrl}/oauth/token`,
params: {
grant_type: "authorization_code",
},
},
Comment on lines +37 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The grant_type is set to authorization_code by default, so there’s no need to use the object.

userinfo: {
url: `${apiBaseUrl}/user`,
request: async function ({
tokens,
provider,
}: {
tokens: TokenSet;
provider: {
token?: {
url: URL;
};
userinfo?: {
url?: URL;
};
};
}) {
const profile = await fetch(provider.userinfo?.url as URL, {
headers: {
Authorization: `Bearer ${tokens.access_token}`,
"User-Agent": "authjs",
},
}).then(async (res) => await res.json());

if (!profile.email) {
const res = await fetch(`${apiBaseUrl}/user/emails`, {
headers: {
Authorization: `Bearer ${tokens.access_token}`,
"User-Agent": "authjs",
},
});

if (res.ok) {
const emails: { primary: boolean; email: string }[] =
await res.json();
profile.email = (emails.find((e) => e.primary) ?? emails[0])?.email;
}
}

return profile;
},
},
profile(profile) {
return {
id: profile.id.toString(),
name: profile.name ?? profile.login,
email: profile.email,
image: profile.avatar_url,
};
},
options: config,
};
}
Loading