diff --git a/src/components/Auth/OtpInput.tsx b/src/components/Auth/OtpInput.tsx index 53ad045..22bc7da 100644 --- a/src/components/Auth/OtpInput.tsx +++ b/src/components/Auth/OtpInput.tsx @@ -16,8 +16,8 @@ const OtpInput: React.FC = ({ setAuthStep, email, otpId }) => { // Verify OTP using the auth context const result = await verifyOtp(email, otp, otpId); - if ( result.success ) { - authenticateUser(email, () => { + if ( result.success && result.credentialBundle ) { + authenticateUser(email, result.credentialBundle, () => { setAuthStep(2); // Move to success page after authentication }); } diff --git a/src/components/Auth/SuccessPage.tsx b/src/components/Auth/SuccessPage.tsx index 972531d..8870059 100644 --- a/src/components/Auth/SuccessPage.tsx +++ b/src/components/Auth/SuccessPage.tsx @@ -1,10 +1,14 @@ // src/components/SuccessPage.tsx import React from "react"; +import { useAuthContext } from "../../context/AuthContext"; const SuccessPage: React.FC = () => { + const { user } = useAuthContext(); // Get verifyOtp from the context return (
Authentication successful! You are logged in. +

+ Wallet Address:{user?.walletAddress}
); }; diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index 19171e6..cf67b3a 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -19,8 +19,13 @@ */ import React, { createContext, useContext, ReactNode, useState } from "react"; -import { sendOtp, verifyOtp } from "../services/accountsService"; // Import the service functions +import { + fetchUserDetails, + sendOtp, + verifyOtp, +} from "../services/accountsService"; // Import the service functions import { authenticateUser } from "../utils/authUtils"; +import { UserObject } from "../models/user"; interface AuthContextProps { sendOtp: ( @@ -31,7 +36,12 @@ interface AuthContextProps { otp: string, otpId: string ) => Promise<{ success: boolean; credentialBundle?: string; error?: string }>; - authenticateUser: (email: string, onSuccess: (token: string) => void) => void; + authenticateUser: ( + email: string, + credentialBundle: string, + onSuccess: (token: string) => void + ) => void; + user: UserObject | null; loading: boolean; // Add loading state to context } @@ -44,6 +54,7 @@ export const AuthProvider = ({ children: ReactNode; }): JSX.Element => { const [loading, setLoading] = useState(false); + const [user, setUser] = useState(null); const [error, setError] = useState(null); const handleSendOtp = async ( @@ -97,15 +108,30 @@ export const AuthProvider = ({ } }; - const handleAuthenticateUser = ( + const handleAuthenticateUser = async ( email: string, + credentialBundle: string, onSuccess: (token: string) => void ) => { setLoading(true); - authenticateUser(email, (token) => { - onSuccess(token); - setLoading(false); // Only handle loading in the provider - }); + setError(null); + + try { + const userDetailsResponse = await fetchUserDetails(email); + if (!userDetailsResponse.success || !userDetailsResponse.user) { + throw new Error("Failed to fetch user details"); + } + const user = userDetailsResponse.user; + console.log(user); + setUser(user); // Store the user object in the context + authenticateUser(email, (token) => { + onSuccess(token); + }); + } catch (error) { + console.error(error); + } finally { + setLoading(false); + } }; return ( @@ -115,6 +141,7 @@ export const AuthProvider = ({ verifyOtp: handleVerifyOtp, authenticateUser: handleAuthenticateUser, loading, + user, }} > {children} diff --git a/src/models/user.ts b/src/models/user.ts new file mode 100644 index 0000000..ed8012a --- /dev/null +++ b/src/models/user.ts @@ -0,0 +1,11 @@ +// src/models/account.ts + +export interface UserObject { + email: string; + subOrganizationId: string | null; + hasPasskey: boolean; + smartContractAddress: string | null; + walletAddress: string | null; + emailVerified: boolean; + } + \ No newline at end of file diff --git a/src/services/accountsService.ts b/src/services/accountsService.ts index 11f2b30..2730983 100644 --- a/src/services/accountsService.ts +++ b/src/services/accountsService.ts @@ -6,6 +6,7 @@ * */ +import { UserObject } from "../models/user"; import { generateTargetPublicKey } from "../utils/authUtils"; const DIMO_ACCOUNTS_BASE_URL = process.env.REACT_APP_DIMO_ACCOUNTS_URL || 'https://accounts.dev.dimo.org'; @@ -118,16 +119,25 @@ export const deployAccount = async (email: string): Promise<{ success: boolean } return { success: true }; }; -// Check if an account exists -export const checkAccountExists = async (email: string) => { - const response = await fetch(`${DIMO_ACCOUNTS_BASE_URL}/account/${email}`, { - method: "GET", - headers: { - "Content-Type": "application/json", - }, - }); - if (!response.ok) { - throw new Error("Account lookup failed"); +// src/services/authService.ts +export const fetchUserDetails = async (email: string): Promise<{ success: boolean; error?: string; user?: UserObject }> => { + try { + const response = await fetch(`${DIMO_ACCOUNTS_BASE_URL}/account/${email}`, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + + if (!response.ok) { + const errorData = await response.json(); + return { success: false, error: errorData.message || 'Failed to fetch user details' }; + } + + const user = await response.json(); + return { success: true, user: {...user, email} }; + } catch (error) { + console.error('Error fetching user details:', error); + return { success: false, error: 'An error occurred while fetching user details' }; } - return await response.json(); };