import React, { Component } from "react"; import { LOGIN, LOGIN_FAILED, LOGOUT } from '../types' const defaultState = { isLoggedIn: false, UserName: '', UserEmail: '', UserPassword: '' }; export default function reducer(state = defaultState, action) { switch (action.type) { case LOGIN: return Object.assign({}, state, { isLoggedIn: true, UserName: action.UserName, UserEmail: action.UserEmail, UserPassword: action.UserPassword }); case LOGOUT: return Object.assign({}, state, { isLoggedIn: false, UserName: '', UserEmail: '', UserPassword: '' }); case LOGIN_FAILED: return { UserName: '', UserEmail: '', UserPassword: '', isLoggedIn: false } default: return state; } import { LOGIN, LOGIN_FAILED, LOGOUT } from '../types' export const login = (UserName, UserEmail, UserPassword) => async (dispatch) => { function onSuccess(success) { dispatch({ type: LOGIN, payload: success }) return success } function onError(error) { dispatch({ type: LOGIN_FAILED, error }) return error } try { const { UserEmail } = this.state ; const { UserName } = this.state ; const { UserPassword } = this.state ; const res = await fetch('https://lifestormweb.000webhostapp.com/User_Login.php', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ email: UserEmail, password: UserPassword, name: UserName }) }).then((response) => response.json()) .then((responseJson) => { // If server response message same as Data Matched if(responseJson === 'Data Matched') { //Then open Profile activity and send user email to profile activity. this.props.navigation.navigate("Profil"); } else{ Alert.alert(responseJson); } }) const success = await res.json() return onSuccess(success) } catch (error) { return onError(error) } }; export const logout = () => { return { type: 'LOGOUT' }; }; export class Login extends Component { state = { UserName: '', UserEmail: '', UserPassword: '' } userLogin (e) { this.props.onLogin(this.state.UserName, this.state.UserEmail, this.state.UserPassword); e.preventDefault(); } render() { return ( Willkommen zu LifeStorm! this.setState({ UserName: text })} /> this.setState({ UserEmail: text })} /> this.setState({ UserPassword: text })} /> this.userLogin(e)} > Sich einloggen Haben Sie kein Konto? this.props.navigation.navigate("Register")}> Sich anmelden ); } } const mapStateToProps = (state, ownProps) => { return { isLoggedIn: state.auth.isLoggedIn, }; } const mapDispatchToProps = (dispatch) => { return { onLogin: (UserName, UserEmail, UserPassword) => { dispatch(login(UserName, UserEmail, UserPassword)); } } } export default connect(mapStateToProps, mapDispatchToProps)(Login); Actions must be plain objects. Use custom middleware for async actions