Skip to content
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
44 changes: 44 additions & 0 deletions src/api/auth/use-update-password.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createMutation } from 'react-query-kit';

import { client } from '../common';

type Variables = {
password: string;
passwordConfirmation: string;
};

type ResponseData = {
email: string;
provider: string;
uid: string;
id: number;
allow_password_change: boolean;
name: string;
nickname: string;
image: string | null;
created_at: string;
updated_at: string;
birthday: string | null;
};

type Response = {
success: boolean;
message: string;
data?: ResponseData;
};

const updatePasswordRequest = async (variables: Variables) => {
const { data } = await client({
url: '/v1/users/password',
method: 'PUT',
data: { ...variables },
headers: {
'Content-Type': 'application/json',
},
});
return data;
};

export const useUpdatePassword = createMutation<Response, Variables>({
mutationFn: (variables) => updatePasswordRequest(variables),
});
8 changes: 8 additions & 0 deletions src/app/(app)/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export default function Settings() {
text={'settings.account.email'}
value={userData?.email ?? ''}
/>
<Link
asChild
href={{
pathname: '/update-password',
}}
>
<Item text="settings.account.password" />
</Link>
</ItemsContainer>
<ItemsContainer title="settings.generale">
<LanguageItem />
Expand Down
8 changes: 8 additions & 0 deletions src/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BottomSheetModalProvider } from '@gorhom/bottom-sheet';
import { ThemeProvider } from '@react-navigation/native';
import { SplashScreen, Stack } from 'expo-router';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { StyleSheet } from 'react-native';
import FlashMessage from 'react-native-flash-message';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
Expand All @@ -29,12 +30,19 @@ interceptors();
SplashScreen.preventAutoHideAsync();

export default function RootLayout() {
const { t } = useTranslation();
return (
<Providers>
<Stack>
<Stack.Screen name="(app)" options={{ headerShown: false }} />
<Stack.Screen name="onboarding" options={{ headerShown: false }} />
<Stack.Screen name="forgot-password" />
<Stack.Screen
name="update-password"
options={{
title: t('updatePassword.title'),
}}
/>
<Stack.Screen name="sign-in" options={{ headerShown: false }} />
<Stack.Screen
name="sign-up"
Expand Down
101 changes: 101 additions & 0 deletions src/app/update-password.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { zodResolver } from '@hookform/resolvers/zod';
import { useNavigation } from 'expo-router';
import React from 'react';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { showMessage } from 'react-native-flash-message';
import { KeyboardAvoidingView } from 'react-native-keyboard-controller';
import { z } from 'zod';

import { useUpdatePassword } from '@/api/auth/use-update-password';
import { translate } from '@/core';
import { Button, ControlledInput, FocusAwareStatusBar, Text, View } from '@/ui';

type FormValues = { password: string; passwordConfirmation: string };
const MIN_CHARS = 6;

const schema = z
.object({
password: z.string().min(
MIN_CHARS,
translate('updatePassword.error.shortPassword', {
minChars: MIN_CHARS,
}),
),
passwordConfirmation: z.string(),
})
.refine((data) => data.password === data.passwordConfirmation, {
message: translate('updatePassword.error.passwordsMustMatch'),
path: ['passwordConfirmation'],
});

export default function UpdatePassword() {
const { t } = useTranslation();
const navigate = useNavigation();

const { mutateAsync: updatePassword } = useUpdatePassword({
onSuccess: () => {
showMessage({
message: t('updatePassword.successMessage'),
type: 'success',
});
navigate.goBack();
},
onError: () => {
showMessage({
message: t('updatePassword.errorMessage'),
type: 'danger',
});
},
});

const { handleSubmit, control } = useForm<FormValues>({
resolver: zodResolver(schema),
});
const onSubmit = async (data: FormValues) => {
await updatePassword(data);
};

return (
<>
<FocusAwareStatusBar />
<KeyboardAvoidingView>
<View className="gap-8 p-4">
<View className="gap-2">
<Text className="text-center text-2xl">
{t('updatePassword.title')}
</Text>
<Text className="text-center text-gray-600">
{t('updatePassword.description')}
</Text>
</View>
<View className="gap-2">
<ControlledInput
testID="password-input"
autoCapitalize="none"
secureTextEntry
control={control}
name="password"
label={t('updatePassword.passwordLabel')}
placeholder={t('updatePassword.passwordPlaceholder')}
/>
<ControlledInput
testID="confirm-password-input"
autoCapitalize="none"
secureTextEntry
control={control}
name="passwordConfirmation"
label={t('updatePassword.confirmPasswordLabel')}
placeholder={t('updatePassword.confirmPasswordPlaceholder')}
/>
<Button
testID="update-password-button"
label={t('updatePassword.buttonLabel')}
onPress={handleSubmit(onSubmit)}
/>
</View>
</View>
</KeyboardAvoidingView>
</>
);
}
16 changes: 16 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"account": {
"email": "Email",
"name": "Name",
"password": "Password",
"title": "Account"
},
"app_name": "App Name",
Expand Down Expand Up @@ -102,6 +103,21 @@
"version": "Version",
"website": "Website"
},
"updatePassword": {
"buttonLabel": "Update Password",
"confirmPasswordLabel": "Confirm Password",
"confirmPasswordPlaceholder": "Re-enter new password",
"description": "Enter a new password for your account.",
"error": {
"passwordsMustMatch": "Passwords must match",
"shortPassword": "Password must be at least {{minChars}} characters"
},
"errorMessage": "An error occurred while updating your password. Please try again.",
"passwordLabel": "New Password",
"passwordPlaceholder": "Enter new password",
"successMessage": "Your password has been successfully updated.",
"title": "Update Password"
},
"welcome": "Welcome to rootstrap app site",
"www": {
"invalidUrl": "Invalid Url"
Expand Down