Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- res.cookie('jwt', token, { httpOnly: true, secure: false });
- const app = express()
- app.use(bodyParser.urlencoded({extended: true}));
- app.use(bodyParser.json());
- app.use(cookieParser());
- app.use(function(req, res, next) {
- res.header("Access-Control-Allow-Origin", "*");
- res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
- next();
- });
- app.post('/login', (req, res) => {
- passport.authenticate('local', { session: false }, (error, user) => {
- if (error || !user) {
- res.status(400).json({ error });
- }
- // Construct JWT payload
- const payload = {
- email: user.email,
- expires: Date.now() + parseInt(process.env.JWT_EXPIRATION_MS),
- };
- // Assign payload to req.user
- req.login(payload, {session: false}, (error) => {
- if (error) {
- res.status(400).send({ error });
- }
- // Generate a signed JWT
- const token = jwt.sign(JSON.stringify(payload), process.env.JWT_SECRET);
- // Assign JWT to cookie
- res.cookie('jwt', token, { httpOnly: true, secure: false });
- res.status(200).send({ email: user.email });
- });
- })(req, res);
- });
- handleLogin = async () => {
- const { name, email, password } = this.state
- try{
- const res = await axios.post('http://localhost:8080/login', {
- email: email,
- password: password,
- })
- if(res.status == 200){
- console.log("Logged in")
- console.log(res)
- }
- } catch (err) {
- console.log(err)
- }
- }
Add Comment
Please, Sign In to add comment