Files
netris-nestri/packages/functions/src/auth/utils/github.ts
Wanjohi a0dc353561 🐜 fix: Fix an issue where ts-server is taking forever to load (#272)
## Description
<!-- Briefly describe the purpose and scope of your changes -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
  - Centralized and standardized error response schemas for APIs.
- Utility functions for result formatting and enhanced validation error
handling.
  - New utility modules for authentication and OAuth provider handling.
  - Added Discord OAuth user data fetching with email verification.

- **Bug Fixes**
- Improved error safety in cloud task creation by preventing potential
runtime errors.

- **Refactor**
- Major simplification and reorganization of API routes and
authentication logic.
  - Migration from valibot to zod for schema validation.
  - Streamlined import paths and consolidated utility exports.
- Simplified TypeScript and .gitignore configuration for easier
maintenance.
  - Disabled machine authentication provider and related logic.

- **Chores**
- Removal of unused or deprecated API endpoints, database migration, and
permissions deployment code.
- Updated package dependencies and scripts for improved reliability and
performance.
  - Enhanced documentation and updated project metadata.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-06 05:22:26 +03:00

42 lines
1.3 KiB
TypeScript

import fetch from "node-fetch";
export const handleGithub = async (accessKey: string) => {
console.log("acceskey", accessKey)
const headers = {
Authorization: `token ${accessKey}`,
Accept: "application/vnd.github.v3+json",
"User-Agent": "Nestri"
};
try {
const [emails, user] = await Promise.all([
fetch("https://api.github.com/user/emails", { headers }).then(r => {
if (!r.ok) throw new Error(`Failed to fetch emails: ${r.status}`);
return r.json();
}),
fetch("https://api.github.com/user", { headers }).then(r => {
if (!r.ok) throw new Error(`Failed to fetch user: ${r.status}`);
return r.json();
})
]);
const primaryEmail = emails.find((email: { primary: boolean }) => email.primary);
if (!primaryEmail.verified) {
throw new Error("Email not verified");
}
// console.log("raw user", user)
const { email, primary, verified } = primaryEmail;
return {
primary: { email, primary, verified },
avatar: user.avatar_url,
username: user.name ?? user.login
};
} catch (error) {
console.error('GitHub OAuth error:', error);
throw error;
}
}