Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Loukissas aloukissas

View GitHub Profile
View config.js
export default {
AWS: {
cognito: {
REGION: "YOUR_COGNITO_REGION",
USER_POOL_ID: "YOUR_COGNITO_USER_POOL_ID",
APP_CLIENT_ID: "YOUR_COGNITO_APP_CLIENT_ID",
IDENTITY_POOL_ID: "YOUR_IDENTITY_POOL_ID"
}
}
};
View router.js
import React from "react";
import {
createStackNavigator,
createBottomTabNavigator,
createSwitchNavigator
} from "react-navigation";
import Signup from "./screens/Signup";
import Login from "./screens/Login";
import Home from "./screens/Home";
View Login.js
import React from "react";
import {
View,
Text,
StyleSheet,
Button,
TextInput,
TouchableOpacity,
Alert
} from "react-native";
View App.js
import React from "react";
import { createAppContainer } from "react-navigation";
import Amplify from "aws-amplify";
import { createRootNavigator } from "./router";
import config from "./config";
Amplify.configure({
Auth: {
mandatorySignIn: true,
region: config.AWS.cognito.REGION,
View App.js
import React from "react";
import { createAppContainer } from "react-navigation";
import Amplify, { Auth } from "aws-amplify";
import { createRootNavigator } from "./router";
import config from "./config";
Amplify.configure({
Auth: {
mandatorySignIn: true,
region: config.AWS.cognito.REGION,
View context.js
import React from "react";
export const AuthContext = React.createContext({});
export const withAuthContext = ChildComponent => props => (
<AuthContext.Consumer>
{context => <ChildComponent {...props} auth={context} />}
</AuthContext.Consumer>
);
View App-render.js
render() {
const RootNavigator = createRootNavigator(this.state.isAuthenticated);
const AppContainer = createAppContainer(RootNavigator);
return (
<AuthContext.Provider
value={{
isAuthenticated: this.state.isAuthenticated,
setAuthenticated: this.setAuthenticated
}}
View Login.js
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" });
}
};
View Login.js
import { withAuthContext } from "../context";
[...]
export default withAuthContext(Login);
You can’t perform that action at this time.