summaryrefslogtreecommitdiffstats
path: root/src/screens/Auth/LoginScreen.js
blob: 6b11a5bd422e2a742b2b3a5ade4cd621f0feeecf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import React, { useState } from 'react';
import { View, TextInput, Button, Text } from 'react-native';
import { login, verifyOtp } from '../../services/authService';

const LoginScreen = ({ navigation }) => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [otp, setOtp] = useState('');
  const [isOtpSent, setIsOtpSent] = useState(false);

  const handleLogin = async () => {
    try {
      const response = await login(email, password);
      if (response.data.otpSent) setIsOtpSent(true);
    } catch (error) {
      console.error('Login error:', error);
    }
  };

  const verifyOtp = async () => {
    try {
      const response = await verifyOtp(email, otp);
      if (response.data.success) navigation.navigate('MallSelection');
    } catch (error) {
      console.error('OTP verification error:', error);
    }
  };

  return (
    <View>
      <TextInput placeholder="Email" value={email} onChangeText={setEmail} />
      <TextInput placeholder="Password" value={password} onChangeText={setPassword} secureTextEntry />
      <Button title="Login" onPress={handleLogin} />

      {isOtpSent && (
        <View>
          <TextInput placeholder="Enter OTP" value={otp} onChangeText={setOtp} />
          <Button title="Verify OTP" onPress={verifyOtp} />
        </View>
      )}
    </View>
  );
};

export default LoginScreen;