Skip to content

Instantly share code, notes, and snippets.

@yvsssantosh
Created September 16, 2019 16:17
Show Gist options
  • Save yvsssantosh/3424a7ef218b431ea434aac0dff54839 to your computer and use it in GitHub Desktop.
Save yvsssantosh/3424a7ef218b431ea434aac0dff54839 to your computer and use it in GitHub Desktop.
import React, { PureComponent } from 'react'
import { View, Text, Button, AsyncStorage } from 'react-native'
import { connect } from 'react-redux'
class LoginScreen extends PureComponent {
constructor (props) {
super(props)
this.attemptLogin = this.attemptLogin.bind(this)
}
attemptLogin () {
// Send request to server and validate login here
var token = 'somerandomtoken'
return token
}
render () {
return (
<View style={{ flex: 1, backgroundColor: '#ffffff', alignItems: 'center', justifyContent: 'center' }}>
<Text children='Redux Login Example' />
<Text children='Click on login to continue' />
<Button color='#90100' title='Login' onPress={this.attemptLogin} />
</View>
)
}
}
const mapStateToProps = (state, ownProps) => {
return {}
}
export const actionCreator = (action, payload = null) => ({ action, payload })
const mapDispatchToProps = (dispatch, ownProps) => {
return {
authSuccess: (token) => {
AsyncStorage.multiSet([['token', token], ['authenticated', 1]])
dispatch(actionCreator('LOGIN_SUCCESS'))
}
}
}
export const authStateReducer = (state = { authenticated: false, appStarted: false }, { type, payload }) => {
switch (type) {
case 'LOGIN_SUCCESS':
return { ...state, authenticated: true }
case 'LOGGOUT':
return { ...state, authenticated: false }
case 'APP_LOADED':
return { ...state, appStarted: true }
default:
return state
}
}
export default connect(mapStateToProps, mapDispatchToProps)(LoginScreen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment