Advertisement
Guest User

Untitled

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