| 1 | import { |
| 2 | ProfileDescription, |
| 3 | SerializedOrgWithProfiles, |
| 4 | } from "core/config/ProfileLifecycleManager"; |
| 5 | import { ControlPlaneSessionInfo } from "core/control-plane/AuthTypes"; |
| 6 | import React, { |
| 7 | createContext, |
| 8 | useCallback, |
| 9 | useContext, |
| 10 | useEffect, |
| 11 | useState, |
| 12 | } from "react"; |
| 13 | import { useWebviewListener } from "../hooks/useWebviewListener"; |
| 14 | import { useAppDispatch, useAppSelector } from "../redux/hooks"; |
| 15 | import { setConfigLoading } from "../redux/slices/configSlice"; |
| 16 | import { |
| 17 | selectCurrentOrg, |
| 18 | selectSelectedProfile, |
| 19 | setOrganizations, |
| 20 | setSelectedOrgId, |
| 21 | } from "../redux/slices/profilesSlice"; |
| 22 | import { IdeMessengerContext } from "./IdeMessenger"; |
| 23 | |
| 24 | interface AuthContextType { |
| 25 | session: ControlPlaneSessionInfo | undefined; |
| 26 | logout: () => void; |
| 27 | login: (useOnboarding: boolean) => Promise<boolean>; |
| 28 | selectedProfile: ProfileDescription | null; |
| 29 | profiles: ProfileDescription[] | null; |
| 30 | refreshProfiles: (reason?: string) => Promise<void>; |
| 31 | organizations: SerializedOrgWithProfiles[]; |
| 32 | } |
| 33 | |
| 34 | const AuthContext = createContext<AuthContextType | undefined>(undefined); |
| 35 | |
| 36 | export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ |
| 37 | children, |
| 38 | }) => { |
| 39 | const dispatch = useAppDispatch(); |
| 40 | const ideMessenger = useContext(IdeMessengerContext); |
| 41 | // Session |
| 42 | const [session, setSession] = useState<ControlPlaneSessionInfo | undefined>( |
| 43 | undefined, |
| 44 | ); |
| 45 | |
| 46 | // Orgs |
| 47 | const orgs = useAppSelector((store) => store.profiles.organizations); |
| 48 | |
| 49 | // Profiles |
| 50 | const currentOrg = useAppSelector(selectCurrentOrg); |
| 51 | const selectedProfile = useAppSelector(selectSelectedProfile); |
| 52 | |
| 53 | const login: AuthContextType["login"] = (useOnboarding: boolean) => { |
| 54 | return new Promise(async (resolve) => { |
| 55 | await ideMessenger |
| 56 | .request("getControlPlaneSessionInfo", { |
| 57 | silent: false, |
| 58 | useOnboarding, |
| 59 | }) |
| 60 | .then((result) => { |
| 61 | if (result.status === "error") { |
| 62 | resolve(false); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | const session = result.content; |
| 67 | setSession(session); |
| 68 | |
| 69 | resolve(true); |
| 70 | }); |
| 71 | }); |
| 72 | }; |
| 73 | |
| 74 | const logout = () => { |
| 75 | ideMessenger.post("logoutOfControlPlane", undefined); |
| 76 | dispatch(setOrganizations(orgs.filter((org) => org.id === "personal"))); |
| 77 | dispatch(setSelectedOrgId("personal")); |
| 78 | setSession(undefined); |
| 79 | }; |
| 80 | |
| 81 | useEffect(() => { |
| 82 | async function init() { |
| 83 | const result = await ideMessenger.request("getControlPlaneSessionInfo", { |
| 84 | silent: true, |
| 85 | useOnboarding: false, |
| 86 | }); |
| 87 | if (result.status === "success") { |
| 88 | setSession(result.content); |
| 89 | } |
| 90 | } |
| 91 | void init(); |
| 92 | }, []); |
| 93 | |
| 94 | useWebviewListener( |
| 95 | "sessionUpdate", |
| 96 | async (data) => { |
| 97 | setSession(data.sessionInfo); |
| 98 | }, |
| 99 | [], |
| 100 | ); |
| 101 | |
| 102 | const refreshProfiles = useCallback( |
| 103 | async (reason?: string) => { |
| 104 | try { |
| 105 | dispatch(setConfigLoading(true)); |
| 106 | await ideMessenger.request("config/refreshProfiles", { |
| 107 | reason, |
| 108 | }); |
| 109 | ideMessenger.post("showToast", ["info", "Config refreshed"]); |
| 110 | } catch (e) { |
| 111 | console.error("Failed to refresh profiles", e); |
| 112 | ideMessenger.post("showToast", ["error", "Failed to refresh config"]); |
| 113 | } finally { |
| 114 | dispatch(setConfigLoading(false)); |
| 115 | } |
| 116 | }, |
| 117 | [ideMessenger], |
| 118 | ); |
| 119 | |
| 120 | return ( |
| 121 | <AuthContext.Provider |
| 122 | value={{ |
| 123 | session, |
| 124 | logout, |
| 125 | login, |
| 126 | selectedProfile, |
| 127 | profiles: currentOrg?.profiles ?? [], |
| 128 | refreshProfiles, |
| 129 | organizations: orgs, |
| 130 | }} |
| 131 | > |
| 132 | {children} |
| 133 | </AuthContext.Provider> |
| 134 | ); |
| 135 | }; |
| 136 | |
| 137 | export const useAuth = (): AuthContextType => { |
| 138 | const context = useContext(AuthContext); |
| 139 | if (!context) { |
| 140 | throw new Error("useAuth must be used within an AuthProvider"); |
| 141 | } |
| 142 | return context; |
| 143 | }; |