-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
3,927 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"commonjs": true, | ||
"es6": false, | ||
"node": true | ||
}, | ||
"extends": "google", | ||
"parserOptions": { | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"no-var": [ | ||
0 | ||
], | ||
"brace-style": [ | ||
"error", | ||
"stroustrup" | ||
], | ||
"indent": [ | ||
"error", | ||
4 | ||
], | ||
"comma-dangle": 0, | ||
"max-len": [ | ||
"error", | ||
{ | ||
"code": 80, | ||
"tabWidth": 4, | ||
"ignoreComments": true, | ||
"ignoreUrls": true, | ||
"ignoreStrings": true | ||
} | ||
], | ||
"require-jsdoc": 0, | ||
"linebreak-style": 0 | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
# start-node-pro | ||
Start Node + webpack + es6 support etc | ||
### This repository is a fork of [repository](https://github.com/laohanme/start-node) by [LaoHan](https://github.com/laohanme/) | ||
|
||
### start-node-pro | ||
Start Node + webpack + es6 support etc | ||
|
||
### Run this script | ||
``` | ||
npm start | ||
``` | ||
|
||
### Serve this script at server | ||
``` | ||
npm serve | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.users = require('./users'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var records = [ | ||
{ | ||
id: 1, | ||
username: 'colvefy', | ||
password: 'password', | ||
displayName: 'Colvefy', | ||
emails: '[email protected]' | ||
}, | ||
{ | ||
id: 2, | ||
username: 'jorden', | ||
password: 'password', | ||
displayName: 'JordenLCH', | ||
emails: '[email protected]' | ||
} | ||
]; | ||
|
||
exports.findById = function(id, cb) { | ||
process.nextTick(function() { | ||
var idx = id - 1; | ||
if (records[idx]) { | ||
cb(null, records[idx]); | ||
} | ||
else { | ||
cb(new Error('User ' + id + ' does not exist')); | ||
} | ||
}); | ||
}; | ||
|
||
exports.findByUsername = function(username, cb) { | ||
process.nextTick(function() { | ||
for (var i = 0, len = records.length; i < len; i++) { | ||
var record = records[i]; | ||
if (record.username === username) { | ||
return cb(null, record); | ||
} | ||
} | ||
return cb(null, null); | ||
}); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const express = require('express'); | ||
const favicon = require('serve-favicon'); | ||
const urlMain = require('./routes/main'); | ||
const path = require('path'); | ||
const bodyParser = require('body-parser'); | ||
const cookieParser = require('cookie-parser'); | ||
|
||
app = express(); | ||
app.set('view engine', 'pug'); | ||
|
||
// templates | ||
app.set('views', './templates'); | ||
|
||
// static | ||
app.use('/static', express.static('static')); | ||
|
||
// Middleware to parse POST request & cookies | ||
// Must include if you want to parse POST request | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({extended: false})); | ||
app.use(cookieParser()); | ||
|
||
// use routes from routes/main.js | ||
// app.use(urlMain); | ||
|
||
app.use(favicon(path.join(__dirname, 'static', 'favicon', 'favicon.png'))); | ||
var passport = require('passport'); | ||
var Strategy = require('passport-local').Strategy; | ||
var db = require('./db'); | ||
|
||
// Configure the local strategy for use by Passport. | ||
// | ||
// The local strategy require a `verify` function which receives the credentials | ||
// (`username` and `password`) submitted by the user. The function must verify | ||
// that the password is correct and then invoke `cb` with a user object, which | ||
// will be set at `req.user` in route handlers after authentication. | ||
passport.use(new Strategy( | ||
function(username, password, cb) { | ||
db.users.findByUsername(username, function(err, user) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (!user) { | ||
return cb(null, false); | ||
} | ||
if (user.password !== password) { | ||
return cb(null, false); | ||
} | ||
return cb(null, user); | ||
}); | ||
})); | ||
|
||
// Configure Passport authenticated session persistence. | ||
// | ||
// In order to restore authentication state across HTTP requests, Passport needs | ||
// to serialize users into and deserialize users out of the session. The | ||
// typical implementation of this is as simple as supplying the user ID when | ||
// serializing, and querying the user record by ID from the database when | ||
// deserializing. | ||
passport.serializeUser(function(user, cb) { | ||
cb(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser(function(id, cb) { | ||
db.users.findById(id, function(err, user) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
cb(null, user); | ||
}); | ||
}); | ||
app.use(require('express-session')( | ||
{secret: 'keyboard cat', resave: false, saveUninitialized: false})); | ||
|
||
// Initialize Passport and restore authentication state, if any, from the | ||
// session. | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
app.get('/', | ||
function(req, res) { | ||
res.render('home', {user: req.user}); | ||
}); | ||
|
||
app.get('/login', | ||
function(req, res) { | ||
res.render('login'); | ||
}); | ||
|
||
app.post('/login', | ||
passport.authenticate('local', {failureRedirect: '/login'}), | ||
function(req, res) { | ||
res.redirect('/'); | ||
}); | ||
|
||
app.get('/logout', | ||
function(req, res) { | ||
req.logout(); | ||
res.redirect('/'); | ||
}); | ||
|
||
app.get('/profile', | ||
require('connect-ensure-login').ensureLoggedIn(), | ||
function(req, res) { | ||
res.render('profile', {user: req.user}); | ||
}); | ||
|
||
const portNumber = 3000; | ||
app.listen(portNumber, function() { | ||
console.log('listening on port ' + portNumber + '!'); | ||
console.log('http:https://localhost:' + portNumber); | ||
}); |
Oops, something went wrong.