Script react native untuk Login via PHP

import React, { useState } from 'react';
import { View, TextInput, Button, StyleSheet } from 'react-native';

const LoginScreen = () => {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  const handleLogin = async () => {
    try {
      const apiUrl = 'https://code.rezaervani.com/vouchergenerator/password.php';

      const response = await fetch(apiUrl, {
        method: 'POST',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
          'User-Agent': 'ReactNative',
        },
        body: JSON.stringify({
          username,
          password,
        }),
      });

      console.log('Response:'+JSON.stringify(response));

      const data = await response.json();

      console.log('Data:'+JSON.stringify(data));

      if (typeof data === 'object' && data.hasOwnProperty('success')) {
        if (data.success) {
          console.log(JSON.stringify('Login successful!'));
          // Redirect to the dashboard or perform other actions on success
        } else {
          console.log(JSON.stringify('Login failed: ' + data.message));
          // Handle the login failure, show an error message, etc.
        }
      } else {
        console.log('Invalid response format:', data);
        // Handle the case when the response is not in expected JSON format
      }
    } catch (error) {
      console.log('An error occurred:', error);

      // Handle the error
      // You can show an error message or perform any desired action here
    }
  };

  return (
    <View style={styles.container}>
      <View style={styles.form}>
        <TextInput
          placeholder="Username"
          value={username}
          onChangeText={setUsername}
          style={styles.input}
        />
        <TextInput
          placeholder="Password"
          secureTextEntry
          value={password}
          onChangeText={setPassword}
          style={styles.input}
        />
        <Button title="Masuk" onPress={handleLogin} />
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  form: {
    width: '80%',
  },
  input: {
    borderWidth: 1,
    padding: 10,
    marginBottom: 10,
  },
});

export default LoginScreen;
About Reza Ervani 430 Articles
Adalah pendiri programming.rezaervani.com -

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.