Guest User

Untitled

a guest
Jul 26th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. // SignUp.js
  2. import React, { Fragment } from 'react'
  3. import {
  4. View,
  5. Button,
  6. TextInput,
  7. StyleSheet
  8. } from 'react-native'
  9. import {Navigation} from 'react-native-navigation';
  10.  
  11. import { Auth } from 'aws-amplify'
  12.  
  13. const initialState = {
  14. username: '', password: '', email: '', phone_number: '', authenticationCode: '', showConfirmationForm: false
  15. }
  16.  
  17. export default class SignUp extends React.Component {
  18. state = initialState
  19. onChangeText = (key, val) => {
  20. this.setState({ [key]: val })
  21. }
  22. signUp = async () => {
  23. const { username, password, email, phone_number } = this.state
  24. try {
  25. const success = await Auth.signUp({ username, password, attributes: { email, phone_number }})
  26. console.log('user successfully signed up!: ', success)
  27. this.setState({ showConfirmationForm: true })
  28. } catch (err) {
  29. console.log('error signing up: ', err)
  30. }
  31. }
  32. confirmSignUp = async () => {
  33. const { username, authenticationCode } = this.state
  34. try {
  35. await Auth.confirmSignUp(username, authenticationCode)
  36. console.log('successully signed up!')
  37. alert('User signed up successfully!')
  38. this.setState({ ...initialState })
  39. } catch (err) {
  40. console.log('error confirming signing up: ', err)
  41. }
  42. }
  43. render() {
  44. return (
  45. <View style={styles.container}>
  46. {
  47. !this.state.showConfirmationForm && (
  48. <Fragment>
  49. <TextInput
  50. style={styles.input}
  51. placeholder='Username'
  52. autoCapitalize="none"
  53. placeholderTextColor='white'
  54. onChangeText={val => this.onChangeText('username', val)}
  55. />
  56. <TextInput
  57. style={styles.input}
  58. placeholder='Password'
  59. secureTextEntry={true}
  60. autoCapitalize="none"
  61. placeholderTextColor='white'
  62. onChangeText={val => this.onChangeText('password', val)}
  63. />
  64. <TextInput
  65. style={styles.input}
  66. placeholder='Email'
  67. autoCapitalize="none"
  68. placeholderTextColor='white'
  69. onChangeText={val => this.onChangeText('email', val)}
  70. />
  71. <TextInput
  72. style={styles.input}
  73. placeholder='Phone Number'
  74. autoCapitalize="none"
  75. placeholderTextColor='white'
  76. onChangeText={val => this.onChangeText('phone_number', val)}
  77. />
  78. <Button
  79. title='Sign Up'
  80. onPress={this.signUp}
  81. />
  82. </Fragment>
  83. )
  84. }
  85. {
  86. this.state.showConfirmationForm && (
  87. <Fragment>
  88. <TextInput
  89. style={styles.input}
  90. placeholder='Authentication code'
  91. autoCapitalize="none"
  92. placeholderTextColor='white'
  93. onChangeText={val => this.onChangeText('authenticationCode', val)}
  94. />
  95. <Button
  96. title='Confirm Sign Up'
  97. onPress={this.confirmSignUp}
  98. />
  99. </Fragment>
  100. )
  101. }
  102. </View>
  103. )
  104. }
  105. }
  106.  
  107. const styles = StyleSheet.create({
  108. input: {
  109. width: 350,
  110. height: 55,
  111. backgroundColor: '#42A5F5',
  112. margin: 10,
  113. padding: 8,
  114. color: 'white',
  115. borderRadius: 14,
  116. },
  117. container: {
  118. flex: 1,
  119. justifyContent: 'center',
  120. alignItems: 'center'
  121. }
  122. })
Add Comment
Please, Sign In to add comment