Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
unicodeveloper committed Oct 4, 2016
0 parents commit e3a4068
Show file tree
Hide file tree
Showing 85 changed files with 5,338 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
118 changes: 118 additions & 0 deletions .gitignore
@@ -0,0 +1,118 @@
################################################
############### .gitignore ##################
################################################
#
# This file is only relevant if you are using git.
#
# Files which match the splat patterns below will
# be ignored by git. This keeps random crap and
# sensitive credentials from being uploaded to
# your repository. It allows you to configure your
# app for your machine without accidentally
# committing settings which will smash the local
# settings of other developers on your team.
#
# Some reasonable defaults are included below,
# but, of course, you should modify/extend/prune
# to fit your needs!
################################################




################################################
# Local Configuration
#
# Explicitly ignore files which contain:
#
# 1. Sensitive information you'd rather not push to
# your git repository.
# e.g., your personal API keys or passwords.
#
# 2. Environment-specific configuration
# Basically, anything that would be annoying
# to have to change every time you do a
# `git pull`
# e.g., your local development database, or
# the S3 bucket you're using for file uploads
# development.
#
################################################

config/local.js





################################################
# Dependencies
#
# When releasing a production app, you may
# consider including your node_modules and
# bower_components directory in your git repo,
# but during development, its best to exclude it,
# since different developers may be working on
# different kernels, where dependencies would
# need to be recompiled anyway.
#
# More on that here about node_modules dir:
# http://www.futurealoof.com/posts/nodemodules-in-git.html
# (credit Mikeal Rogers, @mikeal)
#
# About bower_components dir, you can see this:
# http://addyosmani.com/blog/checking-in-front-end-dependencies/
# (credit Addy Osmani, @addyosmani)
#
################################################

node_modules
bower_components




################################################
# Sails.js / Waterline / Grunt
#
# Files generated by Sails and Grunt, or related
# tasks and adapters.
################################################
.tmp
dump.rdb





################################################
# Node.js / NPM
#
# Common files generated by Node, NPM, and the
# related ecosystem.
################################################
lib-cov
*.seed
*.log
*.out
*.pid
npm-debug.log





################################################
# Miscellaneous
#
# Common files generated by text editors,
# operating systems, file systems, etc.
################################################

*~
*#
.DS_STORE
.netbeans
nbproject
.idea
.node_history
5 changes: 5 additions & 0 deletions .sailsrc
@@ -0,0 +1,5 @@
{
"generators": {
"modules": {}
}
}
82 changes: 82 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,82 @@
/**
* Gruntfile
*
* This Node script is executed when you run `grunt` or `sails lift`.
* It's purpose is to load the Grunt tasks in your project's `tasks`
* folder, and allow you to add and remove tasks as you see fit.
* For more information on how this works, check out the `README.md`
* file that was generated in your `tasks` folder.
*
* WARNING:
* Unless you know what you're doing, you shouldn't change this file.
* Check out the `tasks` directory instead.
*/

module.exports = function(grunt) {


// Load the include-all library in order to require all of our grunt
// configurations and task registrations dynamically.
var includeAll;
try {
includeAll = require('include-all');
} catch (e0) {
try {
includeAll = require('sails/node_modules/include-all');
} catch (e1) {
console.error('Could not find `include-all` module.');
console.error('Skipping grunt tasks...');
console.error('To fix this, please run:');
console.error('npm install include-all --save`');
console.error();

grunt.registerTask('default', []);
return;
}
}


/**
* Loads Grunt configuration modules from the specified
* relative path. These modules should export a function
* that, when run, should either load/configure or register
* a Grunt task.
*/
function loadTasks(relPath) {
return includeAll({
dirname: require('path').resolve(__dirname, relPath),
filter: /(.+)\.js$/,
excludeDirs: /^\.(git|svn)$/
}) || {};
}

/**
* Invokes the function from a Grunt configuration module with
* a single argument - the `grunt` object.
*/
function invokeConfigFn(tasks) {
for (var taskName in tasks) {
if (tasks.hasOwnProperty(taskName)) {
tasks[taskName](grunt);
}
}
}



// Load task functions
var taskConfigurations = loadTasks('./tasks/config'),
registerDefinitions = loadTasks('./tasks/register');

// (ensure that a default task exists)
if (!registerDefinitions.default) {
registerDefinitions.default = function(grunt) {
grunt.registerTask('default', []);
};
}

// Run task functions to configure Grunt.
invokeConfigFn(taskConfigurations);
invokeConfigFn(registerDefinitions);

};
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
# project

a [Sails](http://sailsjs.org) application
Empty file added api/controllers/.gitkeep
Empty file.
17 changes: 17 additions & 0 deletions api/controllers/QuoteController.js
@@ -0,0 +1,17 @@
/**
* QuoteController
*
* @description :: Server-side logic for managing users
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

module.exports = {
getQuote: function(req, res) {
return res.json({ quote: quoter.getRandomOne() });
},

getProtectedQuote: function(req, res) {
return res.json({ quote: quoter.getRandomOne() });
},
};

64 changes: 64 additions & 0 deletions api/controllers/UserController.js
@@ -0,0 +1,64 @@
/**
* UserController
*
* @description :: Server-side logic for managing users
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

var jwt = require('jsonwebtoken');

module.exports = {
signup: function(req, res) {
var email = req.body.email;
var password = req.body.password;
var username = req.body.username;

if (!email || !password || !username) {
return res.json(401, {err: 'Please fill in all the fields'});
}

User.create({username: req.body.username, email: req.body.email, password: req.body.password}).exec(function(err, user) {
if (err) {
res.json(err.status, {err: err});
return;
}
if (user) {
res.json({ success: true, message: "User Registered successfully. Please, go ahead and login" });
}
});
},

login: function(req, res) {
var email = req.body.email;
var password = req.body.password;
var user = {
sub: 'User detail',
email: email
};

var token = jwt.sign(user, sails.config.sessionSecret, { expiresIn: "24h" });

if (!email || !password) {
return res.json(401, {err: 'username and password required'});
}

User.findOneByEmail(email, function(err, user) {
if (!user) {
return res.json(401, {err: 'Invalid email. User does not exist'});
}

User.validPassword(password, user, function(err, valid) {
if (err) {
return res.json(403, {err: 'forbidden'});
}

if (!valid) {
return res.json(401, {err: 'invalid username or password'});
} else {
res.json({success: true, token: token});
}
});
})
}
};

13 changes: 13 additions & 0 deletions api/controllers/WelcomeController.js
@@ -0,0 +1,13 @@
/**
* UserController
*
* @description :: Server-side logic for managing users
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

module.exports = {
welcome: function(req, res) {
res.json({ message: "Welcome to the API of your life" });
}
};

Empty file added api/models/.gitkeep
Empty file.
53 changes: 53 additions & 0 deletions api/models/User.js
@@ -0,0 +1,53 @@
/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/
var bcrypt = require('bcrypt');

module.exports = {

schema: true,

attributes: {
username: {
type: 'string',
required: true
},
email: {
type: 'email',
required: true,
unique: true
},
password: {
type: 'string',
required: true
}
},

beforeCreate: function(values, next) {
// Hash password
bcrypt.hash(values.password, 10, function(err, hash) {
if (err) return next(err);
values.password = hash;
//calling next() with an argument returns an error. Useful for canceling the entire operation if some criteria fails.
next();
});
},

validPassword: function(password, user, cb) {
bcrypt.compare(password, user.password, function(err, match) {
if (err) cb(err);

if (match) {
cb(null, true);
} else {
cb(err);
}
});
}


};

0 comments on commit e3a4068

Please sign in to comment.