Skip to content

Commit

Permalink
Use rpc to check if password is the same (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 authored Apr 9, 2024
1 parent 507523a commit f9f6ed6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/app/auth/resetPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import colors from '../../../styles/colors';
import globalStyles from '../../../styles/globalStyles';
import { useSession } from '../../../utils/AuthContext';
import PasswordComplexityText from '../../../components/PasswordComplexityText/PasswordComplexityText';
import { isPasswordSameAsBefore } from '../../../queries/profiles';

function ResetPasswordScreen() {
const { updateUser, signOut } = useSession();
const { session, updateUser, signOut } = useSession();
const [password, setPassword] = useState('');
const [passwordTextHidden, setPasswordTextHidden] = useState(true);
const [confirmPassword, setConfirmPassword] = useState('');
Expand All @@ -36,6 +37,9 @@ function ResetPasswordScreen() {

const checkPassword = (text: string) => {
if (text !== '') {
isPasswordSameAsBefore(text, session?.user?.id).then(isSame =>
setIsDifferent(!isSame),
);
setHasUppercase(text !== text.toLowerCase());
setHasLowercase(text !== text.toUpperCase());
setHasNumber(/[0-9]/.test(text));
Expand Down Expand Up @@ -70,6 +74,7 @@ function ResetPasswordScreen() {
const { error } = await updateUser({ password });

if (error) {
console.error(error);
Alert.alert('Updating password failed');
} else {
await signOut();
Expand Down
17 changes: 17 additions & 0 deletions src/queries/profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ export async function isEmailTaken(newEmail: string) {
const emailIsTaken = (count ?? 0) >= 1;
return emailIsTaken as boolean;
}

export async function isPasswordSameAsBefore(
new_plain_password: string,
user_id: string | undefined,
): Promise<boolean> {
let { data, error } = await supabase.rpc('check_same_as_old_pass', {
new_plain_password,
user_id,
});

if (error) {
console.error(error);
return false;
} else {
return data;
}
}

0 comments on commit f9f6ed6

Please sign in to comment.