Skip to content

Commit

Permalink
Merge pull request #3 from DIMO-Network/moiz/user-object
Browse files Browse the repository at this point in the history
Fetching User Support
  • Loading branch information
MoizAhmedd authored Oct 28, 2024
2 parents 13d7f66 + 7bfb7dc commit 6100f29
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/components/Auth/OtpInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const OtpInput: React.FC<OtpInputProps> = ({ 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
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/Auth/SuccessPage.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="text-green-500 text-lg">
Authentication successful! You are logged in.
<br></br>
Wallet Address:{user?.walletAddress}
</div>
);
};
Expand Down
41 changes: 34 additions & 7 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand All @@ -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
}

Expand All @@ -44,6 +54,7 @@ export const AuthProvider = ({
children: ReactNode;
}): JSX.Element => {
const [loading, setLoading] = useState(false);
const [user, setUser] = useState<UserObject | null>(null);
const [error, setError] = useState<string | null>(null);

const handleSendOtp = async (
Expand Down Expand Up @@ -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 (
Expand All @@ -115,6 +141,7 @@ export const AuthProvider = ({
verifyOtp: handleVerifyOtp,
authenticateUser: handleAuthenticateUser,
loading,
user,
}}
>
{children}
Expand Down
11 changes: 11 additions & 0 deletions src/models/user.ts
Original file line number Diff line number Diff line change
@@ -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;
}

32 changes: 21 additions & 11 deletions src/services/accountsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
};

0 comments on commit 6100f29

Please sign in to comment.