Skip to content

Commit

Permalink
fix: Update OTP verification logic to handle password reset flow
Browse files Browse the repository at this point in the history
  • Loading branch information
KamogeloMoeketse committed Aug 25, 2024
1 parent 52754fd commit 5c0b9bd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
4 changes: 2 additions & 2 deletions frontend/occupi-mobile4/screens/Login/CreatePassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function CreatePassword() {
if (data.password === data.confirmpassword) {
const response = await userResetPassword(data.password, data.confirmpassword);
toast.show({
placement: 'bottom right',
placement: 'top',
render: ({ id }) => {
return (
<Toast nativeID={id} variant="accent" action="success">
Expand All @@ -102,7 +102,7 @@ export default function CreatePassword() {
});
} else {
toast.show({
placement: 'bottom right',
placement: 'top',
render: ({ id }) => {
return (
<Toast nativeID={id} variant="accent" action="error">
Expand Down
15 changes: 14 additions & 1 deletion frontend/occupi-mobile4/screens/Login/OtpVerification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Logo from '../../screens/Login/assets/images/Occupi/Occupi-gradient.png';
import StyledExpoRouterLink from '@/components/StyledExpoRouterLink';
import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen';
import { LinearGradient } from 'expo-linear-gradient';
import { VerifyUserOtpLogin, verifyUserOtpRegister } from '@/utils/auth';
import { verifyUserOtp, VerifyUserOtpLogin, verifyUserOtpRegister } from '@/utils/auth';

const OTPSchema = z.object({
OTP: z.string().min(6, 'OTP must be at least 6 characters in length'),
Expand Down Expand Up @@ -80,6 +80,19 @@ const OTPVerification = () => {
}
});
}
else if (state === 'reset_password') {
const response = await verifyUserOtp(email, pin);
toast.show({
placement: 'top',
render: ({ id }) => {
return (
<Toast nativeID={String(id)} variant="accent" action={response === 'OTP verified successfully!' ? 'success' : 'error'}>
<ToastTitle>{response}</ToastTitle>
</Toast>
);
}
});
}
else {
const response = await VerifyUserOtpLogin(email, pin);
toast.show({
Expand Down
22 changes: 22 additions & 0 deletions frontend/occupi-mobile4/services/authservices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ export async function verifyOtplogin(req: VerifyOTPReq): Promise<LoginSuccess |
}
}

export async function verifyOtp(req: VerifyOTPReq): Promise<LoginSuccess | Unsuccessful> {
console.log(req);
try {
const response = await axios.post("https://dev.occupi.tech/auth/verify-otp", req, {

Check warning on line 101 in frontend/occupi-mobile4/services/authservices.ts

View check run for this annotation

Codecov / codecov/patch

frontend/occupi-mobile4/services/authservices.ts#L99-L101

Added lines #L99 - L101 were not covered by tests
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
withCredentials: true
});
// console.log(response.data);
return response.data as LoginSuccess;

Check warning on line 109 in frontend/occupi-mobile4/services/authservices.ts

View check run for this annotation

Codecov / codecov/patch

frontend/occupi-mobile4/services/authservices.ts#L109

Added line #L109 was not covered by tests
} catch (error) {
if (axios.isAxiosError(error) && error.response) {
// console.log(error.response.data);
return error.response.data as Unsuccessful;

Check warning on line 113 in frontend/occupi-mobile4/services/authservices.ts

View check run for this annotation

Codecov / codecov/patch

frontend/occupi-mobile4/services/authservices.ts#L113

Added line #L113 was not covered by tests
} else {
throw error;

Check warning on line 115 in frontend/occupi-mobile4/services/authservices.ts

View check run for this annotation

Codecov / codecov/patch

frontend/occupi-mobile4/services/authservices.ts#L115

Added line #L115 was not covered by tests
}
}
}

export async function forgotPassword(req: any): Promise<Success | Unsuccessful> {
try {
const response = await axios.post("https://dev.occupi.tech/auth/forgot-password", req, {

Check warning on line 122 in frontend/occupi-mobile4/services/authservices.ts

View check run for this annotation

Codecov / codecov/patch

frontend/occupi-mobile4/services/authservices.ts#L121-L122

Added lines #L121 - L122 were not covered by tests
Expand Down
56 changes: 34 additions & 22 deletions frontend/occupi-mobile4/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//this folder contains functions that will call the service functions which make api requests for authentication
//the purpose of this file is to refine and process the data and return these to the View

import { forgotPassword, login, logout, register, resetPassword, verifyOtplogin, verifyOtpRegister } from "../services/authservices";
import { forgotPassword, login, logout, register, resetPassword, verifyOtp, verifyOtplogin, verifyOtpRegister } from "../services/authservices";
import { fetchNotificationSettings, fetchSecuritySettings, fetchUserDetails } from "./user";
import { router } from 'expo-router';
import * as SecureStore from 'expo-secure-store';
Expand All @@ -26,7 +26,7 @@ export async function UserLogin(email: string, password: string) {
fetchNotificationSettings(email);
fetchSecuritySettings(email);
router.replace('/home');
}
}
else {
setState('verify_otp_login');
router.replace('/verify-otp')
Expand Down Expand Up @@ -90,30 +90,42 @@ export async function verifyUserOtpRegister(email: string, otp: string) {
}
}

export async function VerifyUserOtpLogin(email : string, otp : string) {
export async function VerifyUserOtpLogin(email: string, otp: string) {
try {
const response = await verifyOtplogin({
email: email,
otp: otp
});
if (response.status === 200) {
// console.log('responseee',response);
const state = await SecureStore.getItemAsync('AppState');
console.log('staaate',state)
if (state === 'reset_password') {
storeOtp(otp);
router.replace('/create-password');
}
else {
setState('logged_in');
storeToken(response.data.token);
console.log('here');
fetchUserDetails(email, response.data.token);
fetchNotificationSettings(email);
fetchSecuritySettings(email);
router.replace('/home');
}
// console.log('responseee',response)
setState('logged_in');
storeToken(response.data.token);
console.log('here');
fetchUserDetails(email, response.data.token);
fetchNotificationSettings(email);
fetchSecuritySettings(email);
router.replace('/home');
return response.message;
}
else {
console.log('woahhh', response)
return response.message;
}
} catch (error) {
console.error('Error:', error);
}
}

export async function verifyUserOtp(email: string, otp: string) {
try {
const response = await verifyOtp({
email: email,
otp: otp
});
console.log(response);
if (response.status === 200) {
storeOtp(otp);
router.replace('/create-password');
return response.message;
}
else {
Expand All @@ -123,7 +135,7 @@ export async function VerifyUserOtpLogin(email : string, otp : string) {
} catch (error) {
console.error('Error:', error);
}
}
}

export async function userForgotPassword(email: string) {
storeUserEmail(email);
Expand All @@ -133,7 +145,7 @@ export async function userForgotPassword(email: string) {
try {
const response = await forgotPassword(body);
if (response.status === 200) {
console.log('responseee',response);
console.log('responseee', response);
setState('reset_password');
router.replace('/verify-otp');
return response.message as string;
Expand All @@ -159,7 +171,7 @@ export async function userResetPassword(newPassword: string, newPasswordConfirm:
try {
const response = await resetPassword(body);
if (response.status === 200) {
console.log('responseee',response);
console.log('responseee', response);
setState('logged_in');
storeToken(response.data.token);
fetchUserDetails(email, response.data.token);
Expand Down

0 comments on commit 5c0b9bd

Please sign in to comment.