Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rpc to check if password is the same #80

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading