Skip to content

Commit

Permalink
Merge pull request #57 from FlemingVincent/feature/profile_update
Browse files Browse the repository at this point in the history
Feature/profile update
  • Loading branch information
flemingvincent authored Nov 15, 2023
2 parents 96bbe6a + a8e41a7 commit 036c97b
Show file tree
Hide file tree
Showing 3 changed files with 408 additions and 50 deletions.
181 changes: 180 additions & 1 deletion src/providers/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type AuthContextProps = {
id: string,
password: string,
) => Promise<void>;
updateUsername: (newUsername: string) => Promise<void>;
updateUserEmail: (newUserEmail: string) => Promise<void>;
updateUserPassword: (newPassword: string) => Promise<void>;
logout: () => Promise<void>;
};

Expand All @@ -37,6 +40,9 @@ export const AuthContext = createContext<AuthContextProps>({
login: async () => {},
verifyOtp: async () => "",
forgotPassword: async () => {},
updateUsername: async () => {},
updateUserEmail: async () => {},
updateUserPassword: async () => {},
logout: async () => {},
});

Expand Down Expand Up @@ -122,6 +128,7 @@ export const AuthProvider = ({ children }: any) => {
username,
first_name: firstName,
last_name: lastName,
avatar_url: null,
});
}
} catch (error) {
Expand Down Expand Up @@ -151,6 +158,7 @@ export const AuthProvider = ({ children }: any) => {
username: dbData![0].username,
first_name: dbData![0].first_name,
last_name: dbData![0].last_name,
avatar_url: dbData![0].avatar_url,
});
}
} catch (error) {
Expand Down Expand Up @@ -195,19 +203,187 @@ export const AuthProvider = ({ children }: any) => {
.eq("id", id)
.eq("email", email);

if (dbError) {
throw dbError;
} else {
const { data: dbData, error: dbError } = await supabase
.from("profiles")
.select("*")
.eq("id", verifyData.user?.id);

if (dbError) {
throw dbError;
} else {
setProfile({
id: verifyData.user!.id,
email: dbData![0].email,
username: dbData![0].username,
first_name: dbData![0].first_name,
last_name: dbData![0].last_name,
avatar_url: dbData![0].avatar_url,
});
}
}
}
} catch (error) {
throw error;
}
};

const updateUsername = async (newUsername: string) => {
try {
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
throw new Error("User not authenticated");
}
const userId = user.id;

const { data: dbData, error: dbError } = await supabase
.from("profiles")
.select("*")
.eq("id", userId);

if (dbError) {
throw dbError;
} else {
setProfile({
id: userId,
email: dbData![0].email,
username: newUsername,
first_name: dbData![0].first_name,
last_name: dbData![0].last_name,
avatar_url: dbData![0].avatar_url,
});
}

const { error: profileError } = await supabase
.from("profiles")
.update({ username: newUsername })
.eq("id", userId);

if (profileError) {
throw profileError;
}

console.log("Username updated successfully");
} catch (error) {
throw error;
}
};

const updateUserEmail = async (newUserEmail: string) => {
try {
// Get the current user
const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error("User not authenticated");
}

const userId = user.id;

// Update email in the authentication system
const { error: updateEmailAuthError } = await supabase.auth.updateUser({
email: newUserEmail,
});

if (updateEmailAuthError) {
console.error(
"Error updating email in authentication system:",
updateEmailAuthError,
);
throw updateEmailAuthError;
} else {
// Update email in the 'profiles' table
const { data: dbData, error: dbError } = await supabase
.from("profiles")
.select("*")
.eq("id", userId);

if (dbError) {
throw dbError;
} else {
setProfile({
id: userId,
email: newUserEmail,
username: dbData![0].username,
first_name: dbData![0].first_name,
last_name: dbData![0].last_name,
avatar_url: dbData![0].avatar_url,
});
}

const { error: profileError } = await supabase
.from("profiles")
.update({ email: newUserEmail })
.eq("id", userId);

if (profileError) {
console.error(
"Error updating email in profiles table:",
profileError,
);
throw profileError;
}
console.log("Email updated successfully");
}

// Successful email update
} catch (error) {
console.error("Error in updateUserEmail:", error);
throw error;
}
};
const updateUserPassword = async (newPassword: string) => {
try {
// Get the current user
const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error("User not authenticated");
}

const userId = user.id;

// Update email in the authentication system
const { error: updateError } = await supabase.auth.updateUser({
password: newPassword,
});

if (updateError) {
throw updateError;
} else {
const { data: dbData, error: dbError } = await supabase
.from("profiles")
.select("*")
.eq("id", userId);

if (dbError) {
throw dbError;
} else {
setProfile({
id: dbData![0].id,
id: userId,
email: dbData![0].email,
username: dbData![0].username,
first_name: dbData![0].first_name,
last_name: dbData![0].last_name,
avatar_url: dbData![0].avatar_url,
});
}
console.log("Password updated successfully");
}

// Update email in the 'profiles' table

// Successful email update
} catch (error) {
console.error("Error in updateUserEmail:", error);
throw error;
}
};
Expand Down Expand Up @@ -244,6 +420,9 @@ export const AuthProvider = ({ children }: any) => {
login,
verifyOtp,
forgotPassword,
updateUsername,
updateUserEmail,
updateUserPassword,
logout,
}}
>
Expand Down
Loading

0 comments on commit 036c97b

Please sign in to comment.