Github linking works

This commit is contained in:
austinkelsay 2024-12-09 12:43:33 -06:00
parent 2cef7e6cc9
commit c87ccb8c2d
No known key found for this signature in database
GPG Key ID: 44CB4EC6D9F2FA02
3 changed files with 85 additions and 10 deletions

View File

@ -1,7 +1,8 @@
import React, { useState, useEffect } from 'react';
import { ProgressBar } from 'primereact/progressbar';
import { Accordion, AccordionTab } from 'primereact/accordion';
import { useSession, signIn } from 'next-auth/react';
import { useSession, signIn, getSession } from 'next-auth/react';
import { useRouter } from 'next/router';
import GenericButton from '@/components/buttons/GenericButton';
const allTasks = [
@ -54,6 +55,7 @@ const UserProgress = () => {
const [completedCourses, setCompletedCourses] = useState([]);
const [tasks, setTasks] = useState([]);
const router = useRouter();
const { data: session, update } = useSession();
useEffect(() => {
@ -78,10 +80,10 @@ const UserProgress = () => {
if (task.status === 'Connect GitHub') {
return {
...task,
completed: session?.githubProfile ? true : false,
completed: session?.account?.provider === 'github' ? true : false,
subTasks: task.subTasks ? task.subTasks.map(subTask => ({
...subTask,
completed: session?.githubProfile ? true : false
completed: session?.account?.provider === 'github' ? true : false
})) : undefined
};
}
@ -102,7 +104,7 @@ const UserProgress = () => {
const calculateProgress = (completedCourseIds) => {
let progressValue = 0;
if (session?.githubProfile) {
if (session?.account?.provider === 'github') {
progressValue += 25;
}
@ -125,7 +127,7 @@ const UserProgress = () => {
tier = 'Junior Dev';
} else if (completedCourseIds.includes("f6daa88a-53d6-4901-8dbd-d2203a05b7ab")) {
tier = 'New Dev';
} else if (session?.githubProfile) {
} else if (session?.account?.provider === 'github') {
tier = 'Pleb';
}
@ -140,9 +142,32 @@ const UserProgress = () => {
};
const handleGitHubLink = async () => {
if (!session?.user?.id) return;
await signIn("github");
try {
// If user is already signed in, we'll link the accounts
if (session?.user) {
const result = await signIn("github", {
redirect: false,
// Pass existing user data for linking
userId: session.user.id,
pubkey: session.user.pubkey,
privkey: session.user.privkey
});
if (result?.ok) {
// Wait for session update
await new Promise(resolve => setTimeout(resolve, 1000));
const updatedSession = await getSession();
if (updatedSession?.account?.provider === 'github') {
router.push('/'); // Accounts linked successfully
}
}
} else {
// Normal GitHub sign in
await signIn("github");
}
} catch (error) {
console.error("GitHub sign in error:", error);
}
};
return (

View File

@ -0,0 +1,18 @@
{
"kind": 30009,
"tags": [
["d", "junior_dev_2024"],
["name", "Junior Developer 2024"],
["description", "Awarded upon completion of the Junior Developer course track"],
["image", "https://yourplatform.com/badges/junior-dev.png", "1024x1024"],
["thumb", "https://yourplatform.com/badges/junior-dev_256x256.png", "256x256"],
["thumb", "https://yourplatform.com/badges/junior-dev_64x64.png", "64x64"]
]
}
Key points for implementation:
Use a unique identifier in the d tag that includes the course level and year
Include high-res (1024x1024) badge images and multiple thumbnail sizes
Create Badge Award events (kind 8) to assign badges to students who complete courses
Students can then choose to display badges via Profile Badges events (kind 30008)

View File

@ -247,11 +247,43 @@ export const authOptions = {
}
return session;
},
async jwt({ token, user, account }) {
async jwt({ token, user, account, profile }) {
// If we are linking a github account to an existing email or anon account (we have privkey)
if (account?.provider === "github" && user?.id && user?.pubkey && user?.privkey) {
console.log("Linking GitHub account to existing user", account, profile);
try {
// First update the user's profile with GitHub info
const updatedUser = await updateUser(user.id, {
name: profile?.login || profile?.name,
username: profile?.login || profile?.name,
avatar: profile?.avatar_url,
image: profile?.avatar_url,
});
console.log("Updated user", updatedUser);
// Get the updated user
const existingUser = await getUserById(updatedUser?.id);
if (existingUser) {
token.user = existingUser;
}
} catch (error) {
console.error("Error linking GitHub account:", error);
}
}
// If we are linking a github account to a nostr account (we do not have privkey)
if (account?.provider === "github" && account?.userId && account?.pubkey) {
try {
// I think we just need auth + account in session and thats it?
} catch (error) {
console.error("Error linking GitHub account:", error);
}
}
if (user) {
token.user = user;
}
// Also store the account info in the token if it exists
if (account) {
token.account = account;
}