진재영

passport server main.js updated

// 사용 전 npm 설치
// npm install express express-session session-file-store passport passport-local
// npm install express express-session session-file-store passport passport-local//
// npm install body-parser --save
const express = require('express');
var express = require('express');
var app = express();
var fs = require('fs');
const session = require('express-session');
const exp = require('constants');
const passport = require('passport'), LocalStrategy = require('passport-local').Strategy;
const fileStore = require('session-file-store')(session);
const app = express();
var bodyParser = require('body-parser');
//미들웨어 리스트
app.use(express.urlencoded({extended:false}));
......@@ -18,6 +22,8 @@ app.use(session({
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended:false}));
//사용자 정보 session 읽기, 쓰기
passport.serializeUser(function(user, done) { //쓰기
......@@ -29,26 +35,23 @@ passport.deserializeUser(function(id, done) { //읽기
});
//메인 페이지
app.get('/',(req,res)=>{
let page = getPage('Passport','example page(수정하시기 바랍니다)',authInfo(req));
res.send(page);
app.get('/', function (req, res) {
fs.readFile('first.html', function(error, data) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
});
});
//로그인 페이지
app.get('/login',(req,res)=>{
let page = getPage('로그인',`
<form action="/login" method="post">
<input type="text" name="email" placeholder="email"><br>
<input type="password" name="password" placeholder="****"><br>
<div style="display : flex;justify-content:space-between;width: 153px;">
<input type="submit" value="로그인" style="display:inline-block;">
<a href="/join" style="background : #E5E5E5;padding : 2px; border: 0.5px solid black;cursor:pointer;border-radius:3px;font-size:13px;color:black;text-decoration:none;">회원가입</a>
</div>
</form>
`,`<a href="/">뒤로가기</a>`);
res.send(page);
app.get('/login',function(req, res) {
fs.readFile('login.html', function(error, data) {
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end(data);
})
});
//로그인 인증 (Passport)
passport.use(new LocalStrategy({
//로그인 페이지 input 태그 내 name
......@@ -63,10 +66,10 @@ passport.use(new LocalStrategy({
//아이디가 다를때
if (id !== user.email)
return done(null, false, { message: '아이디가 다다' });
return done(null, false, { message: '아이디가 다릅니다' });
//비밀번호가 다를때
else if (password !== user.password)
return done(null, false, { message: '비번이 다다' });
return done(null, false, { message: '비번이 다릅니다' });
//아이디, 비밀번호 모두 맞을 경우
return done(null, user);
}
......@@ -75,32 +78,35 @@ passport.use(new LocalStrategy({
//로그인 처리 (Passport)
app.post('/login',
passport.authenticate('local', {
//성공시, 메인페이지 이동
//실패시 로그인 페이지 이동
successRedirect: '/',
successRedirect: '/main',
failureRedirect: '/login'
}));
//회원가입 페이지 Get
app.get('/join',(req,res)=>{
let page = getPage('회원가입',`
<form action="/join" method="post">
<input type="email" name="email" placeholder="email"><br>
<input type="password" name="password" placeholder="****"><br>
<input type="name" name="name" placeholder="이름"><br>
<input type="submit" value="회원가입"><br>
</form>
`,'<a href="/login">뒤로가기</a>');
res.send(page);
app.get('/join',function(req, res) {
fs.readFile('register.html', function(error, data) {
res.writeHead(200, { 'Contect-Type': 'text/html'});
res.end(data);
})
});
//회원가입 처리 Post : 예제를 위해 간단 저장 방식으로 구현
app.get('/main',function(req, res) {
fs.readFile('main.html', function(error, data) {
res.writeHead(200, { 'Contect-Type': 'text/html'});
res.end(data);
})
});
//회원가입
var user = {};
app.post('/join',(req,res)=>{
app.post('/join',(req,res) =>{
user.email = req.body.email;
user.password = req.body.password;
user.name=req.body.name;
user.name = req.body.name;
//로그인 페이지로 이동
res.redirect('/login');
});
......@@ -119,7 +125,9 @@ app.get('/logout',(req,res)=>{
//포트 연결
app.listen(3000,()=>console.log(`http://localhost:3000`));
app.listen(3000, function() {
console.log('http://localhost:3000');
});
//로그인 로그아웃 여부
......@@ -128,22 +136,3 @@ const authInfo = (req)=>{
return `<a href="/login">login</a>`;
}
//페이지 템플릿
const getPage = (title, content, auth) =>{
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Passport Example</title>
</head>
<body>
${auth}
<h1>${title}</h1>
<p>${content}</p>
</body>
</html>
`;
}
\ No newline at end of file
......