Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 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.  
  7. const awsCognitoSettings = {
  8. UserPoolId: 'some #',
  9. ClientId: 'some #'
  10. };
  11.  
  12. class Login extends Component {
  13. constructor (props) {
  14. super(props);
  15. this.state = {
  16. page: 'Login',
  17. username: '',
  18. password: ''
  19. };
  20. }
  21.  
  22. get alt () { return (this.state.page === 'Login') ? 'SignUp' : 'Login'; }
  23.  
  24. handleClick (e) {
  25. e.preventDefault();
  26. const userPool = new CognitoUserPool(awsCognitoSettings);
  27.  
  28. // Sign up
  29. if (this.state.page === 'SignUp') {
  30. const attributeList = [
  31. new CognitoUserAttribute({ Name: 'email', Value: this.state.username })
  32. ];
  33. userPool.signUp(
  34. this.state.username,
  35. this.state.password,
  36. attributeList,
  37. null,
  38. (err, result) => {
  39. if (err) {
  40. alert(err);
  41. this.setState({ username: '', password: '' });
  42. return;
  43. }
  44. console.log(`result = ${JSON.stringify(result)}`);
  45. this.props.onLogin(this.state.username, this.state.password);
  46. }
  47. );
  48. } else {
  49. const authDetails = new AuthenticationDetails({
  50. Username: this.state.username,
  51. Password: this.state.password
  52. });
  53. const cognitoUser = new CognitoUser({
  54. Username: this.state.username,
  55. Pool: userPool
  56. });
  57. cognitoUser.authenticateUser(authDetails, {
  58. onSuccess: (result) => {
  59. console.log(`access token = ${result.getAccessToken().getJwtToken()}`);
  60. this.props.onLogin(this.state.username, this.state.password);
  61. },
  62. onFailure: (err) => {
  63. alert(err);
  64. this.setState({ username: '', password: '' });
  65. return;
  66. }
  67. });
  68. }
  69. }
  70.  
  71. togglePage (e) {
  72. this.setState({ page: this.alt });
  73. e.preventDefault();
  74. }
  75.  
  76. render() {
  77. return (
  78. <ScrollView style={{padding: 20}}>
  79. {/*<Text style={styles.title}>Welcome!</Text>*/}
  80. <TextInput
  81. style={styles.pw}
  82. placeholder=' Email Address'
  83. autoCapitalize='none'
  84. autoCorrect={false}
  85. autoFocus={true}
  86. keyboardType='email-address'
  87. value={this.state.username}
  88. onChangeText={(text) => this.setState({ username: text })} />
  89.  
  90. <TextInput
  91. style={styles.pw}
  92. placeholder=' Password'
  93. autoCapitalize='none'
  94. autoCorrect={false}
  95. secureTextEntry={true}
  96. value={this.state.password}
  97. onChangeText={(text) => this.setState({ password: text })} />
  98.  
  99. <View style={{margin: 7}}/>
  100. <Button onPress={(e) => this.handleClick(e)} title={this.state.page}/>
  101. <View style={{margin: 7, flexDirection: 'row', justifyContent: 'center'}}>
  102.  
  103. <Text onPress={(e) => this.togglePage(e)} style={styles.buttons}>
  104. {this.alt}
  105. </Text>
  106. </View>
  107.  
  108. </ScrollView>
  109. );
  110. }
  111. }
  112.  
  113. const styles = StyleSheet.create({
  114. title: {
  115. fontSize: 27,
  116. textAlign: 'center'
  117. },
  118.  
  119. icon: {
  120. position: 'absolute'
  121. },
  122.  
  123. pw: {
  124. paddingRight: 90,
  125. justifyContent: 'flex-end',
  126. marginBottom: 20,
  127. backgroundColor: '#9b42f4',
  128. height: 40,
  129. borderWidth: 1,
  130. borderRadius: 5
  131. },
  132.  
  133. buttons: {
  134. fontFamily: 'AvenirNext-Heavy'
  135. }
  136. });
  137.  
  138. const mapStateToProps = (state, ownProps) => {
  139. return {
  140. isLoggedIn: state.auth.isLoggedIn
  141. };
  142. }
  143.  
  144. const mapDispatchToProps = (dispatch) => {
  145. return {
  146. onLogin: (username, password) => { dispatch(login(username, password)); }
  147. }
  148. }
  149.  
  150. export default connect(mapStateToProps, mapDispatchToProps)(Login);
  151.  
  152. import React, { Component } from 'react';
  153. import { connect } from 'react-redux';
  154. import { ScrollView, Text, View, Button } from 'react-native';
  155. import { logout } from '../redux/actions/auth';
  156. import DropdownMenu from 'react-native-dropdown-menu';
  157. import Icon from './Icon';
  158.  
  159. class Secured extends Component {
  160. userLogout(e) {
  161. this.props.onLogout();
  162. e.preventDefault();
  163. }
  164.  
  165. render() {
  166. var data = [["Choice 1"], ["Choice 2"], ["Choice 3"], ["Choice 4"]];
  167.  
  168. return(
  169. <ScrollView style={{padding: 20}}>
  170. <Icon/>
  171.  
  172. <Text style={{fontSize: 27}}>
  173. {`Welcome ${this.props.username}`}
  174. </Text>
  175.  
  176. <View style={{flex: 1}}>
  177.  
  178. <DropdownMenu style={{flex: 1}}
  179. bgColor={"purple"} //the background color of the head, default is grey
  180. tintColor={"white"} //the text color of the head, default is white
  181. selectItemColor={"orange"} //the text color of the selected item, default is red
  182. data={data}
  183. maxHeight={410} // the max height of the menu
  184. handler={(selection, row) => alert(data[selection][row])} >
  185.  
  186. <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}} >
  187. </View>
  188. </DropdownMenu>
  189.  
  190. </View>
  191.  
  192. <View style={{margin: 20}}/>
  193.  
  194.  
  195. <Button onPress={(e) => this.userLogout(e)} title="Logout"/>
  196.  
  197. </ScrollView>
  198. );
  199. }
  200. }
  201.  
  202. const mapStateToProps = (state, ownProps) => {
  203. return {
  204. username: state.auth.username
  205. };
  206. }
  207.  
  208. const mapDispatchToProps = (dispatch) => {
  209. return {
  210. onLogout: () => { dispatch(logout()); }
  211. }
  212. }
  213.  
  214. export default connect(mapStateToProps, mapDispatchToProps)(Secured);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement