Skip to content

Instantly share code, notes, and snippets.

@ryanjones
Last active March 26, 2019 01:52
Show Gist options
  • Save ryanjones/58b6dbebcf9d4a30ab09f127719da1b7 to your computer and use it in GitHub Desktop.
Save ryanjones/58b6dbebcf9d4a30ab09f127719da1b7 to your computer and use it in GitHub Desktop.
React Native App.js with authenticator
import React from 'react';
import { Linking, Button, StyleSheet, Text, View } from 'react-native';
import Auth from '@aws-amplify/auth';
import Analytics from '@aws-amplify/analytics';
import { withAuthenticator } from 'aws-amplify-react-native';
import awsconfig from './aws-exports';
// retrieve temporary AWS credentials and sign requests
Auth.configure(awsconfig);
// send analytics events to Amazon Pinpoint
Analytics.configure(awsconfig);
class App extends React.Component {
constructor(props) {
super(props);
this.handleAnalyticsClick = this.handleAnalyticsClick.bind(this);
this.state = {resultHtml: <Text></Text>, eventsSent: 0};
}
handleAnalyticsClick() {
Analytics.record('AWS Amplify Tutorial Event')
.then( (evt) => {
const url = 'https://'+awsconfig.aws_project_region+'.console.aws.amazon.com/pinpoint/home/?region='+awsconfig.aws_project_region+'#/apps/'+awsconfig.aws_mobile_analytics_app_id+'/analytics/events';
let result = (
<View>
<Text>Event Submitted.</Text>
<Text>Events sent: {++this.state.eventsSent}</Text>
<Text style={styles.link} onPress={() => Linking.openURL(url)}>
View Events on the Amazon Pinpoint Console
</Text>
</View>
);
this.setState({
'resultHtml': result
});
});
};
render() {
return (
<View style={styles.container}>
<Text>Welcome to your React Native App with Amplify!</Text>
<Button title="Generate Analytics Event" onPress={this.handleAnalyticsClick} />
{this.state.resultHtml}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
link: {
color: 'blue'
}
});
const signUpConfig = {
hideAllDefaults: true,
signUpFields: [
{
label: 'Email',
key: 'email',
required: true,
displayOrder: 1,
type: 'string',
custom: false
},
{
label: 'Password',
key: 'password',
required: true,
displayOrder: 2,
type: 'password',
custom: false
},
{
label: 'Username',
key: 'username',
required: false,
displayOrder: 3,
type: 'string',
custom: false
},
{
label: 'Phone Number',
key: 'phone_number',
required: false,
displayOrder: 4,
type: 'tel',
custom: false
}
]
};
export default withAuthenticator(App, { signUpConfig });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment