File size: 2,824 Bytes
6f5b644
dc06026
 
 
6f5b644
dc06026
 
 
 
 
6f5b644
dc06026
6f5b644
dc06026
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f5b644
 
 
 
 
dc06026
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { redirect } from "@remix-run/node";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { getSession, commitSession, destroySession } from "~/lib/session.server";
import { accountLinkingService } from "~/lib/account-linking.server";

export async function loader({ request }: LoaderFunctionArgs) {
  const url = new URL(request.url);
  const service = url.searchParams.get("service");
  const unlink = url.searchParams.get("unlink") === "true";
  
  const session = await getSession(request.headers.get("Cookie"));
  const userSession = session.get("user");
  
  // If no user session, just redirect to home
  if (!userSession) {
    return redirect("/");
  }

  // Handle account unlinking
  if (unlink && userSession.isLinked && userSession.github) {
    console.log(`πŸ”„ Unlinking accounts for GitHub user: ${userSession.github.login}`);
    try {
      // Remove the link from storage
      accountLinkingService.removeLink(userSession.github.userId);
      
      // Update session to reflect unlinked status
      userSession.isLinked = false;
      delete userSession.linkedAt;
      
      // Save updated session
      session.set("user", userSession);
      
      return redirect("/?message=accounts_unlinked", {
        headers: {
          "Set-Cookie": await commitSession(session),
        },
      });
    } catch (error) {
      console.error("Error unlinking accounts:", error);
      return redirect("/?error=unlink_failed");
    }
  }

  // Handle selective logout by service
  if (service) {
    console.log(`πŸ”„ Logging out of ${service} service`);
    
    if (service === "github" && userSession.github) {
      // Remove GitHub info but keep HuggingFace if present
      delete userSession.github;
      
      // If linked, update linking status
      if (userSession.isLinked) {
        userSession.isLinked = false;
        delete userSession.linkedAt;
      }
      
      // Save updated session
      session.set("user", userSession);
      
      return redirect("/", {
        headers: {
          "Set-Cookie": await commitSession(session),
        },
      });
    }
    
    if (service === "huggingface" && userSession.huggingface) {
      // Remove HuggingFace info but keep GitHub if present
      delete userSession.huggingface;
      
      // If linked, update linking status
      if (userSession.isLinked) {
        userSession.isLinked = false;
        delete userSession.linkedAt;
      }
      
      // Save updated session
      session.set("user", userSession);
      
      return redirect("/", {
        headers: {
          "Set-Cookie": await commitSession(session),
        },
      });
    }
  }

  // Full logout (destroy entire session)
  return redirect("/", {
    headers: {
      "Set-Cookie": await destroySession(session),
    },
  });
}