Last active
January 19, 2019 10:53
-
-
Save youneshenniwrites/8f0de488cb7c696599b33a242cd87303 to your computer and use it in GitHub Desktop.
Sign up methods with AWS Amplify Auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sign up user with AWS Amplify Auth | |
async signUp() { | |
const { username, password, email, phoneNumber } = this.state | |
// rename variable to conform with Amplify Auth field phone attribute | |
const phone_number = phoneNumber | |
await Auth.signUp({ | |
username, | |
password, | |
attributes: { email, phone_number } | |
}) | |
.then(() => { | |
console.log('sign up successful!') | |
Alert.alert('Enter the confirmation code you received.') | |
}) | |
.catch(err => { | |
if (! err.message) { | |
console.log('Error when signing up: ', err) | |
Alert.alert('Error when signing up: ', err) | |
} else { | |
console.log('Error when signing up: ', err.message) | |
Alert.alert('Error when signing up: ', err.message) | |
} | |
}) | |
} | |
// Confirm users and redirect them to the SignIn page | |
async confirmSignUp() { | |
const { username, authCode } = this.state | |
await Auth.confirmSignUp(username, authCode) | |
.then(() => { | |
this.props.navigation.navigate('SignIn') | |
console.log('Confirm sign up successful') | |
}) | |
.catch(err => { | |
if (! err.message) { | |
console.log('Error when entering confirmation code: ', err) | |
Alert.alert('Error when entering confirmation code: ', err) | |
} else { | |
console.log('Error when entering confirmation code: ', err.message) | |
Alert.alert('Error when entering confirmation code: ', err.message) | |
} | |
}) | |
} | |
// Resend code if not received already | |
async resendSignUp() { | |
const { username } = this.state | |
await Auth.resendSignUp(username) | |
.then(() => console.log('Confirmation code resent successfully')) | |
.catch(err => { | |
if (! err.message) { | |
console.log('Error requesting new confirmation code: ', err) | |
Alert.alert('Error requesting new confirmation code: ', err) | |
} else { | |
console.log('Error requesting new confirmation code: ', err.message) | |
Alert.alert('Error requesting new confirmation code: ', err.message) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment