Node ch.13 _ middleware, strategy 설정
passport에 필요한 middleware를 설정하고 local 인증에 필요한 strategy 추가하기
-
app.js 에 미들웨어 추가
참고) router 보다 위에 위치하도록 해줘야 함
var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var router = require('./router/index'); var passport = require('passport'); var LocalStrategy = require('passport-local').Strategy; var session = require('express-session'); var flash = require('connect-flash'); app.listen(3000, function() { console.log("start@ on port 3000"); }); app.use(express.static('public')) app.use(bodyParser.json()) app.use(bodyParser.urlencoded({extended:true})) app.set('view engine', 'ejs') app.use(session({ secret: 'keyboard cat', resave: false, saveUnitialized: true })) app.use(passport.initialize()) app.use(passport.session()) app.use(flash()) app.use(router)
-
passport 모듈 정의
http://passportjs.org/docs/overview 사이트에서 new LocalStrategy 사용 예제 참조
router/join/index.js
var express = require('express') var app = express() var router = express.Router() var path = require('path') var mysql = require('mysql') var passport = require('passport'); var LocalStrategy = require('passport-local').Strategy; // DATABASE SETTING (Google Cloud SQL) var connection = mysql.createConnection({ host : '35.189.176.97', port : 3306, user : 'root', password : 'root', database : 'jsman' }); connection.connect(); router.get('/', function(req,res){ //res.sendFile(path.join(__dirname, '../../public/join.html')) res.render('join.ejs') }) passport.use('local-join', new LocalStrategy({ usernameField: 'email', //default 속성값 passwordField: 'password', //default 속성값 passReqToCallback: true }, function (req, email, password, done) { console.log('local-join callback called'); } )); // router.post('/', function(req,res){ // var body = req.body; // var email = body.email; // var name = body.name; // var passwd = body.password; // // // var sql = {email : email, name : name, password : passwd}; // var query = connection.query('insert into user set ?', sql, function(err, rows) { // if(err) { throw err;} // console.log("Data inserted!", rows.insertID, name); // res.render('welcome.ejs', {'name' : name, 'id': rows.insertId}) // // // // }) // }) module.exports = router;
-
여기까지의 결과 확인
아직 라우팅 처리를 안해줘서 Cannot Post