|
1 | | -import { View, Text, TextInput, Button } from 'react-native'; |
2 | | -import { useState } from 'react'; |
3 | | -import { useRouter } from 'expo-router'; |
4 | | -import { verifyOtp } from '@/services/auth/otp.service'; |
5 | | -import { useLocalSearchParams, useSearchParams } from 'expo-router/build/hooks'; |
| 1 | +import { |
| 2 | + View, |
| 3 | + Text, |
| 4 | + TextInput, |
| 5 | + StyleSheet, |
| 6 | + Pressable, |
| 7 | +} from "react-native"; |
| 8 | +import { SafeAreaView } from "react-native-safe-area-context"; |
| 9 | +import { useState } from "react"; |
| 10 | +import { useRouter } from "expo-router"; |
| 11 | +import { verifyOtp } from "@/services/auth/otp.service"; |
| 12 | +import { useLocalSearchParams } from "expo-router/build/hooks"; |
6 | 13 |
|
7 | 14 | export default function OtpScreen() { |
8 | | - const [otp, setOtp] = useState(''); |
| 15 | + const [otp, setOtp] = useState(""); |
| 16 | + const [loading, setLoading] = useState(false); |
9 | 17 | const router = useRouter(); |
10 | | - const { phoneNumber } = useLocalSearchParams(); |
| 18 | + const { phone } = useLocalSearchParams(); |
11 | 19 |
|
12 | 20 | const handleVerify = async () => { |
13 | | - const res = await verifyOtp(phoneNumber.toString(), otp); |
14 | | - console.log(res); |
| 21 | + if (!otp || otp.length < 4) return; |
| 22 | + |
| 23 | + setLoading(true); |
| 24 | + const res = await verifyOtp(phone.toString(), otp); |
| 25 | + setLoading(false); |
| 26 | + |
15 | 27 | if (!res?.success) return; |
16 | | - if (res.success) { |
17 | | - if (res.userExists) { |
18 | | - router.replace('/(tabs)/home'); |
19 | | - } else { |
20 | | - router.replace({ |
21 | | - pathname: '/(auth)/register', |
22 | | - params: { |
23 | | - otpId: res.otpId, |
24 | | - phoneNumber: phoneNumber.toString(), |
25 | | - }, |
26 | | - }); |
27 | | - } |
| 28 | + |
| 29 | + if (res.userExists) { |
| 30 | + router.replace("/(tabs)/home"); |
| 31 | + } else { |
| 32 | + router.replace({ |
| 33 | + pathname: "/(auth)/register", |
| 34 | + params: { |
| 35 | + otpId: res.otpId, |
| 36 | + phoneNumber: phone.toString(), |
| 37 | + }, |
| 38 | + }); |
28 | 39 | } |
29 | 40 | }; |
30 | 41 |
|
31 | 42 | return ( |
32 | | - <View> |
33 | | - <Text>Enter OTP</Text> |
34 | | - |
35 | | - <TextInput |
36 | | - placeholder="OTP" |
37 | | - keyboardType="number-pad" |
38 | | - value={otp} |
39 | | - onChangeText={setOtp} |
40 | | - /> |
41 | | - |
42 | | - <Button title="Verify OTP" onPress={handleVerify} /> |
43 | | - </View> |
| 43 | + <SafeAreaView style={styles.safeArea}> |
| 44 | + <View style={styles.container}> |
| 45 | + <View style={styles.content}> |
| 46 | + <Text style={styles.title}>Enter OTP</Text> |
| 47 | + |
| 48 | + <Text style={styles.description}> |
| 49 | + We’ve sent a one-time password to{" "} |
| 50 | + <Text style={styles.phone}> |
| 51 | + {phone} |
| 52 | + </Text> |
| 53 | + </Text> |
| 54 | + |
| 55 | + <TextInput |
| 56 | + style={styles.otpInput} |
| 57 | + placeholder="••••" |
| 58 | + keyboardType="number-pad" |
| 59 | + value={otp} |
| 60 | + onChangeText={setOtp} |
| 61 | + maxLength={6} |
| 62 | + textAlign="center" |
| 63 | + /> |
| 64 | + |
| 65 | + <Pressable |
| 66 | + style={[ |
| 67 | + styles.button, |
| 68 | + (otp.length < 4 || loading) && |
| 69 | + styles.buttonDisabled, |
| 70 | + ]} |
| 71 | + onPress={handleVerify} |
| 72 | + disabled={otp.length < 4 || loading} |
| 73 | + > |
| 74 | + <Text style={styles.buttonText}> |
| 75 | + {loading ? "Verifying..." : "Verify OTP"} |
| 76 | + </Text> |
| 77 | + </Pressable> |
| 78 | + |
| 79 | + <Text style={styles.resend}> |
| 80 | + Didn’t receive OTP?{" "} |
| 81 | + <Text style={styles.resendLink}>Resend</Text> |
| 82 | + </Text> |
| 83 | + </View> |
| 84 | + </View> |
| 85 | + </SafeAreaView> |
44 | 86 | ); |
45 | 87 | } |
| 88 | +const styles = StyleSheet.create({ |
| 89 | + safeArea: { |
| 90 | + flex: 1, |
| 91 | + backgroundColor: "#EAF5EC", |
| 92 | + }, |
| 93 | + |
| 94 | + container: { |
| 95 | + flex: 1, |
| 96 | + paddingHorizontal: 24, |
| 97 | + }, |
| 98 | + |
| 99 | + content: { |
| 100 | + flex: 1, |
| 101 | + justifyContent: "center", |
| 102 | + }, |
| 103 | + |
| 104 | + title: { |
| 105 | + fontSize: 24, |
| 106 | + fontWeight: "700", |
| 107 | + color: "#111", |
| 108 | + marginBottom: 8, |
| 109 | + }, |
| 110 | + |
| 111 | + description: { |
| 112 | + fontSize: 14, |
| 113 | + color: "#555", |
| 114 | + lineHeight: 20, |
| 115 | + marginBottom: 32, |
| 116 | + }, |
| 117 | + |
| 118 | + phone: { |
| 119 | + fontWeight: "600", |
| 120 | + color: "#111", |
| 121 | + }, |
| 122 | + |
| 123 | + otpInput: { |
| 124 | + backgroundColor: "#FFF", |
| 125 | + borderRadius: 16, |
| 126 | + height: 56, |
| 127 | + fontSize: 20, |
| 128 | + letterSpacing: 12, |
| 129 | + marginBottom: 24, |
| 130 | + }, |
| 131 | + |
| 132 | + button: { |
| 133 | + backgroundColor: "#000", |
| 134 | + paddingVertical: 16, |
| 135 | + borderRadius: 16, |
| 136 | + alignItems: "center", |
| 137 | + marginBottom: 16, |
| 138 | + }, |
| 139 | + |
| 140 | + buttonDisabled: { |
| 141 | + opacity: 0.5, |
| 142 | + }, |
| 143 | + |
| 144 | + buttonText: { |
| 145 | + color: "#FFF", |
| 146 | + fontSize: 16, |
| 147 | + fontWeight: "600", |
| 148 | + }, |
| 149 | + |
| 150 | + resend: { |
| 151 | + textAlign: "center", |
| 152 | + fontSize: 13, |
| 153 | + color: "#555", |
| 154 | + }, |
| 155 | + |
| 156 | + resendLink: { |
| 157 | + fontWeight: "600", |
| 158 | + color: "#000", |
| 159 | + }, |
| 160 | +}); |
0 commit comments