Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Sevilleja committed Dec 3, 2013
1 parent 0a83358 commit 2ae90a3
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ results

npm-debug.log
node_modules

config/secretDatabase.js
config/secretAuth.js
Empty file added app/models/user.js
Empty file.
66 changes: 66 additions & 0 deletions app/routes.js
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 added config/auth.js
Empty file.
6 changes: 6 additions & 0 deletions config/database.js
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 added config/passport.js
Empty file.
16 changes: 16 additions & 0 deletions package.json
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"
}
}
42 changes: 42 additions & 0 deletions server.js
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);
26 changes: 26 additions & 0 deletions views/index.ejs
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>
47 changes: 47 additions & 0 deletions views/login.ejs
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>
47 changes: 47 additions & 0 deletions views/signup.ejs
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>

0 comments on commit 2ae90a3

Please sign in to comment.