-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
base: main
Are you sure you want to change the base?
Feat/add gitee provider #12475
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"; | ||
|
||
return { | ||
id: "gitee", | ||
name: "Gitee", | ||
type: "oauth", | ||
authorization: { | ||
url: `${baseUrl}/oauth/authorize`, | ||
params: { | ||
scope: "", | ||
}, | ||
}, | ||
Comment on lines
+31
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
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, | ||
}; | ||
} |
There was a problem hiding this comment.
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