Skip to content

Instantly share code, notes, and snippets.

@thelebster
Created April 7, 2014 04:00
Show Gist options
  • Select an option

  • Save thelebster/10014645 to your computer and use it in GitHub Desktop.

Select an option

Save thelebster/10014645 to your computer and use it in GitHub Desktop.
var User = require('../app/models/user');
var Auth = require('./middlewares/api_auth.js');
module.exports = function(app, passport){
app.get('/api/auth/fb', function(req, res, next) {
passport.authenticate('facebook-token', function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
return res.send({ success : false, message : 'authentication failed' });
}
return res.send({
success : true,
message : 'authentication succeeded',
user: user
});
})(req, res, next);
});
app.post("/api/users", function (req, res, next) {
});
app.post("/api/signup", function (req, res, next) {
User.count({
email: req.body.email
}, function (err, count) {
if (count === 0) {
//next();
res.send({ success : false, message : 'user do not exist' });
} else {
res.send({ success : false, message : 'user already exist', user: count });
}
});
User.signup(req.body.email, req.body.password, function(err, user){
if(err) return next(err);
if(err) throw err;
req.login(user, function(err){
if(err) return next(err);
return res.redirect("profile");
});
});
});
/**
* Delete the user with the specified id
*/
app.delete('/api/users/:user_id', function(req, res) {
User.remove({
_id : req.params.user_id
}, function(err, user) {
if (err)
res.send(err);
// get and return all the users after you create another
User.find(function(err, users) {
if (err)
res.send(err)
res.json(users);
});
});
});
/**
* Retrieve all users
*/
app.get("/api/users", function (req, res, next){
User.find(function(err, users) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(users);
});
});
/**
* Retrieve the user with the specified id
*/
app.get("/api/users/:email", function (req, res, next){
User.findOne({email : req.params.email}, function(err, user) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send({error: err});
if (! user) {
return res.send({ success : false, message : 'user not found' });
}
return res.send({
success : true,
message : 'user found',
user: user
});
});
});
app.post("/api/test", function (req, res, next) {
return res.json({email: req.body.email, password: req.body.password});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment