ãã®ã¬ãã¹ã³ã¯ã
derbyjsã®äžé£ã®ã¬ãã¹ã³ã®ç¶ç¶-ãªã¢ã¯ãã£ããã«ã¹ã¿ãã¯ãã¬ãŒã ã¯ãŒã¯ã§ãã å¿
èŠãªä»¥åã®ã¬ãã¹ã³ãèªãã§ãã ããïŒ
first ã
second ïŒã
ãã®ã¬ãã¹ã³ã¯2ã€ã®éšåã§æ§æãããŸãã1ã€ç®ã¯ãããŒããŒã¢ããªã±ãŒã·ã§ã³ã®ãµãŒããŒåŽã®æŠèŠã§ããã2ã€ç®ã¯ããœãŒã·ã£ã«ãããã¯ãŒã¯ã䜿çšããããŒããŒã¢ããªã±ãŒã·ã§ã³ã§ã®ç»é²/æ¿èªã®ããã®
derby-auth
ã¢ãžã¥ãŒã«ïŒ
passportjs
ã©ãããŒïŒã®äœ¿çšã§ãã
ããŒã1-ããŒããŒã¢ããªã±ãŒã·ã§ã³ã®ãµãŒããŒã³ãŒã
æºåãã
åã®äŸã§ã¯ã
derby-starter
ã䜿çšããŠããµãŒããŒéšåãšããŠã®æ
å ±ã®èªèãä¿é²ããŸããã ãã®ã¢ãžã¥ãŒã«ã¯ããµãŒããŒèšå®ã®è©³çŽ°ãé ããŸããã ãµãŒããŒã§ã¯ãããŒããŒã¢ããªã±ãŒã·ã§ã³ãæšæºã®expressjsã¢ããªã±ãŒã·ã§ã³ã®äžã«æ§ç¯ãããŠããããšãæãåºãããŠãã ãããç¹ã«ãããŒã¿ããŒã¹ãããã«æ§æãããå¿
èŠãªé«éããããŠã§ã¢ãæ¥ç¶ãããŠããŸãã çŸåšã®äŸã§ã¯ããµãŒããŒåŽãããæ·±ãåŠçããå¿
èŠããããŸã
derby-starter
ã¯äœ¿çšããŸããã
誰ãã
expressjs
粟éããŠããªããã
expressjs
ããç¥ããªãå Žåã¯ãIlya Kantorã®ãã°ããã
ã³ãŒã¹ nodejs
ãå§ãããŸãã
åºæ¬ã¢ããªã±ãŒã·ã§ã³
ä»æ¥ã®äŸã®ããŒã¹ãšããŠã
derby-boilerplate
ãã³ããŒããå¿
èŠããã
derby-boilerplate
ã å®éãããã¯æåã®äŸãšåæ§ã®æå°éã®ã¢ããªã±ãŒã·ã§ã³ã§ããããµãŒããŒéšåã¯
derby-starter
ã§ã¯ãªããããžã§ã¯ãèªäœã«ãããŸãïŒæåŸã®4çªç®ã®ãšã¯ã¹ãã¬ã¹ãããã§äœ¿çšããã
redis
ã¯ãŸã£ãã䜿çšãããŸããïŒ -ä»ã§ã¯ãããªãã§ãå¯èœã§ãã ã³ãã³ããã³ããŒããŸãã
git clone https://github.com/derbyparty/derby-boilerplate.git
ãããžã§ã¯ãã®æ§é ã調ã¹ãŠã¿ãŸããã-åã®äŸãšã¯ç°ãªããããã¯ãã§ã«ããªãæŠéã§ãïŒ
src/ app/ server/ styles/ views/ index.js
app
ãã©ã«ããŒã«ã¯ãããŒããŒã¢ããªã±ãŒã·ã§ã³ããããŸãã æŠç¥çã«ã¯ããµãŒããŒãã©ã«ããŒã«
derby-starter
ãã¢ããã°ããããïŒ
derby-starter
ãå®éã«ã³ããŒããããã«ã³ããŒããŠå°ãã³ãŒãã³ã°ããŸããïŒããã®å
容ããã詳现ã«åæããŸãã
styles
ãš
views
ãã©ã«ããŒãã¹ã¿ã€ã«ãšãã³ãã¬ãŒãã«ããããã index.jsãã¡ã€ã«ã¯ãåã®äŸã®å
容ãšäŒŒãŠããŸã-å
šäœãéå§ããæ°è¡ã§ãã
ããŒããŒã¢ããªã±ãŒã·ã§ã³ã®ãµãŒããŒéšå
ãŸã第äžã«ãderbyã
expressjs
ã¢ããªã±ãŒã·ã§ã³äžã§åäœãããšããäºå®ã«åã³æ³šç®ããŸãããã®ããããããäœã§ãããã
express-middleware
äœããã©ã®ããã«åäœãããããšã¯ã¹ãã¬ã¹ã»ãã·ã§ã³ã®åäœãã«ãŒãã£ã³ã°ãããããªãå Žå
express-middleware
ãããã§åæ¢ããå¿
èŠããããŸãç¥èã®ã®ã£ããã解æ¶ããŸãã
ãµãŒããŒãã©ã«ããŒã«ãããã®ãèŠãŠã¿ãŸãããã
error/ index.js server.js
ãšã©ãŒãã³ãã©ãŒããšã©ãŒãã©ã«ããŒã«é
眮ããŸããããããã«ã¯ããŸããã ããã¯
expressjs-middlware
ã§ãããšããèšããŸãããããã¯ãåæå®ããã
url
ãã³ãã©ãŒããªãå ŽåããŸãã¯ã¢ããªã±ãŒã·ã§ã³ã®æäœäžã«ãšã©ãŒãçºçããå Žåã«ããªã¬ãŒãããŸãã ããªãèªèº«ã®ããã«ãããææ¡ãããå Žåã
index.js
ãã¡ã€ã«ã®äž»ãªç®çã¯ãïŒ
server.js
ãã¡ã€ã«ã§æ§æãããïŒ
express
ãéžæããèšå®ã§æå®ãããããŒãã§ãµãŒããŒãäžããããšã§ãã
ãœãŒã¹ã³ãŒãïŒderby-starterããåãããå®è³ªçã«å€æŽãªãïŒ var derby = require('derby'); exports.run = function (app, options, cb) { options = options || {}; var port = options.port || process.env.PORT || 3000; derby.run(createServer); function createServer() { if (typeof app === 'string') app = require(app); var expressApp = require('./server.js').setup(app, options); var server = require('http').createServer(expressApp); server.listen(port, function (err) { console.log('%d listening. Go to: http://localhost:%d/', process.pid, port); cb && cb(err); }); } }
次ã«ã
derby
è¿œå ããŠexpressjsãèšå®ãã楜ããéšåããããŸãã ãã®ç¹ãç解ããããšã¯ãããŒããŒã®ä»çµã¿ãç解ããããã®éµã§ãã
ç§ã¯4çªç®ã®ãšã¯ã¹ãã¬ã¹ã䜿çšããŸããã3çªç®ãšã¯ããã€ãã®éãããããŸãã äž»ãªããšã¯ãæšæºã®middlwareã
express
ãã£ã¹ããªãã¥ãŒã·ã§ã³ã«å«ãŸããŠããªããããåå¥ã«ã€ã³ã¹ããŒã«ããå¿
èŠãããããšã§ãã
ãããã£ãŠããã®ãã¡ã€ã«ã¯ããŒããŒãªãã®ããã«ãªããŸãã
Server.jsã³ãŒããéåžžã®expressjsã¢ããªã±ãŒã·ã§ã³ãäœæããå ŽåïŒ var express = require('express');
äžèšã§èšãã°ããããã©ã®ããã«æ©èœããããæãåºãããŠãã ããã æåã«ãã¢ãžã¥ãŒã«ãæ¥ç¶ããã次ã«æãéèŠãªããš-ãšã¯ã¹ãã¬ã¹
middleware
æ¥ç¶ïŒ
expressApp.use
çµç±ïŒã å®éããããã®
middlware
ã¯ããµãŒããŒã«å°çããåèŠæ±ã«å¯ŸããŠãç»é²ãããé åºãšåãé åºã§åŒã³åºãããåçŽãªé¢æ°ã§ãã ãããã®å
middleware
ã¯ãåŠçãã§ãŒã³ãå®äºããããšã§èŠæ±ã«å¿çãããïŒæ®ãã®middlwareã«å¶åŸ¡ã転éãããªãïŒãèŠæ±ã䜿çšããŠäžéã¢ã¯ã·ã§ã³ãå®è¡ãïŒ
cookies
解æã
cookies
ã«ããã»ãã·ã§ã³ã®æ±ºå®ãªã©ïŒãå¶åŸ¡ã転éã§ããŸãããã«ãã§ãŒã³ãäžãã
middlware
ã®æ¥ç¶é åº
middlware
éåžžã«éèŠã§ãããã®ã·ãŒã±ã³ã¹ã§ã¯ãåãªã¯ãšã¹ãã«å¯ŸããŠé¢æ°ãåŒã³åºãããŸãã
ãããŠãããã«ããŒããŒãªãã·ã§ã³ããããŸã-ããŒããŒã§ã³ã¡ã³ããããserver.jsã³ãŒã
å°ãæéããããŠãããç解ããŠãã ããã å®éãããã§ã®äž»ãªããšã¯
browserify
䜿çšç®çã§ã-ã¯ã©ã€ã¢ã³ãã«ããããããã³ãã«ããã¯ã©ã€ã¢ã³ãã«æäŸããå¿
èŠããããŸã-ããŒããŒã¢ããªã±ãŒã·ã§ã³ã®ãã¹ãŠã®
javascript
ãã¡ã€ã«ãšãã³ãã¬ãŒããå«ãããŒã¿ãããã¯ïŒcssã¹ã¯ãªããã¯å«ãŸããŸããïŒ ã ã¯ã©ã€ã¢ã³ãã«ãããµã€ãã®ããŒãžã®å®æçãªãªã¯ãšã¹ãã§ã¯ããã³ãã«ããã©ãŠã¶ã«ããã«éä¿¡ãããªããããã³ã¹ããé«ãããããšãç解ããå¿
èŠããããŸãã ã¹ã¿ã€ã«ãšåæããŒã¿ãå«ãã¬ã³ããªã³ã°æžã¿ã®ããŒãžãæšå¥šãããŸãã ããã§ã¯é床ãéåžžã«éèŠãªã®ã§ãããã¯è«ççã§ãã ããã«ããã®ããŒãžèªäœãããã³ãã«ããããŒãããŸã-ã¢ãã¬ã¹ã/ derby /ïŒbandle-nameãã§èŠæ±ãè¡ãããŸãã
è€æ°ã®ãããŒããŒã¢ããªã±ãŒã·ã§ã³ããããŒããŒã®1ã€ã®ãµãŒããŒéšåã«å¯Ÿå¿ã§ããããšã¯æ¢ã«è¿°ã¹ãŸãããå®éãããããã«ç¬èªã®ãã³ãã«ããããŸãã ããã¯è«ççã§ãã ãããã£ãŠãããšãã°ããµã€ãã®ã¯ã©ã€ã¢ã³ãéšåã管çããã«ããåé¢ããŠãäžèŠãªããŒã¿ãå
šå¡ã«è»¢éããªãããã«ããããšãã§ããŸãã ãã®å Žåã¯ãç»é²ããå¿
èŠããããŸãã
expressApp.use(clientApp.scripts(store)); expressApp.use(adminApp.scripts(store));
åãããšãã«ãŒãã®åŠçã«ãåœãŠã¯ãŸããŸãã uã«2ã€ã®ãããŒããŒã¢ããªã±ãŒã·ã§ã³ãããªãå Žåã代ããã«æ¬¡ã®ããã«ãªããŸãã
expressApp.use(app.router());
2ã€ã®ã¢ããªã±ãŒã·ã§ã³ã«ãŒã¿ãŒã®æ¥ç¶ïŒ
expressApp.use(clientApp.router()); expressApp.use(adminApp.router());
ãŸãããããŒããŒã¢ããªã±ãŒã·ã§ã³ãã«ãŒãã®ãã³ãã©ãŒã«å ããŠãããã«
expressjs
ãã³ãã©ãŒãè¿œå ããŠããã©ãã¯ãžã£ãã¯ãè¥ã女æ§ã§å¿
èŠãªRESTFull APIãæŽçã§ããããšãç解ããŠããå¿
èŠããããŸãã ããã¯ãã¹ãŠãããŒããŒã¢ããªã±ãŒã·ã§ã³ã®ã¯ã©ã€ã¢ã³ãã«ãŒã¿ãŒããã¢ããªã±ãŒã·ã§ã³ã§ç®çã®ãã³ãã©ãŒãèŠã€ããããããµãŒããŒã«åŠçã®èŠæ±ãéä¿¡ããã ãã§æ©èœããããã§ãã
次ã®éèŠãªãã€ã³ãã¯ã
browserchannel
ã®æ¥ç¶ã§ããããã¯ã
socket.io
Google
socket.io
ã§ãã å®éãããã¯ãã©ã³ã¹ããŒãã§ããããã«ãããã¢ããªã±ãŒã·ã§ã³ããªã¢ã«ã¿ã€ã ã§ãµãŒããŒãšããŒã¿ãåæããŸãã å
éšã§ã¯ãç¹å®ã®ç¬éã«ã¯ã©ã€ã¢ã³ãã¹ã¯ãªããbrowserchannelããã³ãã«ã«è¿œå ããããã®ã¢ãžã¥ãŒã«ããã®ãªã¯ãšã¹ããåŠçããŸãïŒãã³ã°ããŒãªã³ã°ã«åºã¥ããŠããããããã®å Žåã¯
/channel
ã§ã®åŠçãå¿
èŠã§ãïŒ
ã¯ããããŒããŒã®åºæ¬çãªã»ããã¢ããäŸã§ã¯ã
redis
ã¯äœ¿çšãããªãããšã«æ³šæããŠãã ããã ããŒããŒã®ã¢ããããŒããæè¿ãªãªãŒã¹ãããŸããããä»ã§ã¯
redis
å¿
èŠ
redis
ãŸããïŒæ°Žå¹³ã¹ã±ãŒãªã³ã°ãè¡ãå Žåãã€ãŸãããã©ãŒãã³ã¹ãåäžãããããã«è€æ°ã®ããŒããŒãµãŒããŒãäžåºŠã«å®è¡ããå Žåã«å¿
èŠã§ãïŒã
ãããã£ãŠãçŸæç¹ã§ã¯ããã®æ§é ã䜿çšãããšãéåžžã«æè»ã«ã¢ããªã±ãŒã·ã§ã³ã«äœããè¿œå ã§ããããšãç解ããå¿
èŠããããŸãã äœããã®çš®é¡ã®
expressjs-middlware
ãããŒããŒãã©ã°ã€ã³ãåã
ã®ããŒããŒã¢ããªã±ãŒã·ã§ã³ããšã¯ã¹ãã¬ã¹ãã³ãã©ãŒãªã©ã§ãã ãããã¯ãã¹ãŠäžç·ã«éãã«æ®ããã§ãããã
ããã§ãç¹ã«èªå¯ã®åé¡ãèæ
®ããŠãã»ãã·ã§ã³ã«æ³šæãæãå¿
èŠãããæåŸã®ãã€ã³ãã§ãã ã芧ã®ãšãããããŒããŒã»ãã·ã§ã³ã¯éåžžã«
express
ãªã»ãã·ã§ã³ã䜿çšããŸãã ã€ãŸãããã¹ãŠã¯å€å
žçãªã¹ããŒã ã«åŸã£ãŠè¡ãããŸã
mongodb
ã¯èå¥ã«äœ¿çšããïŒã¯ã©ã€ã¢ã³ãã®ãã©ãŠã¶ã«ä¿åãããŸãïŒãã»ãã·ã§ã³èªäœã¯
sessions
ã³ã¬ã¯ã·ã§ã³ã®
mongodb
ã«ä¿åãã
sessions
ã
createUserId
é¢æ°ãèŠããšïŒ
function createUserId(req, res, next) { var model = req.getModel(); var userId = req.session.userId; if (!userId) userId = req.session.userId = model.id(); model.set('_session.userId', userId); next(); }
userIdã¯ãã®ã»ãã·ã§ã³ããååŸãããååšããªãå Žåã¯idãã©ã³ãã ã«çæããïŒ
model.id()
ïŒããã¹
_session.userId
ïŒãã®ããŒã¿ã¯ã·ãªã¢ã«åãããŠã¯ã©ã€ã¢ã³ãã«éä¿¡ãããŸãïŒãšã»ãã·ã§ã³èªäœã«æ²¿ã£ãŠã¢ãã«ã«æžã蟌ãŸããŸãã
ããã€ãã®ã¯ãšãªãªãã·ã§ã³ãæ³åããŠãã ããã
- ãŠãŒã¶ãŒãæåã«ãµã€ãã蚪åããã¡ã€ã³ããŒãžãèŠæ±ããŸãã-æ°ããCookieããµãŒããŒäžã«åœ¢æãããæ°ããã»ãã·ã§ã³ïŒæ°ããã©ã³ãã
userId
ïŒ-ã¡ã€ã³ããŒãžããã©ãŠã¶ãŒã«æç»ãããã¯ã©ã€ã¢ã³ãã§ã¢ãã«ãèŠããšããã¹ '_session.userId'ã«æ²¿ã£ãŠ-æ°ãã圢æãããIDã衚瀺ãããŸãã ã¢ããªã±ãŒã·ã§ã³ã®ããŒãžãããŸãããŸãïŒãµãŒããŒãžã®ãªã¯ãšã¹ãã¯ãããŸããïŒ-ã¯ã©ã€ã¢ã³ãã¯åžžã«ãã®IDãæã¡ããµãŒããŒãžã®ãªã¯ãšã¹ããè¡ãå Žåãã¯ãããŒãšã»ãã·ã§ã³ã§åžžã«ãããèªèã§ããŸãã - ãŠãŒã¶ãŒã¯1é±éåŸã«ç»é²ããã³ãã°ã€ã³ããŸããã§ããã Cookieã¯åœŒããä¿åãããŸã-圌ã«ã¯åã
userId
ãäžããããŸã-ãã¹ãŠã¯åé¡ãããŸããã - ãŠãŒã¶ãŒãå¥ã®ãã©ãŠã¶ãŒãããã°ã€ã³ãã-ã¹ããã1ãšåã
ïŒãµãŒããŒã®èŠ³ç¹ããïŒç»é²/æ¿èªãã©ã®ããã«éçºãããšããŸãã ã¯ã©ã€ã¢ã³ãããã©ãŠã¶ã«ç»é²ããŒã¿ãå
¥åãããšããŸã-ãã®åŸããŠãŒã¶ãŒã³ã¬ã¯ã·ã§ã³ã«ãšã³ããªãäœæãããµãŒããŒã«ç§»åããå¿
èŠããããŸããIDãäœæããå¿
èŠã¯ãªããæ¢ã«ååšããŸãã
æ¿èªãããã«å°é£ã«ãªããšããµã€ãã«ã¢ã¯ã»ã¹ããã¯ã©ã€ã¢ã³ãã¯ã©ã³ãã IDãåãåããæ¿èªããŒãžã«ç§»åããŸã-ãŠãŒã¶ãŒå/ãã¹ã¯ãŒããå
¥åãããµãŒããŒã«ãã¹ãŠè»¢éããŸãããµãŒããŒã¯ãã»ãã·ã§ã³ãš
_session.userId
userIdãå€æŽããå¿
èŠãããããšãç解ããŸã
ãã®ã³ã¬ã¯ã·ã§ã³ã«äœãä¿åããŸããã æãå¯èœæ§ãé«ãã®ã¯ããŠãŒã¶ãŒåãã¡ãŒã«ãpasswordHashãç¬èªã®çµ±èšæ
å ±ãªã©ã§ãã ç§ãã¡ã®ãµã€ãã«ãœãŒã·ã£ã«çµç±ã®æ¿èªïŒç»é²ïŒãããå Žåã ãããã¯ãŒã¯ããœãŒã·ã£ã«ãããã¯ãŒã¯ã®ããŒã¯ããã«ä¿åãããŸãã ãŸãããã¡ããããŠãŒã¶ãŒèšå®ãããã«ä¿åããŠãããšããã§ãããã
åœç¶ããã®ã³ã¬ã¯ã·ã§ã³ã®äžéšãã¯ã©ã€ã¢ã³ãã§è¡šç€ºã§ããªãããã«ããŠãã ããã ããã§åé¡ã¯ãããŸããã æè¿ããããããããžã§ã¯ã·ã§ã³ãããŒããŒïŒã³ã¬ã¯ã·ã§ã³ã«ãµãã¹ã¯ã©ã€ãããæ©èœãããã¥ã¡ã³ãå
šäœã§ã¯ãªãããã®ç¹å®ã®ãã£ãŒã«ãã®ã¿ã«ãµãã¹ã¯ã©ã€ãããæ©èœïŒã«ç»å ŽããŸããããæ°é±éå-ããã¯äžå¯èœã§ããããããŠããã«ã€ããŠæ€èšãã
derby-auth
ã¢ãžã¥ãŒã«ãŸã ã§ããŸããã ãŠãŒã¶ãŒããŒã¿ã2ã€ã®éšåã«åããŠäœ¿çšããŸã
auths
ç®ã¯
auths
ã³ã¬ã¯ã·ã§ã³ïŒããã§ã¯ãã©ã€ããŒãããŒã¿ã®ã¿ïŒã2ã€ç®ã®
users
ã¯ããã§å
¬éããŒã¿ã§ãã ã ãã
derby-auth
ããŒã2-ããŒããŒã¢ããªã±ãŒã·ã§ã³ã§ã®æ¿èªïŒderby-authã¢ãžã¥ãŒã«ïŒ
ããŒããŒèªèšŒã«ã€ããŠ
çŸæç¹ã§ã¯ã2ã€ã®ããã±ãŒãžããããŸã-ããŒããŒã®æ¿èªã ãããã¯
derby-auth
ãš
derby-passport
ã§ãã ã©ã¡ãã
passportjs
ã©ãããŒã§ãïŒç§ã®æèŠã§ã¯ããã®ãã¡ã®1ã€ã¯å¥ã®ãã©ãŒã¯ã§ãïŒã äž¡æ¹ã®çŸåšã®ããŒãžã§ã³ã¯ããŒããŒã®5çªç®ã®ããŒãžã§ã³ïŒã¢ã«ãã¡ã§6çªç®ã®ããŒãžã§ã³ãæãåºãïŒã§äœ¿çšããããšãç®çãšããŠããã6æ¥ã§ãŸã æŽæ°ãããŠããŸãããã6-keã®ãã¹ãŠã®ãããžã§ã¯ãã§
derby-auth
ã䜿çšããããšã劚ããŸããhtmlãã©ãŒã ãã³ãã¬ãŒãã®ã¿ã§ããããããã«ããŠãã«ã¹ã¿ã ã§ãïŒã
ç§ã
derby-auth
ãéžæããã®ã¯ãããã䜿ã£ãããã§ãã
æ¢ã«èšåããå¥ã®å¶éã¯ããããã®ãããžã§ã¯ãã®äž¡æ¹ã®ãŠãŒã¶ãŒããŒã¿ã2ã€ã®éšåã«åå²ãããããšã§ãã ãããªãã¯ããŒã¿ã¯
users
ã³ã¬ã¯ã·ã§ã³ã«ä¿åãã
auths
ãã©ã€ããŒãããŒã¿ãä¿åããå¿
èŠããã
users
ã æè¿ãããŒããŒã«ã¯ãæ圱ããã€ãŸãã³ã¬ã¯ã·ã§ã³ã®ç¹å®ã®ãã£ãŒã«ãã®ã¿ããµãã¹ã¯ã©ã€ãããæ©äŒããããŸãããããã®ã¢ãžã¥ãŒã«ãããŒããŒã®ç¬¬6ããŒãžã§ã³ã«æŽæ°ããããšãå¶éã¯ãªããªããšæããŸãã
æŽæ°ãããã®èšäºãæžããŠããæ°æ¥åŸãVladimir MakhaevïŒ@vmakhaevïŒã¯derby-authã«åºã¥ããçŽ æŽããã
derby-loginèªèšŒã¢ãžã¥ãŒã«ãéçºããŸããã 0.6æªæºã§ããããžã§ã¯ã·ã§ã³ããµããŒãããç»é²ãå
¥åããã¹ã¯ãŒãã®å€æŽã®ããã®æ¢è£œã®ã³ã³ããŒãã³ããåããŠããŸã-èŠããã«ãããã ãã䜿çšããŸãã
ã¡ãªã¿ã«ãå°ãªããšã
githubã«ã¢ã¹ã¿ãªã¹ã¯ãä»ããŠããŠã©ãžããŒã«ã«æè¬ããããšãå¿ããªãã§ãã ããã 圌ã¯ãšãŠã䟿å©ãªããšãããŸããã
passportjsã«ã€ããŠ
PassportJS
ã¯ã
node.js
ã§ã®æ¿èªçš
middleware
ã§ãã ããã¯ãã¢ããªã±ãŒã·ã§ã³ããã°ã€ã³ãšãã¹ã¯ãŒãã䜿çšããéåžžã®èš±å¯ãšãOauthãOauth 2ãªã©ããµããŒãããã»ãŒ
ãã¹ãŠã®ãµãŒãã¹ã«ããèš±å¯ã®äž¡æ¹ã䜿çšã§ããããã«ããæœè±¡ã¢ãžã¥ãŒã«ã§ãã èªå¯ã ããã§ã¯ãæ¿èªæ¹æ³ãæŠç¥ãšåŒã³ãŸãã ããšãã°ããã°ã€ã³/ãã¹ã¯ãŒãèªèšŒã¯
LocalStrategy
ïŒãããŒã«ã«èªèšŒæŠç¥ãïŒãš
GithubStrategy
ãGitHubèªèšŒã¯
GithubStrategy
ãš
GithubStrategy
ã åæŠç¥ã¯åå¥ã®ã¢ãžã¥ãŒã«ã«é
眮ãããããããã®å
šäœãnode.jsã¢ããªã±ãŒã·ã§ã³ã«æ¥ç¶ãããšæ¬¡ã®ããã«ãªããŸãã
var passport = require('passport'); var LocalStrategy = require('passport-local').Strategy; var GithubStrategy = require('passport-github').Strategy;
ãŸãããœãŒã·ã£ã«ãããã¯ãŒã¯ãä»ããŠæ¿èªæŠç¥ã䜿çšããæ©èœã«ã泚æããå¿
èŠããããŸãã ä»ã®èª°ããç¥ããªãå Žåãã¢ããªã±ãŒã·ã§ã³ããã®ãããªæ¿èªã䜿çšããåã«ããœãŒã·ã£ã«ïŒã¢ããªã±ãŒã·ã§ã³ïŒã«ç»é²ããå¿
èŠããããŸãã ãããã¯ãŒã¯ãš2ã€ã®ãã©ã¡ãŒã¿ãŒãååŸããŸãã
appId
ïŒãŸãã¯
clientId
ã¯ãã®ãœãŒã·ã£ã«ãããã¯ãŒã¯å
ã®ã¢ããªã±ãŒã·ã§ã³ã®äžæã®id-shnikã§ãïŒãšSecretïŒèª°ã«ãèšããªãç§å¯éµãã¢ããªã±ãŒã·ã§ã³ã確èªã§ããããã«äœ¿çšãããŸãïŒç§ãã¡ã¯ç§ãã¡ã§ãïŒã ç»é²æã«ã¯ããããã
redirectURL
ãã€ãŸããœãŒã·ã£ã«ã®URLã¢ãã¬ã¹ãå
¥åããå¿
èŠããããŸãã ãããã¯ãŒã¯ã¯èªèšŒåŸã«ã¯ã©ã€ã¢ã³ãã®ãã©ãŠã¶ããªãã€ã¬ã¯ãããŸãã ç§ã®äŸã§ã¯ãããã¯æ¬¡ã®ããã«ãªããŸãïŒ
localhost ïŒ3000 / auth / github / callback-ããããããã«ã€ããŠã¯åŸã§è©³ãã説æããŸãã
ãµã³ãã«ã¢ããªã±ãŒã·ã§ã³ã§ã¯ã
github
ãä»ããŠæ¿èªã䜿çšããã¢ããªã±ãŒã·ã§ã³ãç»é²ããã®ã¯éåžžã«ç°¡åã§ãïŒä»ã®ãããã€ããŒã«ãšã£ãŠã¯ããã»ã©é£ãããããŸããïŒã ãŸããéåžžã®ãŠãŒã¶ãŒã¢ã«ãŠã³ããå¿
èŠã§ããèšå®ã§ã¢ããªã±ãŒã·ã§ã³ãç°¡åã«äœæã§ããŸãã
誰ã«ãç§ã®ç§å¯ãæããªãã§ãã ããã
derby-authãã¢ããªã±ãŒã·ã§ã³ã«æ¥ç¶ããŸã
ãã®ããããŸããgitãªããžããªãã
derby-auth
çŽæ¥é
眮ããŸããäœããã®çç±ã§ã以åã®
derby
ãã¹ã¿ãŒããŒãžã§ã³ããããŸãã
npm install gitïŒ//github.com/cray0000/derby-auth#0.5 -S
Derby-auth
èªåçã«
passport
ãš
passport-local
ã
passport-github
ãã€ã³ã¹ããŒã«ããŸãïŒ
npm install passport-github -S
次ã«èšå®ã«é²ã¿ãŸãã
https://github.com/lefnire/derby-auth/tree/0.5ã®æ瀺ã«åŸã£ãŠãã¹ãŠãè¡ã
ãŸã ã æ¥ç¶ã¢ã¯ã·ã§ã³ã®ã»ãšãã©ã¯ãµãŒããŒã§å®è¡ãããŸããã€ãŸããsrc / server / server.jsãå¿
èŠã§ãã
ã¹ããã1
derby-authèªäœãæ¥ç¶ããæŠç¥ãèšå®ããŸãïŒéåžžã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãèšå®ã¯èšå®ãã¡ã€ã«ãŸãã¯ç°å¢å€æ°ããååŸããå¿
èŠããããŸãïŒ
ããå€ãã®æŠç¥ãããå Žåããããã®ãã¹ãŠãããã«ãªã¹ããããããšã¯æããã§ãïŒ
ã¹ããã2
ãªãã·ã§ã³ãèšå®ããŸãã
var options = { passport: { failureRedirect: '/login', successRedirect: '/' }, site: { domain: 'http://localhost:3000', name: 'Derby-auth example', email: 'admin@mysite.com' }, smtp: { service: 'Gmail', user: 'zag2art@gmail.com', pass: 'blahblahblah' } }
ååãšããŠããã¹ãŠã¯ååããæããã§ãïŒ
failureRedirect
ã
successRedirect
æ¿èªã«
successRedirect
ããå Žå/æåããå Žåã«ã¯ã©ã€ã¢ã³ãããªãã€ã¬ã¯ããããURLã ãµã€ãã«é¢ããæ
å ±ã¯ãæŠç¥ãéä¿¡ããããã«å¿
èŠã§ãïŒããšãã°ãã¢ããªã±ãŒã·ã§ã³ã®ç»é²æã«å¯Ÿå¿ãããã£ãŒã«ããå
¥åãããŠããªãå ŽåããœãŒã·ã£ã«ãããã¯ãŒã¯ã§äœ¿çšã§ããŸãïŒã ããã¹ã¯ãŒããå¿ããå Žå-ãã¹ã¯ãŒãããªã»ããããŠãç³é¹žçšã®æ°ãããã¹ã¯ãŒããéä¿¡ãããããã«ã¯ãã¡ãŒã«ãã©ã¡ãŒã¿ãå¿
èŠã§ãã
ã¹ããã3
ã¹ãã¬ãŒãžãåæåãã
auth.store(store, false, strategies);
ãã®ã¹ãããã§ã
derby-auth
ã¯ããŒã¿ã«ã¢ã¯ã»ã¹å¶åŸ¡ãè¿œå ããŸãããŠãŒã¶ãŒã¯authsã³ã¬ã¯ã·ã§ã³ããä»ã®äººã®ã¬ã³ãŒããååŸã§ããŸãã-èªåã®ã¢ã«ãŠã³ãã®ã¿ã衚瀺ãããŸãã 2çªç®ã®ãã©ã¡ãŒã¿ãŒã¯çŸåšäœ¿çšãããŠããŸããã
ã¹ããã4
, middleware
body-parser
( â
npm i body-parser -S
):
expressApp.use(require('cookie-parser')()); expressApp.use(session({ secret: process.env.SESSION_SECRET || 'YOUR SECRET HERE', store: new MongoStore({url: mongoUrl}) })); expressApp.use(require('body-parser')())
â
derby-auth middeware
â :
expressApp.use(auth.middleware(strategies, options));
, . â middleware, , -
expressjs
, url. â :
| | ãã©ã¡ãŒã¿ | è¡ãå
|
---|
/login | post | username, password | |
/register | post | username, email, password | |
/auth/:provider | get | | . ãããã¯ãŒã¯ã : /auth/github
|
/auth/:provider/callback | get | | . â derby-auth failureRedirect successRedirect url. : /auth/github/callback â url , . ãããã¯ãŒã¯ã
|
/logout | get | | «» '/' |
/password-reset | post | email | email ( AJAX) |
/password-change | post | uid, oldPassword, newPassword | ( AJAX) |
() â (html / ..), 0.5 . , , . .
Derby-auth
, , id, , :
ãã㊠| 説æ |
---|
_session.loggedIn | â / |
_session.userId | id |
_session.flash.error | â ( /) |
auth.{userId}.local | |
authã{userId}ã{provider} | é¢é£ãããœãŒã·ã£ã«ãéããç»é²ããŒã¿ã ããã |
ãããã¯ãã¹ãŠãã³ãã¬ãŒãã§å©çšã§ããæ¿èªã¹ããŒã¿ã¹ãå€æããããã«äœ¿çšãããŸããå¿çšäŸ
äžè¬ã«ããã¡ãããããžã¥ã¢ã«ã³ã³ããŒãã³ãderby-auth
ãããŒããŒã®6çªç®ã®ããŒãžã§ã³ã«é©åããŠããå Žåãã¢ããªã±ãŒã·ã§ã³ã¯2ã€ã®ã¢ã«ãŠã³ãã§èšè¿°ãããŸããããããªãã§ã¯ããå°é£ã§ããéçºãç°¡çŽ åããããã«ãæ¥ç¶bootstrap
ããŸãããã®ããã«ãsrc / app / index.jsã«å
¥åããŸã app.use(require('d-bootstrap'));
åºæ¥äžãã npm i d-bootstrap -S
.
2 : , "/" , , , , '/login' , . ,
auths
.
-
src/app/index.js
, , , "*", :
var derby = require('derby'); var app = module.exports = derby.createApp('auth', __filename); global.app = app; app.use(require('d-bootstrap')); app.loadViews (__dirname+'/../../views'); app.loadStyles(__dirname+'/../../styles'); app.get('*', function(page, model, params, next){ var user = 'auths.' + model.get('_session.userId'); model.subscribe(user, function(){ model.ref('_page.user', user); next(); }); }); app.get('/', function (page, model){ page.render('home'); }); app.get('/login', function (page, model){ page.render('login'); });
â
layout
c
index.html
,
home.html
login.html
, , (/ , github-), /.
, index.html <import: src="./home"> <import: src="./login"> <Title:> Derby-auth example <Body:> <div class="navbar navbar-inverse" role="navigation"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="/">Derby-auth Example</a> </div> <div class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <view name="nav-link" href="/"></view> <view name="nav-link" href="/login"></view> </ul> </div> </div> </div> {{if _session.flash.error}} <div class="container"> {{each _session.flash.error as #error}} <div class="alert alert-warning">{{#error}}</div> {{/}} </div> {{/}} <view name="{{$render.ns}}"></view> <nav-link: element="nav-link"> <li class="{{if $render.url === @href}}active{{/}}"> <a href="{{@href}}">{{@content}}</a> </li>
â ( alert-). , , :
<view name="{{$render.ns}}"></view>
home.html
,
login.html
, , :
page.render('home')
page.render('login')
,
home.html
:
<index:> <div class="container"> <h1>:</h1> {{if _session.loggedIn}} <p> </p> <h2> :</h2> {{if _page.user.local}} <p> : <b>{{_page.user.local.username}}</b></p> {{else}} <p> </p> {{/}} <h2>GitHub:</h2> {{if _page.user.github}} <p> : <b>{{_page.user.github.username}}</b></p> {{else}} <p> </p> {{/}} <a class="btn btn-danger" href="/logout"></a> {{else}} <p> </p> <a href="/login">/</a> {{/}} </div>
«» â
GET /logout derby-auth
, «sign in»
http://getbootstrap.com/examples/signin/- slyles/index.styl .form-signin { max-width: 330px; padding: 15px; margin: 0 auto; } .form-signin .form-signin-heading, .form-signin .checkbox { margin-bottom: 10px; } .form-signin .checkbox { font-weight: normal; } .form-signin .form-control { position: relative; height: auto; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; padding: 10px; font-size: 16px; } .form-signin .form-control:focus { z-index: 2; } .form-signin input[type="email"] { margin-bottom: -1px; border-bottom-right-radius: 0; border-bottom-left-radius: 0; } .form-signin input[type="password"] { margin-bottom: 10px; border-top-left-radius: 0; border-top-right-radius: 0; }
login.html :
<index:> <view name="signin"></view> <hr/> <view name="signup"></view> <signin:> <div class="container"> <form class="form-signin" role="form" action='/login' method='post'> <h3 class="form-signin-heading"></h3> <input name="username" type="text" class="form-control" placeholder="" required="" autofocus=""> <input name="password" type="password" class="form-control" placeholder="" required=""> <button class="btn btn-lg btn-primary btn-block" type="submit"></button> <br/> <a class="btn btn-lg btn-danger btn-block" href="/auth/github"> GitHub</a> </form> </div> <signup:> <div class="container"> <form class="form-signin" role="form" action='/register' method='post'> <h3 class="form-signin-heading"></h3> <input name="username" type="text" class="form-control" placeholder="" required="" autofocus=""> <input name="email" type="email" class="form-control" placeholder="" required=""> <input name="password" type="password" class="form-control" placeholder="" required=""> <button class="btn btn-lg btn-primary btn-block" type="submit"></button> </form> </div>
-:
, derby-auth . : post /login â derby-auth, username password â . GitHub â get /auth/github â .
. â post /register, : username, email, password.
: npm start â â « », , â « ». â /logout. github-, « github», , , .
Derby-auth , , . ãããã¯ãŒã¯ã , , . , derby-auth .
, . , ( app.model.get() ).
,
derby-auth
( ) â - .
, , , .
github, - , , . â
. , .
PSderbyjs, :
zag2art . â ( ) , .
PPS, 0.6 alpha 7 â .
derbyjs â
github