Skip to content

Instantly share code, notes, and snippets.

@jatazoulja
Created August 1, 2018 01:00
Show Gist options
  • Save jatazoulja/e038249d25d75f2f153a76a4dbb457b5 to your computer and use it in GitHub Desktop.
Save jatazoulja/e038249d25d75f2f153a76a4dbb457b5 to your computer and use it in GitHub Desktop.
Context with HoC for consumer.
import React, { createContext, Component } from "react";
import { Auth } from "aws-amplify";
const AuthContext = createContext({
isAuthenticated: false,
isAuthenticating: true
});
class AuthProvider extends Component {
state = {
isAuthenticated: false,
isAuthenticating: true,
isLogingIn: false
};
userHasAuthenticated = authenticated => {
this.setState({ isAuthenticated: authenticated });
};
async componentDidMount() {
try {
console.debug(await Auth.currentCredentials());
if (await Auth.currentCredentials()) {
this.userHasAuthenticated(true);
}
if (await Auth.currentSession()) {
this.userHasAuthenticated(true);
}
} catch (e) {
if (e !== "No current user") {
alert(e);
}
}
this.setState({ isAuthenticating: false });
}
handleSocialCallback = userToken => {
this.userHasAuthenticated(true);
};
handleLogout = async event => {
await Auth.signOut();
this.userHasAuthenticated(false);
this.props.history.push("/login");
};
handleLogin = async (event, email, password) => {
event.preventDefault();
this.setState({ isLogingIn: true });
try {
await Auth.signIn(email, password);
this.userHasAuthenticated(true);
} catch (e) {
alert(e.message);
this.setState({ isLogingIn: false });
}
};
render() {
return (
<AuthContext.Provider value={{ ...this }}>
{this.props.children}
</AuthContext.Provider>
);
}
}
function withContext(WrappedComponent, context) {
return class extends React.Component {
render() {
console.log(this);
return (
<AuthContext.Consumer>
{value => {
console.log(this, arguments, value);
return <WrappedComponent contextProps={{ ...value }} />;
}}
</AuthContext.Consumer>
);
}
};
}
export { AuthContext, withContext };
export default AuthProvider;
import React, { Component } from "react";
import { Auth } from "aws-amplify";
import Grid from "@material-ui/core/Grid";
import FacebookButton from "components/FacebookButton";
import LoaderButton from "components/LoaderButton";
import "./Login.css";
import TextField from "@material-ui/core/TextField";
import {
AuthContext,
withContext
} from "Packages/Context/AuthenticationContext";
class Login extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: false,
email: "",
password: ""
};
}
validateForm() {
return this.state.email.length > 0 && this.state.password.length > 0;
}
handleChange = event => {
this.setState({
[event.target.id]: event.target.value
});
};
render() {
const { contextProps } = this.props;
console.log(this);
return (
<Grid className="Login" container>
<form
onSubmit={e =>
contextProps.handleLogin(e, this.state.email, this.state.password)
}
>
<TextField
id="email"
autoFocus
label="Email"
shrink="false"
type="email"
value={this.state.email}
onChange={this.handleChange}
fullWidth
/>
<TextField
id="password"
label="Password"
margin="normal"
value={this.state.password}
shrink="false"
onChange={this.handleChange}
type="password"
fullWidth
/>
<LoaderButton
disabled={!this.validateForm()}
type="submit"
isLoading={contextProps.state.isLoading}
text="Login"
loadingText="Logging in…"
/>
</form>
<Grid className="Login" container>
<FacebookButton
buttonClass="fb-button-signup"
callback={contextProps.handleSocialCallback}
/>
</Grid>
</Grid>
);
}
}
Login = withContext(Login, AuthContext);
export default Login;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment