Skip to content

Instantly share code, notes, and snippets.

deleteUserCity = async () => {
try {
await AsyncStorage.removeItem("userCity");
} catch (error) {
console.log(`While removing userCity: ${error.message}`);
}
}
getUserCity = async () => {
try {
return await AsyncStorage.getItem("userCity");
catch (error) {
console.log(`While getting userCity: ${error.message}`);
return null;
}
}
setUserCity = async (city) => {
try {
await AsyncStorage.setItem("userCity", city);
} catch (error) {
console.log(`While storing userCity: ${error.message}`);
}
}
import React from "react";
import { View, Text, Platform } from "react-native";
import { Constants, Location, Permissions } from "expo";
class PermissionsExample extends React.Component {
state = {
message: null,
location: null
};
import React from "react";
import { View, Text } from "react-native";
import { Permissions } from "expo";
class PermissionsExample extends React.Component {
state = {
message: ""
};
async componentWillMount() {
import React from "react";
import { View, Text, StyleSheet, Button } from "react-native";
import { Auth } from "aws-amplify";
import { withAuthContext } from "../context";
class Home extends React.Component {
handleLogout = async () => {
await Auth.signOut();
this.props.auth.setAuthenticated(false);
};
import { withAuthContext } from "../context";
[...]
export default withAuthContext(Login);
handleSubmit = async () => {
try {
await Auth.signIn(this.state.email, this.state.password);
this.props.auth.setAuthenticated(true);
} catch (error) {
Alert.alert("Error logging in", error, { text: "OK" });
}
};
render() {
const RootNavigator = createRootNavigator(this.state.isAuthenticated);
const AppContainer = createAppContainer(RootNavigator);
return (
<AuthContext.Provider
value={{
isAuthenticated: this.state.isAuthenticated,
setAuthenticated: this.setAuthenticated
}}
import React from "react";
export const AuthContext = React.createContext({});
export const withAuthContext = ChildComponent => props => (
<AuthContext.Consumer>
{context => <ChildComponent {...props} auth={context} />}
</AuthContext.Consumer>
);