Expressã§ãã°ã¤ã³æ©è½ãä½ã
MacOSXä¸ã§ãnvmã使ã£ã¦ã¤ã³ã¹ãã¼ã«ããnode.js v0.4.9ã®ç°å¢ã§ãã
ã¾ãç´°ããè¨å®ã¯ããã«ããã£ããã¨ãä¾ã§ããExpressã®ã»ãã·ã§ã³ãµãã¼ãã«ã¤ãã¦ã¯こちらã«è¨è¼ããã¦ãã¾ãããã®ã¾ã¾è¨å®ããã¨ãããªæãã
app.use(express.cookieParser()); app.use(express.session({ secret: "keyboard cat" }));
ä¸è¨ã®è¨å®ãããã¨ãreq.sessionã¨ããå¤ãå©ç¨ã§ããããã«ãªãã¾ããã«ã¼ãã£ã³ã°ã®ä¾ã
app.get('/', function(req, res){ if (req.session.flg) { res.render('index'); } });
ã¢ã¯ã»ã¹ããã ãã§ã»ãã·ã§ã³idã¯çºè¡ããã¾ãããã»ãã·ã§ã³ãå¼µã£ãã ãã§ã¯ãreq.session.flgã¨ããå¤ã¯ç¡ãã®ã§ãã¼ã¸ã¯è¦ãã¾ãããidã¨passwdã®èªè¨¼ãéã£ãå¾ãªã©ã«è¨å®ãã¦ãããå¿ è¦ãããã¾ãã
var id = 'foo'; var passwd = 'hoge'; app.post('/check', function(req, res) { if(id === req.body.id && passwd === req.body.pw) { req.session.flg = true; res.redirect('/'); } });
ãã³ãã¬ã¼ãã¯ãããªæãã
h1= title p Welcome to #{title}
h1= title p Enter your id & pw form( method='POST', action='/check') input( type='text', name='id' ) input( type='password', name='pw' ) input( type='submit', name='enter', value='login' )
ã¨ããããããã ãã§ããã°ã¤ã³ã½ããã®ã¯ã§ãã¾ããã
ããã©ã«ãã®ã¾ãã¾ã ã¨ã»ãã·ã§ã³æ å ±ã¯...ã¡ã¢ãªä¸? ã«ããã®ããªãã ã¨ãªããä¸å®ãªã®ã§ãã»ãã·ã§ã³ã¹ãã¢ãå©ç¨ãã¾ããããã¥ã¡ã³ãã ã¨Redisãã¹ã¹ã¡ããã¦ãã¾ãããã¢ããªã±ã¼ã¸ã§ã³ã®æ å ±ã¯MongoDBã使ã£ã¦ããã®ã§ãã»ãã·ã§ã³ã¹ãã¢ã«ãMongoDBã使ãããã®ã§ãã使ãã¨ãããªæãã§ãã
var Db = require('mongodb').Db; var Server = require('mongodb').Server; var server_config = new Server( 'localhost', 27017, {auto_reconnect: true, native_parser: true}); var db = new Db( 'authtest', server_config, {} ); var mongoStore = require( 'connect-mongodb'); var auth;
app.use(express.cookieParser()); app.use(express.session({ cookie: {maxAge: 60000 * 20}, secret: 'foo', store: auth = new mongoStore({db: db}) }));
ããã§ãã»ãã·ã§ã³ã®ç½®ãå ´ãMongoDBã«ç½®ãæãããã¾ããã確èªãã¦ã¿ã¾ãããã
$ mongo MongoDB shell version: 1.8.0 connecting to: test > use authtest switched to db authtest > db.sessions.find(); { "_id" : "fWBduKOKEq0rnHc5YWp7UBQ3.uCg+n9B8B0Kx6KxPK7AaixqSrCArGGIgl3EAkiKm4+M", "session" : { "lastAccess" : NumberLong("1310397363006"), "cookie" : { "originalMaxAge" : 1200000, "expires" : "2011-07-11T15:36:03.007Z", "httpOnly" : true, "path" : "/" } }, "expires" : NumberLong("1310398563000") } >
ã»ãã·ã§ã³ã¹ãã¢ãå©ç¨ãã¦èªè¨¼ç¶æ ã確èªããã¨ãã¯ãããªæãã«ãªããã¨æãã¾ãã
app.get('/', function(req, res){ auth.get(req.session.id, function(err, sess) { if(sess && sess.views) { res.render('index', { title: req.session.userid }); } else { res.redirect('/login'); } }); }); app.post('/check', function(req, res) { User.findOne({id: req.body.id}, function(err, docs){ if(docs !== null && docs.passwd === req.body.pw) { req.session.userid = req.body.id; req.session.views = 1; res.redirect('/'); } else { res.render('login', { title: 'login', message: 'login form', error_message: 'password faild' }); } }); });
ãã®ã¾ã¾ã ã¨ãã°ã¤ã³ãã£ã±ãªããªã®ã§ããã°ã¢ã¦ãæ©è½ãä»ãã¾ãããã
app.get('/logout', function(req, res) { auth.destroy(req.session.id, function(err) { req.session.destroy(); console.log('deleted sesstion'); res.redirect('/'); }); });
å ¨ã½ã¼ã¹ã¯こちら