From 96458fa9fddd7fd1f89f3ebbaf41d507ffb0960f Mon Sep 17 00:00:00 2001 From: epson220 <62466439+epson220@users.noreply.github.com> Date: Wed, 25 Nov 2020 19:44:00 +0900 Subject: [PATCH] passport-local-2 --- backend/routes/users.js | 30 ++++++++++++++++++++++-------- frontend/src/Profile.js | 27 +++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/backend/routes/users.js b/backend/routes/users.js index bd5bf26..6e6abdd 100644 --- a/backend/routes/users.js +++ b/backend/routes/users.js @@ -97,14 +97,28 @@ passport.deserializeUser(function (user, done) { done(null, user); }); -router.post( - "/login", - passport.authenticate("local-login", { - successRedirect: "/profile", - failureRedirect: "/login", - failureFlash: true, - }) -); +router.post("/login", function (req, res, next) { + passport.authenticate("local-login", function (err, user, info) { + if (err) { + console.log(err); + return next(err); + } + + if (user) { + console.log("req.user : " + JSON.stringify(user)); + let json = JSON.parse(JSON.stringify(user)); + + req.logIn(user, function (err) { + if (err) { + return next(err); + } + }); + } else { + console.log("login fail!!!!!!!!!!!!!!!"); + res.send([]); + } + })(req, res, next); +}); router.post( "/signup", diff --git a/frontend/src/Profile.js b/frontend/src/Profile.js index ce4a1fd..998113d 100644 --- a/frontend/src/Profile.js +++ b/frontend/src/Profile.js @@ -1,3 +1,26 @@ -import React from "react"; +import React, { useState } from "react"; -const Profile = () => {}; +const Profile = (login_info) => { + const [user, setUser] = useState(""); + + fetch("/login", { + method: "post", + headers: { + "Content-Type": "application/json; charset=utf-8", + }, + credentials: "same-origin", + body: JSON.stringify(login_info), + }) + .then((res) => res.json()) + .then((data) => { + console.dir(data); + setUser(data); + }); + + return ( + <div> + <h1>{user.username}</h1> + <h1>{user.nicname}</h1> + </div> + ); +}; -- GitLab