Last active
October 11, 2016 14:24
-
-
Save kkurahar/5551884 to your computer and use it in GitHub Desktop.
Node.js + Express(Jade) + MongoDB(mongose + connect-mongo) でログイン認証機能を実装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var express = require('express'), | |
routes = require('./routes'), | |
http = require('http'), | |
path = require('path'), | |
MongoStore = require('connect-mongo')(express); | |
var app = express(); | |
var domain = (process.env.NODE_ENV === 'production')?'10.0.82.198':'127.0.0.1'; | |
app.configure(function(){ | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.locals.pretty = true; | |
app.use(express.favicon()); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.cookieParser()); | |
app.use(express.session({ | |
secret: 'secret', | |
store: new MongoStore({ | |
db: 'session', | |
host: domain, | |
clear_interval: 60 * 60 | |
}), | |
cookie: { | |
httpOnly: false, | |
maxAge: new Date(Date.now() + 60 * 60 * 1000) | |
} | |
})); | |
app.use(app.router); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
}); | |
app.configure('development', function(){ | |
app.set('port', process.env.PORT || 3000); | |
app.use(express.errorHandler()); | |
}); | |
app.configure('production', function(){ | |
app.set('port', process.env.PORT || 8080); | |
app.use(express.errorHandler()); | |
}); | |
var loginCheck = function(req, res, next) { | |
if(req.session.user) | |
{ | |
next(); | |
} | |
else | |
{ | |
res.redirect('/login'); | |
} | |
}; | |
app.get('/', loginCheck, routes.index); | |
app.get('/login', routes.login); | |
http.createServer(app).listen(app.get('port'), function(){ | |
console.log("Express server listening on port " + app.get('port')); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends layout | |
block content | |
.container | |
h1= title | |
p Welcome to #{title} | |
#container |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var model = require('../models/user.js'), | |
User = model.User; | |
var TITLE = 'My Site'; | |
exports.index = function(req, res){ | |
res.render('index', { title: TITLE }); | |
}; | |
exports.login = function(req, res){ | |
var email = req.query.email; | |
var password = req.query.password; | |
var query = { "email": email, "password": password }; | |
User.find(query, function(err, data) { | |
if(err) | |
{ | |
console.log(err); | |
} | |
if (data == "") | |
{ | |
res.render('login', { title: TITLE }); | |
} | |
else | |
{ | |
req.session.user = email; | |
res.redirect('/'); | |
} | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doctype 5 | |
html | |
head | |
title= title | |
link(rel='stylesheet', href='/stylesheets/bootstrap.min.css') | |
link(rel='stylesheet', href='/stylesheets/bootstrap-responsive.min.css') | |
link(rel='stylesheet', href='/stylesheets/todc-bootstrap.css') | |
script(src="/javascripts/jquery-1.8.2.min.js", type="text/javascript") | |
script(src="/javascripts/jquery-ui-1.8.22.custom.min.js", type="text/javascript") | |
script(src="/javascripts/bootstrap.min.js", type="text/javascript") | |
body | |
.navbar.navbar-inverse.navbar-fixed-top | |
.navbar-inner | |
.container | |
button.btn.btn-navbar(type='button',data-toggle='collapse',data-target='.nav-collapse') | |
span.icon-bar | |
span.icon-bar | |
span.icon-bar | |
.nav-collapse.collapse | |
ul.nav | |
li.active | |
a(href='#') Home | |
li | |
a(href='/user') User | |
li | |
a(href='/admin') Admin | |
block content |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends login_layout | |
block content | |
.container | |
.hero-unit | |
.row-fluid | |
.span9 | |
h2 簡易ログイン画面 | |
h1= title | |
.span3 | |
form(action='/login') | |
input(type='text',name='email',placeholder='Email') | |
input(type='password',name='password',placeholder='Password') | |
label.checkbox.gray ログイン状態を保存 | |
input(type='checkbox') | |
button.btn.btn-primary(type='submit') ログイン |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doctype 5 | |
html | |
head | |
title= title | |
link(rel='stylesheet', href='/stylesheets/bootstrap.min.css') | |
link(rel='stylesheet', href='/stylesheets/bootstrap-responsive.min.css') | |
link(rel='stylesheet', href='/stylesheets/todc-bootstrap.css') | |
script(src="/javascripts/jquery-1.8.2.min.js", type="text/javascript") | |
script(src="/javascripts/jquery-ui-1.8.22.custom.min.js", type="text/javascript") | |
script(src="/javascripts/bootstrap.min.js", type="text/javascript") | |
body | |
.navbar.navbar-inverse.navbar-fixed-top | |
.navbar-inner | |
.container | |
button.btn.btn-navbar(type='button',data-toggle='collapse',data-target='.nav-collapse') | |
span.icon-bar | |
span.icon-bar | |
span.icon-bar | |
.nav-collapse.collapse | |
ul.nav | |
li.active | |
a(href='#') Home | |
block content |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var domain = (process.env.NODE_ENV === 'production')?'10.0.82.198':'localhost'; | |
var mongoose = require('mongoose'); | |
var url = 'mongodb://' + domain + '/user'; | |
var db = mongoose.createConnection(url, function(err, res){ | |
if(err) | |
{ | |
console.log('Error connected: ' + url + ' - ' + err); | |
} | |
else | |
{ | |
console.log('Success connected: ' + url); | |
} | |
}); | |
// Modelの定義 | |
var UserSchema = new mongoose.Schema({ | |
email : String, | |
password : String | |
},{collection: 'info'}); | |
exports.User = db.model('User', UserSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment