From 6adb032f4fe201322b23780ad927b7e9ec69f2e7 Mon Sep 17 00:00:00 2001 From: moizahmedd Date: Wed, 23 Oct 2024 16:06:05 -0400 Subject: [PATCH 1/6] create user object model --- src/models/user.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/models/user.ts 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 From 86aa456433f7cfcd86d25ec99216cc4a74acdec1 Mon Sep 17 00:00:00 2001 From: moizahmedd Date: Wed, 23 Oct 2024 16:06:31 -0400 Subject: [PATCH 2/6] add fetch user details by email request --- src/services/accountsService.ts | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) 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(); }; From 2d72b81484034775a67190ba9eb498da987a58b0 Mon Sep 17 00:00:00 2001 From: moizahmedd Date: Wed, 23 Oct 2024 16:08:05 -0400 Subject: [PATCH 3/6] expand authenticate user object to take credential bundle, and set user --- src/context/AuthContext.tsx | 39 ++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index 19171e6..c1170b1 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,11 @@ 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; loading: boolean; // Add loading state to context } @@ -44,6 +53,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 +107,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 ( From cc058b58139eea8889bde2039f121ae1280e1b1b Mon Sep 17 00:00:00 2001 From: moizahmedd Date: Wed, 23 Oct 2024 16:13:02 -0400 Subject: [PATCH 4/6] add user object to auth context --- src/context/AuthContext.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index c1170b1..cf67b3a 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -41,6 +41,7 @@ interface AuthContextProps { credentialBundle: string, onSuccess: (token: string) => void ) => void; + user: UserObject | null; loading: boolean; // Add loading state to context } @@ -140,6 +141,7 @@ export const AuthProvider = ({ verifyOtp: handleVerifyOtp, authenticateUser: handleAuthenticateUser, loading, + user, }} > {children} From 55bf8161746d733577e97a3446937f4d84c75cfc Mon Sep 17 00:00:00 2001 From: moizahmedd Date: Wed, 23 Oct 2024 16:13:16 -0400 Subject: [PATCH 5/6] send credential bundle to authenticate user function --- src/components/Auth/OtpInput.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 }); } From 7bfb7dce14785e6c1fbf5d6018e36f56e0a8c709 Mon Sep 17 00:00:00 2001 From: moizahmedd Date: Wed, 23 Oct 2024 16:13:35 -0400 Subject: [PATCH 6/6] show wallet address on successful auth --- src/components/Auth/SuccessPage.tsx | 4 ++++ 1 file changed, 4 insertions(+) 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}
); };