How can i store token from facebook for successful login in react js?
Iam trying to implement login with facebook in my application the problem is i can see in the console that iam getting access token on correct facebook credentials but iam unable to store that token because of which login flow is not complete can anyone tell me about how can i store the token and complete user login.
import React, { Component } from "react";
import { EMAIL_REGEX } from "../../../common/constants/variables";
import IntlMessages from "../../../common/constants/IntlMessages";
// import Facebook from "./facebook";
import style from "../../../theme/style";
import FacebookLogin from "react-facebook-login";
export default class Comp extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: "",
emailError: "",
passError: "",
remember: true,
isLoggedIn: false,
userID: "",
name: "",
picture: ""
};
}
submitLogin = async () => {
const error = await this.checkLogin();
if (error) {
return;
} else {
const { password, email } = this.state;
this.props.loginUser({ email, password });
}
};
componentDidUpdate(prevProps, prevState) {
if (prevProps.loginInfo != this.props.loginInfo) {
if (!this.props.loginInfo.status) {
this.props.alertPopUp({
text: ["Incorrect Credentials are entered"],
open: true,
type: "error",
});
} else {
this.props.toggleModal();
this.props.authenticateUser(true);
}
}
}
//facebook response function
responseFacebook = response => {
console.log(response.accessToken);
this.setState({
isLoggedIn: true,
userID: response.userID,
name: response.name,
email: response.email,
picture: response.picture.data.url
});
};
componentClicked = (data) => {
console.log("data", data);
};
checkLogin = () => {
this.setState({
emailError: "",
passError: "",
});
let resolveBol = false;
return new Promise((resolve, reject) => {
if (this.state.email.length < 1) {
this.setState({ emailError: "påkrævet" });
resolveBol = true;
}
if (this.state.password.length < 1) {
resolveBol = true;
this.setState({ passError: "påkrævet" });
}
if (this.state.email.length > 1 && !EMAIL_REGEX.test(this.state.email)) {
this.setState({ emailError: "Ugyldig e-mailadresse" });
resolveBol = true;
}
resolve(resolveBol);
});
};
render() {
return (
<React.Fragment>
<div className="modal-col">
<button
type="button"
className="close"
onClick={() => this.props.toggleModal()}
>
<i className="fal fa-close"></i>
</button>
<h1>
<IntlMessages id="login.heading" />
</h1>
<p>
<IntlMessages id="login.detail" />
</p>
<div className="form">
<div className="form-group">
<input
type="email"
className="form-control"
placeholder="Enter your Email"
value={this.state.email}
onChange={(e) => this.setState({ email: e.target.value })}
/>
<span
className={this.state.emailError.length > 0 ? "errorLogin" : ""}
>
{this.state.emailError}
</span>
</div>
<div className="form-group">
<input
type="password"
className="form-control"
placeholder="Enter your Password"
value={this.state.password}
onChange={(e) => this.setState({ password: e.target.value })}
/>
<span
className={this.state.passError.length > 0 ? "errorLogin" : ""}
>
{this.state.passError}
</span>
</div>
<div className="form-group clearfix">
<div className="pull-left">
<div className="custom-control custom-checkbox">
<input
type="checkbox"
className="custom-control-input"
id="customCheck"
name="example1"
checked={this.state.remember}
onChange={(e) =>
this.setState({ remember: e.target.checked })
}
/>
<label className="custom-control-label" htmlFor="customCheck">
<IntlMessages id="login.remember.checkbox" />
</label>
</div>
</div>
<div
className="pull-right"
style={style.link}
onClick={() => {
this.props.toggleModal();
setTimeout(() => {
this.props.toggleForget();
}, 350);
}}
>
<a className="forget-password">
<IntlMessages id="login.forget" />
</a>
</div>
</div>
<div className="form-group">
<ul className="btn-list">
<li className="w-50">
<button
className="w-100 bg-warning"
onClick={this.submitLogin}
>
<IntlMessages id="login.button" />
</button>
</li>
<li className="w-50">
// facebook login button comp
<FacebookLogin
appId="285970052812801"
autoLoad={false}
fields="name,email,picture"
onClick={this.componentClicked}
callback={this.responseFacebook}
cssClass="w-100 bg-info"
/>
</li>
</ul>
</div>
<hr />
<div className="form-group">
<p>
<IntlMessages id="login.detail.second" />
</p>
<a
className="btn bg-light btn-default w-100"
onClick={() => this.props.toggleUserSign()}
>
<IntlMessages id="login.create.user" />
</a>
</div>
</div>
</div>
</React.Fragment>
);
}
}