Use identity widget for auth if present
This commit is contained in:
parent
890ee3d7e3
commit
bba34e5979
@ -6,11 +6,31 @@ import logo from "./netlify_logo.svg";
|
|||||||
import styles from "./AuthenticationPage.css";
|
import styles from "./AuthenticationPage.css";
|
||||||
|
|
||||||
export default class AuthenticationPage extends React.Component {
|
export default class AuthenticationPage extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.identity = window.identity;
|
||||||
|
this.state = {user: this.identity && this.identity.gotrue && this.identity.gotrue.currentUser()};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
if (this.identity && !this.state.user) {
|
||||||
|
this.identity.on('login', (user) => {
|
||||||
|
this.props.onLogin(user);
|
||||||
|
this.identity.close();
|
||||||
|
});
|
||||||
|
this.identity.on('signup', (user) => {
|
||||||
|
this.props.onLogin(user);
|
||||||
|
this.identity.close();
|
||||||
|
});
|
||||||
|
this.identity.open();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
onLogin: React.PropTypes.func.isRequired,
|
onLogin: React.PropTypes.func.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
state = { username: "", password: "", errors: {} };
|
state = { email: "", password: "", errors: {} };
|
||||||
|
|
||||||
handleChange = (name, value) => {
|
handleChange = (name, value) => {
|
||||||
this.setState({ ...this.state, [name]: value });
|
this.setState({ ...this.state, [name]: value });
|
||||||
@ -19,10 +39,10 @@ export default class AuthenticationPage extends React.Component {
|
|||||||
handleLogin = (e) => {
|
handleLogin = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const { username, password } = this.state;
|
const { email, password } = this.state;
|
||||||
const errors = {};
|
const errors = {};
|
||||||
if (!username) {
|
if (!email) {
|
||||||
errors.username = 'Make sure to enter your user name';
|
errors.email = 'Make sure to enter your user name';
|
||||||
}
|
}
|
||||||
if (!password) {
|
if (!password) {
|
||||||
errors.password = 'Please enter your password';
|
errors.password = 'Please enter your password';
|
||||||
@ -33,7 +53,7 @@ export default class AuthenticationPage extends React.Component {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AuthenticationPage.authClient.login(this.state.username, this.state.password, true)
|
AuthenticationPage.authClient.login(this.state.email, this.state.password, true)
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
this.props.onLogin(user);
|
this.props.onLogin(user);
|
||||||
})
|
})
|
||||||
@ -45,6 +65,11 @@ export default class AuthenticationPage extends React.Component {
|
|||||||
render() {
|
render() {
|
||||||
const { errors } = this.state;
|
const { errors } = this.state;
|
||||||
const { error } = this.props;
|
const { error } = this.props;
|
||||||
|
|
||||||
|
if (this.identity) {
|
||||||
|
return <section className={styles.root}>;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={styles.root}>
|
<section className={styles.root}>
|
||||||
<Card className={styles.card}>
|
<Card className={styles.card}>
|
||||||
@ -58,11 +83,11 @@ export default class AuthenticationPage extends React.Component {
|
|||||||
</p>}
|
</p>}
|
||||||
<Input
|
<Input
|
||||||
type="text"
|
type="text"
|
||||||
label="Username"
|
label="Email"
|
||||||
name="username"
|
name="email"
|
||||||
value={this.state.username}
|
value={this.state.email}
|
||||||
error={errors.username}
|
error={errors.email}
|
||||||
onChange={this.handleChange.bind(this, "username")} // eslint-disable-line
|
onChange={this.handleChange.bind(this, "email")} // eslint-disable-line
|
||||||
/>
|
/>
|
||||||
<Input
|
<Input
|
||||||
type="password"
|
type="password"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user