Skip to content

Instantly share code, notes, and snippets.

@digitaldrummerj
Created November 20, 2017 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitaldrummerj/473e6e549e8ecf2494c2e58c2ab039b2 to your computer and use it in GitHub Desktop.
Save digitaldrummerj/473e6e549e8ecf2494c2e58c2ab039b2 to your computer and use it in GitHub Desktop.
Sails Snippets for Talk
{
/*
// Place your snippets for JavaScript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
"Print to console": {
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}
*/
"Sails Policy": {
"prefix": "sails-policy",
"body": [
"module.exports = function(req, res, next) {",
"\tif (req.session.${1:check}) {",
"\t\treturn next();",
"\t}",
"",
"\treturn res.forbidden('You are not permitted to perform this action.');",
"}",
""
],
"description": "Sails Policy Template"
},
"Sails Email Validate": {
"prefix": "sails-validate-email",
"body": [
"var EmailAddresses = require('machinepack-emailaddresses');",
"EmailAddresses.validate({",
" string: $1,",
"}).exec({",
" error: function(err){",
" return res.serverError(err);",
" },",
"",
" invalid: function(){",
" return res.badRequest('Does not look like an email address to me!');",
" },",
"",
" success: function(){",
" $0",
" },",
"});"
],
"description": "Template for Email Address Validation using machinepack-emailaddresses"
},
"Sails Password Encrypt":{
"prefix": "sails-password-encrypt",
"body": [
"var Passwords = require('machinepack-passwords');",
"Passwords.encryptPassword({",
" password: $1,",
"}).exec({",
" error: function(err){",
" return res.serverError(err);",
" },",
"",
" success: function(result){",
" $0",
" },",
"});"
],
"description": "encrypt password template using machinepack-passwords"
},
"Sails Route": {
"prefix": "sails-route",
"body": [
"'${1:Verb} /${2:Route}': '${3:Controller}Controller.${4:function}'"
],
"description": "Create a Sails Route"
},
"Sails Policy Config": {
"prefix": "sails-setpolicy",
"body": [
"'$1: ['$2'],",
"$0"
],
"description": "Add a Policy to a Controller Function"
},
"Sails Db Unique Check": {
"prefix": "sails-unique-check",
"body": [
"sails.log.error('error creating user: ', err.invalidAttributes);",
"if (err.invalidAttributes ",
" && err.invalidAttributes.email",
" && err.invalidAttributes.email[0]",
" && err.invalidAttributes.email[0].rule === 'unique') {",
" return res.alreadyInUse(err);",
"}",
"",
"$0"
],
"description": "Check unique db error"
},
"Sails Check Password": {
"prefix": "sails-password-check",
"body": [
"var Passwords = require('machinepack-passwords');",
"Passwords.checkPassword({",
" passwordAttempt: $1",
" encryptedPassword: $2",
"}).exec({",
" error: function (err) {",
" return res.serverError(err);",
" },",
"",
" incorrect: function () {",
" return return res.forbidden('Invalid Login, Please Try Again!');",
" },",
"",
" success: function () {",
" $0",
" }",
"})"
]
},
"Sails Response alreadyInUse.js":{
"prefix": "sails-alreadyInUse",
"body": [
"module.exports = function alreadyInUse (err){",
" // Get access to `res`",
" // (since the arguments are up to us)",
" var res = this.res;",
"",
" if (err.invalidAttributes.email) {",
" return res.send(409, 'Email address is already taken by another user, please try again.');",
" }",
"};",
""
],
"description": "Template for AlreadyInUse"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment