Skip to content

Instantly share code, notes, and snippets.

@RizkyRajitha
Created Mar 23, 2019
Embed
What would you like to do?
//bkend
router.post("/login1", function(req, res, next) {
passport.authenticate("local", function(err, user, info) {
console.log("ppppp");
if (err) {
console.log("error no user");
return next(err);
}
if (!user) {
console.log("error no1");
console.log(info.message);
return res.send(user);
}
req.logIn(user, function(err) {
if (err) {
return next(err);
} else {
console.log("done");
var token = user.generateJWT();
// res.cookie("jwt", token, { httpOnly: true, secure: true });
return res.status(200).send(token);
}
});
})(req, res, next);
});
//frontend
import React, { Component } from "react";
import { Redirect } from "react-router-dom";
import "../App.css";
const jsonwebtoken = require("jsonwebtoken");
//const request = require("request");
const axios = require("axios");
class login extends Component {
state = {
token: "",
// email: "",
// password: "",
loggedIn: false,
showError: false,
showNullError: false
};
changehandleremail = event => {
this.setState({
email: event.target.value
});
};
changehandlerpass = event => {
this.setState({
password: event.target.value
});
};
componentDidMount() {
var jwt = localStorage.getItem("jwt");
console.log("comp mount");
console.log(jwt);
try {
var tk = jsonwebtoken.verify(jwt, "authdemo");
if (tk) {
console.log("loged in");
this.setState({
loggedIn: true,
email: tk.email
});
}
} catch (error) {
console.log("not logged in" + error);
this.setState({
loggedIn: false
});
}
}
fogotpasswordhandler = () => {
this.props.history.push("/fogotpassword");
};
btn1handler = e => {
e.preventDefault();
console.log("cliking");
if (this.state.email === "" || this.state.password === "") {
console.log(this.state.email + " " + this.state.password);
console.log("wtf");
this.setState({
loggedIn: false,
showError: false,
showNullError: true
});
} else {
console.log("sending..............");
console.log(this.state.email + this.state.password);
var params = new URLSearchParams();
params.append("email", this.state.email);
params.append("password", this.state.password);
// data: {
// email: this.state.email,
// password: this.state.password
// }
axios
.post("/usr/login1", params)
.then(data => {
console.log(data);
var body = data.data;
console.log("body - " + body);
localStorage.setItem("jwt", body);
this.setState({
loggedIn: true,
showError: false,
showNullError: false
});
})
.catch(err => {
console.log(err);
this.setState({
loggedIn: false,
showError: true,
showNullError: false
});
});
// axios.post("/usr/login1", params, (err, res, body) => {
// console.log("errr - " + err);
// console.log("response - " + res);
// console.log("body - " + body);
// if (body !== "false") {
// console.log("body - " + body);
// localStorage.setItem("jwt", body);
// this.setState({
// loggedIn: true,
// showError: false,
// showNullError: false
// });
// } else {
// console.log(err);
// this.setState({
// loggedIn: false,
// showError: true,
// showNullError: false
// });
// }
// // this.setState({ email: data.email, id: data.id });
// });
}
};
render() {
const { email, password, showError, loggedIn, showNullError } = this.state;
if (!loggedIn) {
return (
<div className="container">
<div className="row">
<div className="col-sm" />
<div className="col-sm">
<form onSubmit={this.btn1handler}>
<br />
<br />
<br />
<div className="form-group">
{/* <label> enter email </label> */}
<input
type="text"
name="uname"
className="form-control"
onChange={this.changehandleremail}
placeholder="enter email"
/>
</div>
<div className="form-group">
{/* <label> enter password </label> */}
<input
type="text"
name="pass"
className="form-control"
placeholder="enter password"
onChange={this.changehandlerpass}
/>
</div>
<input
type="submit"
className="btn btn-primary"
value="sign in"
/>
</form>
<br />
<input
type="button"
className="btn btn-primary"
value="fogot password"
onClick={this.fogotpasswordhandler}
/>
{showNullError && (
<div>
<p>The username or password cannot be null.</p>
</div>
)}
{showError && (
<div>
<p>The username or password is incorrect dude XD.</p>
</div>
)}
</div>
<div className="col-sm" />
</div>
</div>
);
} else {
return <Redirect to={`/dashboard`} />;
}
}
}
export default login;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment