Showing
1 changed file
with
46 additions
and
57 deletions
| 1 | // 사용 전 npm 설치 | 1 | // 사용 전 npm 설치 |
| 2 | -// npm install express express-session session-file-store passport passport-local | 2 | +// npm install express express-session session-file-store passport passport-local// |
| 3 | +// npm install body-parser --save | ||
| 3 | 4 | ||
| 4 | -const express = require('express'); | 5 | +var express = require('express'); |
| 6 | +var app = express(); | ||
| 7 | +var fs = require('fs'); | ||
| 5 | const session = require('express-session'); | 8 | const session = require('express-session'); |
| 9 | +const exp = require('constants'); | ||
| 6 | const passport = require('passport'), LocalStrategy = require('passport-local').Strategy; | 10 | const passport = require('passport'), LocalStrategy = require('passport-local').Strategy; |
| 7 | - | ||
| 8 | const fileStore = require('session-file-store')(session); | 11 | const fileStore = require('session-file-store')(session); |
| 9 | -const app = express(); | 12 | +var bodyParser = require('body-parser'); |
| 13 | + | ||
| 10 | 14 | ||
| 11 | //미들웨어 리스트 | 15 | //미들웨어 리스트 |
| 12 | app.use(express.urlencoded({extended:false})); | 16 | app.use(express.urlencoded({extended:false})); |
| ... | @@ -18,6 +22,8 @@ app.use(session({ | ... | @@ -18,6 +22,8 @@ app.use(session({ |
| 18 | })); | 22 | })); |
| 19 | app.use(passport.initialize()); | 23 | app.use(passport.initialize()); |
| 20 | app.use(passport.session()); | 24 | app.use(passport.session()); |
| 25 | +app.use(express.static('public')); | ||
| 26 | +app.use(bodyParser.urlencoded({extended:false})); | ||
| 21 | 27 | ||
| 22 | //사용자 정보 session 읽기, 쓰기 | 28 | //사용자 정보 session 읽기, 쓰기 |
| 23 | passport.serializeUser(function(user, done) { //쓰기 | 29 | passport.serializeUser(function(user, done) { //쓰기 |
| ... | @@ -29,26 +35,23 @@ passport.deserializeUser(function(id, done) { //읽기 | ... | @@ -29,26 +35,23 @@ passport.deserializeUser(function(id, done) { //읽기 |
| 29 | }); | 35 | }); |
| 30 | 36 | ||
| 31 | //메인 페이지 | 37 | //메인 페이지 |
| 32 | -app.get('/',(req,res)=>{ | 38 | +app.get('/', function (req, res) { |
| 33 | - let page = getPage('Passport','example page(수정하시기 바랍니다)',authInfo(req)); | 39 | + fs.readFile('first.html', function(error, data) { |
| 34 | - res.send(page); | 40 | + res.writeHead(200, { 'Content-Type': 'text/html' }); |
| 41 | + res.end(data); | ||
| 42 | + }); | ||
| 35 | }); | 43 | }); |
| 36 | 44 | ||
| 37 | //로그인 페이지 | 45 | //로그인 페이지 |
| 38 | -app.get('/login',(req,res)=>{ | 46 | +app.get('/login',function(req, res) { |
| 39 | - let page = getPage('로그인',` | 47 | + fs.readFile('login.html', function(error, data) { |
| 40 | - <form action="/login" method="post"> | 48 | + res.writeHead(200, { 'Content-Type': 'text/html'}); |
| 41 | - <input type="text" name="email" placeholder="email"><br> | 49 | + res.end(data); |
| 42 | - <input type="password" name="password" placeholder="****"><br> | 50 | + }) |
| 43 | - <div style="display : flex;justify-content:space-between;width: 153px;"> | ||
| 44 | - <input type="submit" value="로그인" style="display:inline-block;"> | ||
| 45 | - <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> | ||
| 46 | - </div> | ||
| 47 | - </form> | ||
| 48 | - `,`<a href="/">뒤로가기</a>`); | ||
| 49 | - res.send(page); | ||
| 50 | }); | 51 | }); |
| 51 | 52 | ||
| 53 | + | ||
| 54 | + | ||
| 52 | //로그인 인증 (Passport) | 55 | //로그인 인증 (Passport) |
| 53 | passport.use(new LocalStrategy({ | 56 | passport.use(new LocalStrategy({ |
| 54 | //로그인 페이지 input 태그 내 name | 57 | //로그인 페이지 input 태그 내 name |
| ... | @@ -63,10 +66,10 @@ passport.use(new LocalStrategy({ | ... | @@ -63,10 +66,10 @@ passport.use(new LocalStrategy({ |
| 63 | 66 | ||
| 64 | //아이디가 다를때 | 67 | //아이디가 다를때 |
| 65 | if (id !== user.email) | 68 | if (id !== user.email) |
| 66 | - return done(null, false, { message: '아이디가 다르다' }); | 69 | + return done(null, false, { message: '아이디가 다릅니다' }); |
| 67 | //비밀번호가 다를때 | 70 | //비밀번호가 다를때 |
| 68 | else if (password !== user.password) | 71 | else if (password !== user.password) |
| 69 | - return done(null, false, { message: '비번이 다르다' }); | 72 | + return done(null, false, { message: '비번이 다릅니다' }); |
| 70 | //아이디, 비밀번호 모두 맞을 경우 | 73 | //아이디, 비밀번호 모두 맞을 경우 |
| 71 | return done(null, user); | 74 | return done(null, user); |
| 72 | } | 75 | } |
| ... | @@ -75,32 +78,35 @@ passport.use(new LocalStrategy({ | ... | @@ -75,32 +78,35 @@ passport.use(new LocalStrategy({ |
| 75 | //로그인 처리 (Passport) | 78 | //로그인 처리 (Passport) |
| 76 | app.post('/login', | 79 | app.post('/login', |
| 77 | passport.authenticate('local', { | 80 | passport.authenticate('local', { |
| 78 | - //성공시, 메인페이지 이동 | 81 | + |
| 79 | - //실패시 로그인 페이지 이동 | 82 | + successRedirect: '/main', |
| 80 | - successRedirect: '/', | ||
| 81 | failureRedirect: '/login' | 83 | failureRedirect: '/login' |
| 82 | })); | 84 | })); |
| 83 | 85 | ||
| 84 | 86 | ||
| 85 | //회원가입 페이지 Get | 87 | //회원가입 페이지 Get |
| 86 | -app.get('/join',(req,res)=>{ | 88 | +app.get('/join',function(req, res) { |
| 87 | - let page = getPage('회원가입',` | 89 | + fs.readFile('register.html', function(error, data) { |
| 88 | - <form action="/join" method="post"> | 90 | + res.writeHead(200, { 'Contect-Type': 'text/html'}); |
| 89 | - <input type="email" name="email" placeholder="email"><br> | 91 | + res.end(data); |
| 90 | - <input type="password" name="password" placeholder="****"><br> | 92 | + }) |
| 91 | - <input type="name" name="name" placeholder="이름"><br> | ||
| 92 | - <input type="submit" value="회원가입"><br> | ||
| 93 | - </form> | ||
| 94 | - `,'<a href="/login">뒤로가기</a>'); | ||
| 95 | - res.send(page); | ||
| 96 | }); | 93 | }); |
| 97 | 94 | ||
| 98 | -//회원가입 처리 Post : 예제를 위해 간단 저장 방식으로 구현 | 95 | +app.get('/main',function(req, res) { |
| 96 | + fs.readFile('main.html', function(error, data) { | ||
| 97 | + res.writeHead(200, { 'Contect-Type': 'text/html'}); | ||
| 98 | + res.end(data); | ||
| 99 | + }) | ||
| 100 | +}); | ||
| 101 | + | ||
| 102 | +//회원가입 | ||
| 99 | var user = {}; | 103 | var user = {}; |
| 100 | -app.post('/join',(req,res)=>{ | 104 | +app.post('/join',(req,res) =>{ |
| 105 | + | ||
| 101 | user.email = req.body.email; | 106 | user.email = req.body.email; |
| 102 | user.password = req.body.password; | 107 | user.password = req.body.password; |
| 103 | - user.name=req.body.name; | 108 | + user.name = req.body.name; |
| 109 | + | ||
| 104 | //로그인 페이지로 이동 | 110 | //로그인 페이지로 이동 |
| 105 | res.redirect('/login'); | 111 | res.redirect('/login'); |
| 106 | }); | 112 | }); |
| ... | @@ -119,7 +125,9 @@ app.get('/logout',(req,res)=>{ | ... | @@ -119,7 +125,9 @@ app.get('/logout',(req,res)=>{ |
| 119 | 125 | ||
| 120 | 126 | ||
| 121 | //포트 연결 | 127 | //포트 연결 |
| 122 | -app.listen(3000,()=>console.log(`http://localhost:3000`)); | 128 | +app.listen(3000, function() { |
| 129 | + console.log('http://localhost:3000'); | ||
| 130 | +}); | ||
| 123 | 131 | ||
| 124 | 132 | ||
| 125 | //로그인 로그아웃 여부 | 133 | //로그인 로그아웃 여부 |
| ... | @@ -128,22 +136,3 @@ const authInfo = (req)=>{ | ... | @@ -128,22 +136,3 @@ const authInfo = (req)=>{ |
| 128 | return `<a href="/login">login</a>`; | 136 | return `<a href="/login">login</a>`; |
| 129 | } | 137 | } |
| 130 | 138 | ||
| 131 | -//페이지 템플릿 | ||
| 132 | -const getPage = (title, content, auth) =>{ | ||
| 133 | - return ` | ||
| 134 | - <!DOCTYPE html> | ||
| 135 | - <html lang="en"> | ||
| 136 | - <head> | ||
| 137 | - <meta charset="UTF-8"> | ||
| 138 | - <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| 139 | - <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| 140 | - <title>Passport Example</title> | ||
| 141 | - </head> | ||
| 142 | - <body> | ||
| 143 | - ${auth} | ||
| 144 | - <h1>${title}</h1> | ||
| 145 | - <p>${content}</p> | ||
| 146 | - </body> | ||
| 147 | - </html> | ||
| 148 | - `; | ||
| 149 | -} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment