postiz-app/apps/backend/src/services/auth/providers/github.provider.ts
2024-06-05 13:13:36 +07:00

54 lines
1.5 KiB
TypeScript

import { ProvidersInterface } from '@gitroom/backend/services/auth/providers.interface';
export class GithubProvider implements ProvidersInterface {
generateLink(): string {
return `https://github.com/login/oauth/authorize?client_id=${
process.env.GITHUB_CLIENT_ID
}&scope=user:email&redirect_uri=${encodeURIComponent(
`${process.env.FRONTEND_URL}/settings`
)}`;
}
async getToken(code: string): Promise<string> {
const { access_token } = await (
await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({
client_id: process.env.GITHUB_CLIENT_ID,
client_secret: process.env.GITHUB_CLIENT_SECRET,
code,
redirect_uri: `${process.env.FRONTEND_URL}/settings`,
}),
})
).json();
return access_token;
}
async getUser(access_token: string): Promise<{ email: string; id: string }> {
const data = await (
await fetch('https://api.github.com/user', {
headers: {
Authorization: `token ${access_token}`,
},
})
).json();
const [{ email }] = await (
await fetch('https://api.github.com/user/emails', {
headers: {
Authorization: `token ${access_token}`,
},
})
).json();
return {
email: email,
id: String(data.id),
};
}
}