payload-auth-plugin
Version:
Authentication plugin for Payload CMS
70 lines (63 loc) • 1.69 kB
text/typescript
import type * as oauth from "oauth4webapi"
import type {
OAuth2ProviderConfig,
AccountInfo,
OAuthBaseProviderConfig,
} from "../../types.js"
const authorization_server: oauth.AuthorizationServer = {
issuer: "https://github.com",
authorization_endpoint: "https://github.com/login/oauth/authorize",
token_endpoint: "https://github.com/login/oauth/access_token",
userinfo_endpoint: "https://api.github.com/user",
}
type GitHubAuthConfig = OAuthBaseProviderConfig
/**
* Add Github OAuth2 Provider
*
* #### Callback or Redirect URL pattern
*
* ```
* https://example.com/api/{name}/oauth/callback/github
* ```
*
* #### Plugin Setup
*
* ```ts
* import { Plugin } from 'payload'
* import {authPlugin} from "payload-auth-plugin"
* import {GithubAuthProvider} from "payload-auth-plugin/providers"
*
* export const plugins: Plugin[] = [
* authPlugin({
* providers:[
* GithubAuthProvider({
* client_id: process.env.GITHUB_CLIENT_ID as string,
* client_secret: process.env.GITHUB_CLIENT_SECRET as string,
* })
* ]
* })
* ]
* ```
*
*/
function GitHubAuthProvider(config: GitHubAuthConfig): OAuth2ProviderConfig {
const { overrideScope, ...restConfig } = config
return {
...restConfig,
id: "github",
scope: overrideScope ?? "openid email profile",
authorization_server,
name: "GitHub",
algorithm: "oauth2",
kind: "oauth",
profile: (profile): AccountInfo => {
return {
sub: profile.id as string,
name: profile.name as string,
email: profile.email as string,
picture: profile.picture as string,
}
},
}
}
export default GitHubAuthProvider