feat: authentication, signup and login
This commit is contained in:
@@ -8,10 +8,13 @@ const Navbar = () => (
|
||||
<Link to={"/"}>Landing</Link>
|
||||
</li>
|
||||
<li>
|
||||
<Link to={"/signin"}>Sign In</Link>
|
||||
<Link to={"/user"}>Account</Link>
|
||||
</li>
|
||||
<li>
|
||||
<Link to={"/user"}>Account</Link>
|
||||
<Link to={"/login"}>Login</Link>
|
||||
</li>
|
||||
<li>
|
||||
<Link to={"/register"}>Register</Link>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
5
src/components/firebase/context.js
Normal file
5
src/components/firebase/context.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import React from "react";
|
||||
|
||||
const FirebaseContext = React.createContext(null);
|
||||
|
||||
export default FirebaseContext;
|
||||
37
src/components/firebase/fire.js
Normal file
37
src/components/firebase/fire.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import app from "firebase/app";
|
||||
import "firebase/auth";
|
||||
|
||||
const config = {
|
||||
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
|
||||
authDomain: process.env.REACT_APP_DEV_AUTH_DOMAIN,
|
||||
databaseURL: process.env.REACT_APP_DEV_DATABASE_URL,
|
||||
projectId: process.env.REACT_APP_DEV_PROJECT_ID,
|
||||
storageBucket: process.env.REACT_APP_DEV_STORAGE_BUCKET,
|
||||
messagingSenderId: process.env.REACT_APP_DEV_MESSAGING_SENDER_ID
|
||||
};
|
||||
console.log("--------------------");
|
||||
console.log(process.env.NODE_ENV);
|
||||
console.log(process.env.REACT_APP_FIREBASE_API_KEY);
|
||||
console.log("--------------------");
|
||||
|
||||
class Firebase {
|
||||
constructor() {
|
||||
app.initializeApp(config);
|
||||
this.auth = app.auth();
|
||||
}
|
||||
|
||||
// Auth
|
||||
doCreateUserWithEmailAndPassword = (email, password) =>
|
||||
this.auth.createUserWithEmailAndPassword(email, password);
|
||||
|
||||
doSignInWithEmailAndPassword = (email, password) =>
|
||||
this.auth.signInWithEmailAndPassword(email, password);
|
||||
|
||||
doSignOut = () => this.auth.signOut();
|
||||
|
||||
doPasswordReset = email => this.auth.sendPasswordResetEmail(email);
|
||||
|
||||
doPasswordUpdate = password => this.auth.currentUser.updatePassword(password);
|
||||
}
|
||||
|
||||
export default Firebase;
|
||||
6
src/components/firebase/index.js
Normal file
6
src/components/firebase/index.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import FirebaseContext from "./context";
|
||||
import Firebase from "./fire";
|
||||
|
||||
export default Firebase;
|
||||
|
||||
export { FirebaseContext };
|
||||
79
src/components/screens/Login/Login.js
Normal file
79
src/components/screens/Login/Login.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import React, { Component } from "react";
|
||||
import { withRouter, Link } from "react-router-dom";
|
||||
|
||||
import { FirebaseContext } from "../../firebase";
|
||||
|
||||
const LoginPage = ({ history }) => (
|
||||
<div>
|
||||
<h1>Login</h1>
|
||||
<FirebaseContext.Consumer>
|
||||
{firebase => <LoginForm history={history} firebase={firebase} />}
|
||||
</FirebaseContext.Consumer>
|
||||
<p>
|
||||
Don't have an account? <Link to={"/register"}>Sign Up</Link>
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
|
||||
class LoginFormBase extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.state = { email: "", password: "", error: null };
|
||||
}
|
||||
|
||||
onSubmit = event => {
|
||||
event.preventDefault();
|
||||
const { email, password } = this.state;
|
||||
|
||||
this.props.firebase
|
||||
.doSignInWithEmailAndPassword(email, password)
|
||||
.then(() => {
|
||||
this.setState({ email: "", password: "", error: null });
|
||||
this.props.history.push("/home");
|
||||
})
|
||||
.catch(error => {
|
||||
this.setState({ error });
|
||||
});
|
||||
};
|
||||
|
||||
onChange = event => {
|
||||
this.setState({ [event.target.name]: event.target.value });
|
||||
};
|
||||
|
||||
render() {
|
||||
const { email, password, error } = this.state;
|
||||
|
||||
const isInvalid = password === "" || email === "";
|
||||
|
||||
return (
|
||||
<form onSubmit={this.onSubmit}>
|
||||
<input
|
||||
name="email"
|
||||
value={email}
|
||||
onChange={this.onChange}
|
||||
type="text"
|
||||
placeholder="Email Address"
|
||||
/>
|
||||
<input
|
||||
name="password"
|
||||
value={password}
|
||||
onChange={this.onChange}
|
||||
type="password"
|
||||
placeholder="Password"
|
||||
/>
|
||||
<button disabled={isInvalid} type="submit">
|
||||
Sign In
|
||||
</button>
|
||||
|
||||
{error && <p>{error.message}</p>}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const LoginForm = withRouter(LoginFormBase);
|
||||
|
||||
export default LoginPage;
|
||||
|
||||
export { LoginForm };
|
||||
2
src/components/screens/Login/index.js
Normal file
2
src/components/screens/Login/index.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import Login from "./Login";
|
||||
export default Login;
|
||||
105
src/components/screens/Register/Register.js
Normal file
105
src/components/screens/Register/Register.js
Normal file
@@ -0,0 +1,105 @@
|
||||
import React, { Component } from "react";
|
||||
import { Link, withRouter } from "react-router-dom";
|
||||
|
||||
import { FirebaseContext } from "../../firebase";
|
||||
|
||||
const RegisterPage = ({ history }) => (
|
||||
<div>
|
||||
<h1>Register</h1>
|
||||
<FirebaseContext.Consumer>
|
||||
{firebase => <RegisterForm history={history} firebase={firebase} />}
|
||||
</FirebaseContext.Consumer>
|
||||
</div>
|
||||
);
|
||||
|
||||
class RegisterForm extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
username: "",
|
||||
email: "",
|
||||
passwordOne: "",
|
||||
passwordTwo: "",
|
||||
error: null
|
||||
};
|
||||
}
|
||||
|
||||
onSubmit = event => {
|
||||
const { username, email, passwordOne } = this.state;
|
||||
|
||||
this.props.firebase
|
||||
.doCreateUserWithEmailAndPassword(email, passwordOne)
|
||||
.then(authUser => {
|
||||
this.setState({
|
||||
username: "",
|
||||
email: "",
|
||||
passwordOne: "",
|
||||
passwordTwo: "",
|
||||
error: null
|
||||
});
|
||||
this.props.history.push("/home");
|
||||
})
|
||||
.catch(error => {
|
||||
this.setState({ error });
|
||||
});
|
||||
|
||||
event.preventDefault();
|
||||
};
|
||||
|
||||
onChange = event => {
|
||||
this.setState({ [event.target.name]: event.target.value });
|
||||
};
|
||||
|
||||
render() {
|
||||
const { username, email, passwordOne, passwordTwo, error } = this.state;
|
||||
|
||||
const isInvalid =
|
||||
passwordOne !== passwordTwo ||
|
||||
passwordOne === "" ||
|
||||
email === "" ||
|
||||
username === "";
|
||||
|
||||
return (
|
||||
<form onSubmit={this.onSubmit}>
|
||||
<input
|
||||
name="username"
|
||||
value={username}
|
||||
onChange={this.onChange}
|
||||
type="text"
|
||||
placeholder="Full Name"
|
||||
/>
|
||||
<input
|
||||
name="email"
|
||||
value={email}
|
||||
onChange={this.onChange}
|
||||
type="text"
|
||||
placeholder="Email Address"
|
||||
/>
|
||||
<input
|
||||
name="passwordOne"
|
||||
value={passwordOne}
|
||||
onChange={this.onChange}
|
||||
type="password"
|
||||
placeholder="Password"
|
||||
/>
|
||||
<input
|
||||
name="passwordTwo"
|
||||
value={passwordTwo}
|
||||
onChange={this.onChange}
|
||||
type="password"
|
||||
placeholder="Confirm Password"
|
||||
/>
|
||||
<button disabled={isInvalid} type="submit">
|
||||
Sign Up
|
||||
</button>
|
||||
|
||||
{error && <p>{error.message}</p>}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default withRouter(RegisterPage);
|
||||
|
||||
export { RegisterForm };
|
||||
2
src/components/screens/Register/index.js
Normal file
2
src/components/screens/Register/index.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import Register from "./Register";
|
||||
export default Register;
|
||||
@@ -1,5 +1,5 @@
|
||||
import React from "react";
|
||||
import fire from "../../../fire.js";
|
||||
import fire from "../../firebase/fire.js";
|
||||
|
||||
class User extends React.Component {
|
||||
state = {
|
||||
|
||||
Reference in New Issue
Block a user