Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { ScrollView, Text, TextInput, View, Button, StyleSheet } from 'react-native';
  4. import { login } from '../redux/actions/auth';
  5. import {AuthenticationDetails, CognitoUser, CognitoUserAttribute, CognitoUserPool} from '../lib/aws-cognito-identity';
  6. import About from '../pages/About';
  7.  
  8.  
  9. const awsCognitoSettings = {
  10. UserPoolId: 'something',
  11. ClientId: 'something'
  12. };
  13.  
  14. class Login extends Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. page: 'Login',
  19. username: '',
  20. password: ''
  21. };
  22. }
  23.  
  24. get alt() {
  25. if(this.state.page == 'Login') {
  26. return 'SignUp';
  27. } else {
  28. return 'Login';
  29. }
  30. }
  31.  
  32. handleClick (e) {
  33. e.preventDefault();
  34. const userPool = new CognitoUserPool(awsCognitoSettings);
  35.  
  36. // Sign up
  37. if (this.state.page === 'SignUp') {
  38. const attributeList = [
  39. new CognitoUserAttribute({ Name: 'email', Value: this.state.username })
  40. ];
  41. userPool.signUp(
  42. this.state.username,
  43. this.state.password,
  44. attributeList,
  45. null,
  46. (err, result) => {
  47. if (err) {
  48. alert(err);
  49. this.setState({ username: '', password: '' });
  50. return;
  51. }
  52. console.log(`result = ${JSON.stringify(result)}`);
  53. this.props.onLogin(this.state.username, this.state.password);
  54. }
  55. );
  56. } else {
  57. const authDetails = new AuthenticationDetails({
  58. Username: this.state.username,
  59. Password: this.state.password
  60. });
  61. const cognitoUser = new CognitoUser({
  62. Username: this.state.username,
  63. Pool: userPool
  64. });
  65. cognitoUser.authenticateUser(authDetails, {
  66. onSuccess: (result) => {
  67. console.log(`access token = ${result.getAccessToken().getJwtToken()}`);
  68. this.props.onLogin(this.state.username, this.state.password);
  69. },
  70. onFailure: (err) => {
  71. alert(err);
  72. this.setState({ username: '', password: '' });
  73. return;
  74. }
  75. });
  76. }
  77. }
  78.  
  79. togglePage (e) {
  80. this.setState({ page: this.alt });
  81. e.preventDefault();
  82. }
  83.  
  84. about = () => {
  85. this.props.navigate.navigation('About');
  86. }
  87.  
  88. render() {
  89. return(
  90. <ScrollView style={{padding: 20}}>
  91.  
  92. <Text onPress={() => this.about()}>About</Text>
  93.  
  94. <Text style={{fontSize: 27}}>{this.state.page}</Text>
  95. <TextInput
  96. placeholder='Email Address'
  97. autoCapitalize='none'
  98. autoCorrect={false}
  99. autoFocus={true}
  100. keyboardType='email-address'
  101. value={this.state.username}
  102. onChangeText={(text) => this.setState({ username: text })} />
  103. <TextInput
  104. placeholder='Password'
  105. autoCapitalize='none'
  106. autoCorrect={false}
  107. secureTextEntry={true}
  108. value={this.state.password}
  109. onChangeText={(text) => this.setState({ password: text })} />
  110. <View style={{margin: 7}}/>
  111. <Button onPress={(e) => this.handleClick(e)} title={this.state.page}/>
  112. <View style={styles.firstView}>
  113. <Text onPress={(e) => this.togglePage(e)} style={styles.buttons}>
  114. {this.alt}
  115. </Text>
  116. </View>
  117. </ScrollView>
  118. );
  119. }
  120. }
  121.  
  122. const styles = StyleSheet.create({
  123. buttons: {
  124. fontSize: 12,
  125. color: 'blue',
  126. flex: 1
  127. },
  128.  
  129. firstView: {
  130. margin: 7,
  131. flexDirection: 'row',
  132. justifyContent: 'center'
  133. }
  134. });
  135.  
  136. const mapStateToProps = (state, ownProps) => {
  137. return {
  138. isLoggedIn: state.auth.isLoggedIn
  139. };
  140. }
  141.  
  142. const mapDispatchToProps = (dispatch) => {
  143. return {
  144. onLogin: (username, password) => { dispatch(login(username, password)); }
  145. }
  146. }
  147.  
  148. export default connect(mapStateToProps, mapDispatchToProps)(Login);
  149.  
  150. import React from 'react';
  151. import {AboutStack} from '../navigation/router';
  152.  
  153. const App = () => {
  154. return (<AboutStack/>);
  155. }
  156.  
  157. export default App;
  158.  
  159. import {StackNavigator} from 'react-navigation';
  160. import Login from '../pages/Login';
  161. import About from '../pages/About';
  162.  
  163. export const AboutStack = StackNavigator({
  164. Login: {
  165. screen: Login,
  166. navigationOptions: {
  167. title: 'Login',
  168. },
  169. },
  170.  
  171. About: {
  172. screen: About,
  173. navigationOptions: {
  174. title: 'About',
  175. },
  176. },
  177. });
  178.  
  179. this.props.navigation.navigate('About');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement