-
Notifications
You must be signed in to change notification settings - Fork 108
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
trbabalh
committed
Oct 5, 2014
1 parent
2d7ccae
commit 63295a4
Showing
5 changed files
with
163 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
node_modules | ||
dist | ||
.c9 | ||
.tmp | ||
.sass-cache | ||
bower_components | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
*.idea | ||
*.idea/ | ||
.idea/ | ||
.bower-cache/* | ||
.bower-registry/* | ||
public/bower/* | ||
config/runtime.json | ||
pids | ||
logs | ||
results | ||
npm-debug.log |
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 @@ | ||
web: node server.js |
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,10 @@ | ||
var mongoose = require('mongoose'); | ||
var Schema = mongoose.Schema; | ||
|
||
var UserSchema = new Schema({ | ||
email: String, | ||
password: String, | ||
token: String | ||
}); | ||
|
||
module.exports = mongoose.model('User', UserSchema); |
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,15 @@ | ||
{ | ||
"name": "angular-restful-auth", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"express": "4.x", | ||
"body-parser": "~1.0.0", | ||
"morgan": "latest", | ||
"mongoose": "3.8.8", | ||
"express-jwt": "0.2.1", | ||
"jsonwebtoken": "0.4.0" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Required Modules | ||
var express = require("express"); | ||
var morgan = require("morgan"); | ||
var bodyParser = require("body-parser"); | ||
var jwt = require("jsonwebtoken"); | ||
var mongoose = require("mongoose"); | ||
var app = express(); | ||
|
||
var port = process.env.PORT || 3001; | ||
var User = require('./models/User'); | ||
|
||
// Connect to DB | ||
mongoose.connect(process.env.MONGO_URL); | ||
|
||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(bodyParser.json()); | ||
app.use(morgan("dev")); | ||
app.use(function(req, res, next) { | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST'); | ||
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization'); | ||
next(); | ||
}); | ||
|
||
|
||
|
||
app.post('/authenticate', function(req, res) { | ||
User.findOne({email: req.body.email, password: req.body.password}, function(err, user) { | ||
if (err) { | ||
res.json({ | ||
type: false, | ||
data: "Error occured: " + err | ||
}); | ||
} else { | ||
res.json({ | ||
type: true, | ||
data: user, | ||
token: user.token | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
|
||
app.post('/signin', function(req, res) { | ||
User.findOne({email: req.body.email, password: req.body.password}, function(err, user) { | ||
if (err) { | ||
res.json({ | ||
type: false, | ||
data: "Error occured: " + err | ||
}); | ||
} else { | ||
if (user) { | ||
res.json({ | ||
type: false, | ||
data: "User already exists!" | ||
}); | ||
} else { | ||
var userModel = new User(); | ||
userModel.email = req.body.email; | ||
userModel.password = req.body.password; | ||
userModel.save(function(err, user) { | ||
user.token = jwt.sign(user, process.env.JWT_SECRET); | ||
user.save(function(err, user1) { | ||
res.json({ | ||
type: true, | ||
data: user1, | ||
token: user1.token | ||
}); | ||
}); | ||
}) | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
app.get('/me/:token', ensureAuthorized, function(req, res) { | ||
User.findOne({token: req.params.token}, function(err, user) { | ||
if (err) { | ||
res.json({ | ||
type: false, | ||
data: "Error occured: " + err | ||
}); | ||
} else { | ||
res.json({ | ||
type: true, | ||
data: user | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
function ensureAuthorized(req, res, next) { | ||
var bearerToken; | ||
var bearerHeader = req.headers["authorization"]; | ||
if (typeof bearerHeader !== 'undefined') { | ||
var bearer = bearerHeader.split(" "); | ||
bearerToken = bearer[1]; | ||
next(); | ||
} else { | ||
res.send(403); | ||
} | ||
} | ||
|
||
process.on('uncaughtException', function(err) { | ||
console.log(err); | ||
}); | ||
|
||
// Start Server | ||
app.listen(port, function () { | ||
console.log( "Express server listening on port " + port); | ||
}); |