Skip to content

Commit

Permalink
auth done
Browse files Browse the repository at this point in the history
  • Loading branch information
isuyashpatel committed Jan 25, 2024
1 parent c1d7aec commit d6e7c02
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/screens/AuthScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
Alert,
Image,
KeyboardAvoidingView,
Platform,
StatusBar,
StyleSheet,
Text,
Expand All @@ -21,7 +23,6 @@ import Ionicons from 'react-native-vector-icons/Ionicons'
import { Dimensions } from 'react-native'
import OTPInputView from '@twotalltotems/react-native-otp-input'
import emailSchema from '../validation/zod'
import axios from 'axios'
import AuthService from '../services'

const windowHeight = Dimensions.get('window').height
Expand All @@ -33,38 +34,50 @@ const AuthScreen = () => {
const [mail, setMail] = useState('')
const [validationMail, setValidationMail] = useState(false)
const [code, setCode] = useState('')
const [isLoading, setIsLoading] = useState(false)
const handleMailChange = (text: string) => {
setMail(text)
}

const handleValidateEmail = async () => {
setIsLoading(true)
try {
emailSchema.parse(mail);

const response = await AuthService.sendLoginOTP(mail);

if (response.status === 200) {
if (response.status === 9999) {
setIsLoading(false)
setValidationMail(true);
} else {
setIsLoading(false)
Alert.alert('Something Unwanted happened');
}
} catch (error:any) {
setIsLoading(false)
Alert.alert('Error', error.message || 'Something went wrong');
}
};
const VerifyOtp = async () => {
setIsLoading(true)
try {
const response = await AuthService.verifyLoginOTP(mail, code);
if (response.status === 200) {
if (response===true) {
setIsLoading(false)
userAuthentication();
} else {
setIsLoading(false)
Alert.alert('Wrong Otp');
}
} catch (error) {
setIsLoading(false)
Alert.alert('Something Went wrong');
}
};
return (
<View style={styles.ScreenContainer}>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.ScreenContainer}>
<StatusBar backgroundColor={COLORS.primaryBlackHex} />
<View style={styles.AuthContainer}>
<Image
Expand Down Expand Up @@ -105,13 +118,13 @@ const AuthScreen = () => {
{validationMail ? (
<TouchableOpacity onPress={VerifyOtp}>
<View style={styles.AuthButton}>
<Text style={styles.Authenticate}>Verify</Text>
<Text style={styles.Authenticate}>{isLoading?'Processing...':'Verify'}</Text>
</View>
</TouchableOpacity>
) : (
<TouchableOpacity onPress={handleValidateEmail}>
<View style={styles.AuthButton}>
<Text style={styles.Authenticate}>Continue</Text>
<Text style={styles.Authenticate}>{isLoading?'Processing...':'Continue'}</Text>
</View>
</TouchableOpacity>
)}
Expand All @@ -126,7 +139,7 @@ const AuthScreen = () => {
) : null}
</View>
</View>
</View>
</KeyboardAvoidingView>
)
}

Expand Down
27 changes: 27 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import axios from 'axios';

const baseURL = ' http://localhost:5000';

const AuthService = {
sendLoginOTP: async (email:string) => {
try {
const {data} = await axios.get(`${baseURL}/login-otp/${email}`);

return data;
} catch (error) {
throw error;
}
},

verifyLoginOTP: async (email:string, token:string) => {
try {
const {data} = await axios.get(`${baseURL}/verify-login-otp/${email}/${token}`);

return data;
} catch (error) {
throw error;
}
},
};

export default AuthService;
1 change: 1 addition & 0 deletions src/validation/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { z } from 'zod';

const emailSchema = z.string().email();


export default emailSchema

0 comments on commit d6e7c02

Please sign in to comment.