Guest User

Untitled

a guest
Feb 12th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. export function login(email, password) { // Fake authentication function
  2. return async dispatch => {
  3. dispatch(loginRequest()); // dispatch a login request to update the state
  4. try {
  5. if (email.trim() === "test123@nomail.com" && password === "123456") { //If the email and password matches
  6. const session = { token: "abc1234", email: email, username: "test123" } // Create a fake token for authentication
  7. await AsyncStorage.setItem(DATA_SESSION, JSON.stringify(session)) // Stringinfy the session data and store it
  8. setTimeout(() => { // Add a delay for faking a asynchronous request
  9. dispatch(loginSuccess(session)) // Dispatch a successful sign in after 1.5 seconds
  10. return Promise.resolve()
  11. }, 1500)
  12. } else { // Otherwise display an error to the user
  13. setTimeout(() => { // Dispatch an error state
  14. dispatch(loginFailed("Incorrect email or password"))
  15. }, 1500)
  16. }
  17. } catch (err) { // When something goes wrong
  18. console.log(err)
  19. dispatch(loginFailed("Something went wrong"));
  20. return Promise.reject()
  21. }
  22. };
  23. } // login
  24.  
  25. import { bindActionCreators } from "redux";
  26. import * as authActions from "../actions/authenticate";
  27. import { connect } from "react-redux";
  28.  
  29. export default connect(
  30. state => ({ state: state.authenticate }),
  31. dispatch => ({
  32. actions: bindActionCreators(authActions, dispatch)
  33. })
  34. )(Login);
  35.  
  36. onPress={() => {
  37. this.props.actions.login(this.state.email, this.state.password)
  38. }}
  39.  
  40. onPress={() => {
  41. this.props.actions.login(this.state.email, this.state.password)
  42. .then(() => this.props.navigation.navigate('AuthScreen'))
  43. }}
Add Comment
Please, Sign In to add comment