mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-13 09:15:37 +02:00
⭐feat(auth): Update the authentication UI (#153)
We added a new Auth UI, with all the business logic to handle profiles and such... it works alright
This commit is contained in:
75
packages/functions/src/utils.ts
Normal file
75
packages/functions/src/utils.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
export const handleGithub = async (accessKey: string) => {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
export const handleDiscord = async (accessKey: string) => {
|
||||
try {
|
||||
const response = await fetch("https://discord.com/api/v10/users/@me", {
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessKey}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Discord API error: ${response.status}`);
|
||||
}
|
||||
|
||||
const user = await response.json();
|
||||
// console.log("raw user", user)
|
||||
if (!user.verified) {
|
||||
throw new Error("Email not verified");
|
||||
}
|
||||
|
||||
return {
|
||||
primary: {
|
||||
email: user.email,
|
||||
verified: user.verified,
|
||||
primary: true
|
||||
},
|
||||
avatar: user.avatar
|
||||
? `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`
|
||||
: null,
|
||||
username: user.global_name ?? user.username
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Discord OAuth error:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user