Skip to content

Commit

Permalink
Use rpc to check if password is the same
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Apr 9, 2024
1 parent 507523a commit e11e776
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions 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 @@ -34,8 +35,9 @@ function ResetPasswordScreen() {
}
}, [hasUppercase, hasLowercase, hasLength, hasNumber, isDifferent]);

const checkPassword = (text: string) => {
const checkPassword = async (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 +72,7 @@ function ResetPasswordScreen() {
const { error } = await updateUser({ password });

if (error) {
console.error(error)
Alert.alert('Updating password failed');
} else {
await signOut();
Expand Down
15 changes: 15 additions & 0 deletions src/queries/profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ 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 e11e776

Please sign in to comment.