Flare-k

[Authenticate] Facebook Login

......@@ -78,6 +78,20 @@ export const postGithubLogin = (req, res) => {
res.redirect(routes.home);
};
export const facebookLogin = passport.authenticate("facebook");
export const facebookLoginCallback = (
accessToken,
refreshToken,
profile,
cb
) => {
console.log(accessToken, refreshToken, profile, cb);
};
export const postFacebookLogin = (req, res) => {
res.redirect(routes.home);
};
// 로그아웃을 클릭하면 LogOut페이지로 가는 것 대신에, 로그아웃을 처리한 후
// home 페이지로 Redirect로 표현할 것이다.
// 즉, 초반에 만들어둔 logout.pug는 삭제해도 좋다.
......@@ -91,8 +105,18 @@ export const getMe = (req, res) =>
res.render("userDetail", { pageTitle: "User Detail", user: req.user });
// export const users = (req, res) => res.render("users", { pageTitle: "Users" });
export const userDetail = (req, res) =>
res.render("userDetail", { pageTitle: "User Detail" });
export const userDetail = async (req, res) => {
const {
params: { id },
} = req; // req로 부터 params의 id가져오기
try {
const user = await User.findById(id);
res.render("userDetail", { pageTitle: "User Detail", user });
} catch (error) {
res.redirect(routes.home);
}
};
export const editProfile = (req, res) =>
res.render("editProfile", { pageTitle: "Edit Profile" });
export const changePassword = (req, res) =>
......
......@@ -35,6 +35,7 @@
"multer": "^1.4.2",
"node-sass": "^4.14.1",
"passport": "^0.4.1",
"passport-facebook": "^3.0.0",
"passport-github": "^1.1.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^6.0.1",
......
import dotenv from "dotenv";
import passport from "passport";
import GithubStrategy from "passport-github";
import FacebookStrategy from "passport-facebook";
import User from "./models/User";
import { githubLoginCallback } from "./controllers/userController";
import {
githubLoginCallback,
facebookLoginCallback,
} from "./controllers/userController";
import routes from "./routes";
dotenv.config();
......@@ -21,5 +25,16 @@ passport.use(
)
);
passport.use(
new FacebookStrategy(
{
clientID: process.env.FB_ID,
clientSecret: process.env.FB_SECRET,
callbackURL: `http://localhost:80${routes.facebookCallback}`,
},
facebookLoginCallback
)
);
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
......
......@@ -11,6 +11,8 @@ import {
githubLogin,
postGithubLogin,
getMe,
facebookLogin,
postFacebookLogin,
} from "../controllers/userController";
import { onlyPublic, onlyPrivate } from "../middlewares";
......@@ -35,4 +37,14 @@ globalRouter.get(
postGithubLogin
);
globalRouter.get(routes.me, getMe);
globalRouter.get(routes.facebook, facebookLogin);
globalRouter.get(
routes.facebookCallback,
passport.authenticate("facebook", {
failureRedirect: "/login",
}),
postFacebookLogin
);
export default globalRouter;
......
......@@ -23,6 +23,10 @@ const DELETE_VIDEO = "/:id/delete";
const GITHUB = "/auth/github";
const GITHUB_CALLBACK = "/auth/github/callback";
// Facebook
const FB = "/auth/facebook";
const FB_CALLBACK = "/auth/facebook/callback";
const routes = {
home: HOME,
join: JOIN,
......@@ -65,6 +69,8 @@ const routes = {
gitHub: GITHUB,
githubCallback: GITHUB_CALLBACK,
me: ME,
facebook: FB,
facebookCallback: FB_CALLBACK,
};
// template에서 직접 접근이 필요한 경우 함수로 바꿔준다.
export default routes;
......
......@@ -5,6 +5,7 @@
i.fab.fa-github
| Continue with Github
button.social-login--facebook
span
i.fab.fa-facebook
| Continue with Facebook
\ No newline at end of file
a(href=routes.facebook)
span
i.fab.fa-facebook
| Continue with Facebook
\ No newline at end of file
......