From db575615046544e83759a3615f37540305aa9742 Mon Sep 17 00:00:00 2001 From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com> Date: Tue, 8 Dec 2020 20:19:32 -0800 Subject: [TMA-308] Forgot password logic [Frontend] (#131) * Done with changes * Submit on enter * Fixed StrongPassword issue * Clean and modular Verification.tsx * small fix --- src/services/UserProfileService.ts | 191 +++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) (limited to 'src/services') diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index c8dbcdd1..18bcfb5a 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -10,6 +10,10 @@ import { GET_IG_POSTS_ENDPOINT, GET_TWITTER_POSTS_ENDPOINT, PROFILE_INFO_ENDPOINT, + PASSWORD_RESET_ENDPOINT, + TAGG_CUSTOMER_SUPPORT, + VERIFY_OTP_ENDPOINT, + SEND_OTP_ENDPOINT, } from '../constants'; export const loadProfileInfo = async (token: string, userId: string) => { @@ -116,3 +120,190 @@ export const loadRecentlySearchedUsers = async () => { console.log(e); } }; + +export const handlePasswordResetRequest = async (value: string) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(PASSWORD_RESET_ENDPOINT + 'request/', { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + value, + }), + }); + const status = response.status; + if (status === 200) { + Alert.alert( + 'A code was sent to your registered phone number, please use the same to reset your password', + ); + return true; + } else { + if (status == 404) { + Alert.alert( + `Please make sure that the email / username entered is registered with us. You may contact our customer support at ${TAGG_CUSTOMER_SUPPORT}`, + ); + } else { + Alert.alert( + 'Something went wrong! 😭', + "Would you believe me if I told you that I don't know what happened?", + ); + } + + console.log(response); + return false; + } + } catch (error) { + console.log(error); + Alert.alert('Something went wrong! 😭', 'Looks like our servers are down'); + return false; + } +}; + +export const handlePasswordCodeVerification = async ( + value: string, + otp: string, +) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(PASSWORD_RESET_ENDPOINT + 'verify/', { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + value, + otp, + }), + }); + const status = response.status; + if (status === 200) { + return true; + } else { + if (status == 404) { + Alert.alert( + `Please make sure that the email / username entered is registered with us. You may contact our customer support at ${TAGG_CUSTOMER_SUPPORT}`, + ); + } else if (status === 401) { + Alert.alert('Looks like you have entered the wrong code'); + } else { + Alert.alert( + 'Something went wrong! 😭', + "Would you believe me if I told you that I don't know what happened?", + ); + } + + console.log(response); + return false; + } + } catch (error) { + console.log(error); + Alert.alert('Something went wrong! 😭', 'Looks like our servers are down'); + return false; + } +}; + +export const handlePasswordReset = async (value: string, password: string) => { + try { + const token = await AsyncStorage.getItem('token'); + const response = await fetch(PASSWORD_RESET_ENDPOINT + `reset/`, { + method: 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + value, + password, + }), + }); + const status = response.status; + if (status === 200) { + Alert.alert('Your password was reset successfully'); + return true; + } else { + if (status == 404) { + Alert.alert( + `Please make sure that the email / username entered is registered with us. You may contact our customer support at ${TAGG_CUSTOMER_SUPPORT}`, + ); + } else if (status == 406) { + Alert.alert('You may not use an already used password'); + } else { + Alert.alert( + 'Something went wrong! 😭', + "Would you believe me if I told you that I don't know what happened?", + ); + } + console.log(response); + return false; + } + } catch (error) { + console.log(error); + Alert.alert('Something went wrong! 😭', 'Looks like our servers are down'); + return false; + } +}; + +export const verifyOtp = async (phone: string, otp: string) => { + try { + let response = await fetch(VERIFY_OTP_ENDPOINT, { + method: 'POST', + body: JSON.stringify({ + phone_number: '+1' + phone, + otp, + }), + }); + let statusCode = response.status; + if (statusCode === 200) { + return true; + } else { + if (statusCode === 401) { + Alert.alert( + 'Invalid verification code 🤔', + 'Try again. Tap the resend code button if you need a new code.', + ); + } else { + Alert.alert( + 'Something went wrong! 😭', + "Would you believe me if I told you that I don't know what happened?", + ); + } + } + } catch (error) { + Alert.alert( + 'Verifiation failed 😓', + 'Please double-check your network connection and retry.', + ); + return { + name: 'Verification error', + description: error, + }; + } +}; + +export const sendOtp = async (phone: string) => { + try { + console.log(phone); + let response = await fetch(SEND_OTP_ENDPOINT, { + method: 'POST', + body: JSON.stringify({ + phone_number: '+1' + phone, + }), + }); + + let status = response.status; + if (status === 200) { + Alert.alert( + 'New verification code sent!', + 'Check your phone messages for your code.', + ); + return true; + } else { + Alert.alert('Something went wrong!'); + return false; + } + } catch (error) { + console.log(error); + return false; + } +}; -- cgit v1.2.3-70-g09d2