How to Validate an Old Password Against an Encrypted Password in React Native?

I am a computer science student working on a project with friends, handling the frontend. The submission deadline is today, and I’ve been struggling with a password issue for days.

I need to implement a feature where users can update their profile information only if the entered “old password” matches the existing password stored in their account. Before integrating with the backend, I simply checked if the two variables matched. However, after the integration, I realized the stored passwords are encrypted, and I need to figure out how to compare the entered password with the encrypted password.

I’ve tried using GPT and searching online, but since this is my first project, I’m feeling lost. I really need help.

<Frontend (React Native) Code>

const handleSave = async () => {
    // Password validation
    if (profile.newPassword.length < 10) {
        Alert.alert('Password Error', 'The password must be at least 10 characters long.');
        return;
    }

    if (profile.newPassword !== profile.confirmPassword) {
        Alert.alert('Password Error', 'The new password and the confirmed password do not match.');
        return;
    }

    try {
        const existingProfileData = await AsyncStorage.getItem('profile');
        const existingProfile = existingProfileData ? JSON.parse(existingProfileData) : {};

        // Validate old password
        if (existingProfile.password !== profile.oldPassword) {
            Alert.alert('Password Error', 'The old password is incorrect.');
            return;
        }

        await updateUserInfo();
        Alert.alert('Save Completed', 'Profile information has been saved.');
        navigation.goBack();
    } catch (error) {
        console.error('Profile save failed:', error);
    }
};

Backend (Spring) Code

@Transactional
public JoinRequestDTO editInfo(@AuthenticationPrincipal PrincipalDetails principalDetails, JoinRequestDTO joinRequestDTO, String currentPassword) {
    usersEntity user = userRepository.findByStudentId_StudentId(principalDetails.getUsername())
            .orElseThrow(() -> new UsernameNotFoundException("User not found."));

    // Validate if the entered current password matches the stored password
    if (passwordEncoder.matches(currentPassword, user.getPassword())) {
        // Update nickname, keep existing info if null
        if (joinRequestDTO.getUsersDTO().getNickName() != null && !joinRequestDTO.getUsersDTO().getNickName().isEmpty()) {
            user.setNickName(joinRequestDTO.getUsersDTO().getNickName());
        }

        // Update password
        if (joinRequestDTO.getUsersDTO().getPassword() != null && !joinRequestDTO.getUsersDTO().getPassword().isEmpty()) {
            user.setPassword(passwordEncoder.encode(joinRequestDTO.getUsersDTO().getPassword()));
        }
    }
    userRepository.save(user);
    return joinRequestDTO;
}

I’ve tried so many different things over the past few days that I can’t remember them all…