forked from scotch-io/easy-node-authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Chris Sevilleja
committed
Dec 3, 2013
1 parent
0a83358
commit 2ae90a3
Showing
11 changed files
with
253 additions
and
0 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 |
---|---|---|
|
@@ -13,3 +13,6 @@ results | |
|
||
npm-debug.log | ||
node_modules | ||
|
||
config/secretDatabase.js | ||
config/secretAuth.js |
Empty file.
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,66 @@ | ||
// app/routes.js | ||
module.exports = function(app, passport) { | ||
|
||
// ===================================== | ||
// HOME PAGE (with login links) ======== | ||
// ===================================== | ||
app.get('/', function(req, res) { | ||
res.render('index.ejs'); // load the index.ejs file | ||
}); | ||
|
||
// ===================================== | ||
// LOGIN =============================== | ||
// ===================================== | ||
// show the login form | ||
app.get('/login', function(req, res) { | ||
|
||
// render the page and pass in any flash data if it exists | ||
res.render('login.ejs', { message: req.flash('loginMessage') }); | ||
}); | ||
|
||
// process the login form | ||
// app.post('/login', do all our passport stuff here); | ||
|
||
// ===================================== | ||
// SIGNUP ============================== | ||
// ===================================== | ||
// show the signup form | ||
app.get('/signup', function(req, res) { | ||
|
||
// render the page and pass in any flash data if it exists | ||
res.render('signup.ejs', { message: req.flash('loginMessage') }); | ||
}); | ||
|
||
// process the signup form | ||
// app.post('/signup', do all our passport stuff here); | ||
|
||
// ===================================== | ||
// PROFILE SECTION ========================= | ||
// ===================================== | ||
// we will want this protected so you have to be logged in to visit | ||
// we will use route middleware to verify this (the isLoggedIn function) | ||
app.get('/profile', isLoggedIn, function(req, res) { | ||
res.render('profile.ejs', { | ||
user : req.user // get the user out of session and pass to template | ||
}); | ||
}); | ||
|
||
// ===================================== | ||
// LOGOUT ============================== | ||
// ===================================== | ||
app.get('/logout', function(req, res) { | ||
req.logout(); | ||
res.redirect('/'); | ||
}); | ||
}; | ||
|
||
// route middleware to make sure | ||
function isLoggedIn(req, res, next) { | ||
|
||
// if user is authenticated in the session, carry on | ||
if (req.isAuthenticated()) | ||
return next(); | ||
|
||
// if they aren't redirect them to the home page | ||
res.redirect('/'); | ||
} |
Empty file.
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,6 @@ | ||
// config/database.js | ||
module.exports = { | ||
|
||
'url' : 'your-database-here' // looks like mongodb://<user>:<pass>@mongo.onmodulus.net:27017/Mikha4ot | ||
|
||
}; |
Empty file.
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,16 @@ | ||
{ | ||
"name": "node-authentication", | ||
"main": "server.js", | ||
"dependencies" : { | ||
"express" : "~3.4.4", | ||
"ejs" : "~0.8.5", | ||
"mongoose" : "~3.8.1", | ||
"passport" : "~0.1.17", | ||
"passport-local" : "~0.1.6", | ||
"passport-facebook" : "~1.0.2", | ||
"passport-twitter" : "~1.0.2", | ||
"passport-google-oauth" : "~0.1.5", | ||
"connect-flash" : "~0.1.1", | ||
"bcrypt-nodejs" : "latest" | ||
} | ||
} |
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,42 @@ | ||
// server.js | ||
|
||
// set up ====================================================================== | ||
// get all the tools we need | ||
var express = require('express'); | ||
var app = express(); | ||
var port = process.env.PORT || 8080; | ||
var mongoose = require('mongoose'); | ||
var passport = require('passport'); | ||
var flash = require('connect-flash'); | ||
|
||
// var configDB = require('./config/database.js'); | ||
var configDB = require('./config/secretDatabase.js'); | ||
|
||
// configuration =============================================================== | ||
mongoose.connect(configDB.url); // connect to our database | ||
|
||
// require('./config/passport')(passport); // pass passport for configuration | ||
|
||
app.configure(function() { | ||
|
||
// set up our express application | ||
app.use(express.logger('dev')); // log every request to the console | ||
app.use(express.cookieParser()); // read cookies (needed for auth) | ||
app.use(express.bodyParser()); // get information from html forms | ||
|
||
app.set('view engine', 'ejs'); // set up ejs for templating | ||
|
||
// required for passport | ||
app.use(express.session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); // persistent login sessions | ||
app.use(flash()); // use connect-flash for flash messages stored in session | ||
|
||
}); | ||
|
||
// routes ====================================================================== | ||
require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport | ||
|
||
// launch ====================================================================== | ||
app.listen(port); | ||
console.log('The magic happens on port ' + port); |
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,26 @@ | ||
<!-- views/index.ejs --> | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Node Authentication</title> | ||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> <!-- load bootstrap css --> | ||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> <!-- load fontawesome --> | ||
<style> | ||
body { padding-top:80px; } | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
|
||
<div class="jumbotron text-center"> | ||
<h1><span class="fa fa-lock"></span> Node Authentication</h1> | ||
|
||
<p>Login or Register with:</p> | ||
|
||
<a href="/login" class="btn btn-default"><span class="fa fa-user"></span> Local Login</a> | ||
<a href="/signup" class="btn btn-default"><span class="fa fa-user"></span> Local Signup</a> | ||
</div> | ||
|
||
</div> | ||
</body> | ||
</html> |
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,47 @@ | ||
<!-- views/login.ejs --> | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Node Authentication</title> | ||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> <!-- load bootstrap css --> | ||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> <!-- load fontawesome --> | ||
<style> | ||
body { padding-top:80px; } | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
|
||
<div class="col-sm-6 col-sm-offset-3"> | ||
|
||
<h1><span class="fa fa-sign-in"></span> Login</h1> | ||
|
||
<!-- show any messages that come back with authentication --> | ||
<% if (message.length > 0) { %> | ||
<div class="alert alert-danger"><%= message %></div> | ||
<% } %> | ||
|
||
<!-- LOGIN FORM --> | ||
<form action="/login" method="post"> | ||
<div class="form-group"> | ||
<label>Email</label> | ||
<input type="text" class="form-control" name="email"> | ||
</div> | ||
<div class="form-group"> | ||
<label>Password</label> | ||
<input type="password" class="form-control" name="password"> | ||
</div> | ||
|
||
<button type="submit" class="btn btn-warning btn-lg">Login</button> | ||
</form> | ||
|
||
<hr> | ||
|
||
<p>Need an account? <a href="/signup">Signup</a></p> | ||
<p>Or go <a href="/">home</a>.</p> | ||
|
||
</div> | ||
|
||
</div> | ||
</body> | ||
</html> |
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,47 @@ | ||
<!-- views/signup.ejs --> | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Node Authentication</title> | ||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> <!-- load bootstrap css --> | ||
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> <!-- load fontawesome --> | ||
<style> | ||
body { padding-top:80px; } | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
|
||
<div class="col-sm-6 col-sm-offset-3"> | ||
|
||
<h1><span class="fa fa-sign-in"></span> Signup</h1> | ||
|
||
<!-- show any messages that come back with authentication --> | ||
<% if (message.length > 0) { %> | ||
<div class="alert alert-danger"><%= message %></div> | ||
<% } %> | ||
|
||
<!-- LOGIN FORM --> | ||
<form action="/signup" method="post"> | ||
<div class="form-group"> | ||
<label>Email</label> | ||
<input type="text" class="form-control" name="email"> | ||
</div> | ||
<div class="form-group"> | ||
<label>Password</label> | ||
<input type="password" class="form-control" name="password"> | ||
</div> | ||
|
||
<button type="submit" class="btn btn-warning btn-lg">Signup</button> | ||
</form> | ||
|
||
<hr> | ||
|
||
<p>Already have an account? <a href="/login">Login</a></p> | ||
<p>Or go <a href="/">home</a>.</p> | ||
|
||
</div> | ||
|
||
</div> | ||
</body> | ||
</html> |