Skip to content

Commit

Permalink
Update AuthContext.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Oct 3, 2024
1 parent 203b11a commit b7c31ac
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions web-app/src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,18 @@ export const AuthProvider = ({ children }: any) => {
const SESSION_KEY = "did";

const { provider: ethProvider, sdk } = useSDK();
const [isSDKConnected, setIsSDKConnected] = useState(false);

useEffect(() => {
const checkSDKConnection = async () => {
if (sdk && typeof sdk.isInitialized === 'function') {
const connected = sdk.isInitialized();
setIsSDKConnected(connected);
}
};

checkSDKConnection();
}, [sdk]);

const [session, setSession] = useState<DIDSession | undefined>();
const [status, setStatus] = useState<AuthStatus>(AuthStatus.IDLE);
Expand Down Expand Up @@ -91,13 +102,23 @@ export const AuthProvider = ({ children }: any) => {
}, [session]);

const startSession = useCallback(async (): Promise<boolean> => {

if (!ethProvider) {
return false
if (!ethProvider || !sdk) {
console.warn("Ethereum provider or SDK not available");
return false;
}

try {
const accounts = await sdk?.connect();
if (!isSDKConnected) {
const accounts = await sdk.connect();
if (!accounts || accounts.length === 0) {
throw new Error("Failed to connect accounts");
}
}

const accounts = await ethProvider.request({ method: 'eth_accounts' });
if (!accounts || accounts.length === 0) {
throw new Error("No accounts available");
}

const accountId = await getAccountId(ethProvider, accounts?.[0]);
const normAccount = normalizeAccountId(accountId);
Expand Down Expand Up @@ -142,7 +163,7 @@ export const AuthProvider = ({ children }: any) => {
console.error("Error starting session:", error);
return false;
}
}, [ethProvider]);
}, [ethProvider, sdk, isSDKConnected]);

const authenticate = useCallback(async () => {

Expand Down

0 comments on commit b7c31ac

Please sign in to comment.