Skip to content

Instantly share code, notes, and snippets.

@derekbasch
Last active April 15, 2016 23:56
Show Gist options
  • Save derekbasch/d496dc3e92dea60047a6 to your computer and use it in GitHub Desktop.
Save derekbasch/d496dc3e92dea60047a6 to your computer and use it in GitHub Desktop.
A sails-generate-auth, passport-local and passport-facebook example. You shall not pass!
sails-generate-auth-example
see README.md

##sails-generate-auth-example

A sails-generate-auth, passport-local and passport-facebook example.

You cannot pass! I am a servant of the Secret Fire, wielder of the Flame of Anor. The dark fire will not avail you, Flame of Udun! Go back to the shadow. You shall not pass! - Gandalf the Grey

References:

Sails-generate-auth

Sails (v0.8.82) + passport + passport-local Rough Example

Bug Fixes:

Generate errors about locals/en.json existing

Why Does npm install say I have unmet dependencies?

Error due to en.json already exists

api/controllers/AuthController.js
login: function (req, res) {
var strategies = sails.config.passport
, providers = {};
// Get a list of available providers for use in your templates.
Object.keys(strategies).forEach(function (key) {
if (key === 'local') return;
providers[key] = {
name : strategies[key].name
, slug : key
};
});
// Render the `auth/login.ext` view
res.view({
providers : providers
, user: req.user
, errors : req.flash('error')
});
},
.....
callback: function (req, res) {
passport.callback(req, res, function (err, user) {
req.login(user, function (err) {
// If an error was thrown, redirect the user to the login which should
// take care of rendering the error messages.
if (err) {
res.redirect(req.param('action') === 'register' ? '/register' : '/login');
}
// Upon successful login, send the user to the account page were req.user
// will available.
else {
res.redirect('/account');
}
});
});
},
/**
* Create a homepage
* @param {Object} req
* @param {Object} res
*/
index: function (req, res) {
res.view({
errors: req.flash('error')
});
}
views/auth/index.ejs
<!-- Home page -->
<link href='http://sailsjs.org/styles/fonts.css' rel='stylesheet'/>
<link href='styles/main.css' rel='stylesheet'/>
<div class="container clearfix">
<div class="main">
<h1 id="main-title">Hi <%= user.username %>,</h1>
<p>You've successfully logged in.</p>
<a class="btn" href="/logout">Logout</a>
<h2>User Model</h2>
<table class="table">
<% for(key in user.toJSON() ) { %>
<tr>
<td><%= key %>: </td>
<td class="boxy"><%= user[key] %></td>
</tr>
<% } %>
</table>
<div class="delete">
<div class="sprite">You're looking at: <span class="boxy">views/auth/index.ejs</span></div>
</div>
</div>
</div>
/views/layout.ejs
<body>
<div class="top-bar">
<div class="container">
<img class="logo" src="http://sailsjs.org/images/sails-logo.jpg" />
<ul id="navlist">
<% if (!user) { %>
<li><a href="/login">Log In</a></li>
<li><a href="/register">Register</a></li>
<% } else { %>
<li><a href="/account">Account</a></li>
<li><a href="/logout">Log Out</a></li>
<% } %>
</ul>
</div>
</div>
<% if (errors && !String(errors).match(/^\s*$/)) { %>
<p class="error"><%= errors %></p>
<% } %>
<%- body %>
<!-- Login page -->
<link href='http://sailsjs.org/styles/fonts.css' rel='stylesheet'/>
<link href='../styles/main.css' rel='stylesheet'/>
<div class="container clearfix">
<div class="main">
<h1 id="main-title">Please sign in</h1>
<form role="form" action="/auth/local" method="post" id="authenticate-form">
<ul>
<li>
<label for="username-email">Username or Email:</label>
<input type="text" name="identifier" placeholder="Username or Email" name="username-email"/>
</li>
<li>
<label for="password">Password:</label>
<input type="password" name="password" placeholder="Password" name="password"/>
</li>
<li>
<button type="submit" class="btn">Sign in</button>
</li>
<li>
<a href="/auth/facebook/" class="btn">with Facebook</a>
</li>
<!-- <li>
<a href="/auth/github/" class="btn">with GitHub</a>
</li>
<li>
<a href="/auth/google/" class="btn">with Google+</a>
</li> -->
</ul>
</form>
<div class="delete">
<div class="sprite">You're looking at: <span class="boxy">views/auth/login.ejs</span></div>
</div>
</div>
</div>
html, body {
text-align: left;
font-size: 1em
}
html, body, img, form, textarea, input, fieldset, div, p, div, ul, li, ol, dl, dt, dd, h1, h2, h3, h4, h5, h6, pre, code {
margin: 0;
padding: 0
}
ul, li {
list-style: none
}
img {
display: block
}
a img {
border: 0
}
a {
text-decoration: none;
font-weight: normal;
font-family: inherit
}
*:active, *:focus {
outline: 0;
-moz-outline-style: none
}
h1, h2, h3, h4, h5, h6 {
font-weight: normal
}
div.clear {
clear: both
}
.clearfix:after {
clear: both;
content: ".";
display: block;
font-size: 0;
height: 0;
line-height: 0;
visibility: hidden
}
body {
font-family: "Open Sans", Arial, sans-serif;
font-weight: 300;
font-size: 15px
}
h1 {
color: #0c8da0;
font-size: 2em;
font-weight: 300
}
h2 {
font-size: 1.5em;
font-weight: 300;
margin-top: 4%
}
h3 {
font-size: 1.25em;
font-weight: 300;
font-style: italic;
margin-bottom: 5px
}
h4 {
color: #0c8da0;
font-weight: 300;
font-size: 1.5em
}
span {
font-weight: 700
}
ul {
margin-top: 5%;
margin-bottom: 5%
}
a {
text-decoration: none;
color: inherit
}
p {
margin-bottom: 7px;
font-size: .9em
}
.container {
max-width: 997px;
margin: 0 auto;
padding: 0 4%
}
.sprite {
font-weight: normal;
background: url(http://sailsjs.org/images/newapp.sprite.png) no-repeat
}
.top-bar {
position: relative;
padding-top: 10px;
background-color: #001c20;
height: 55px
}
.main {
float: left;
width: 610px;
height: 555px;
margin-top: 50px
}
.delete {
margin-top: 5%;
height: 52px;
background: #e3f0f1;
border: 1px solid #118798;
color: #118798;
clear: both
}
.delete .sprite {
margin-top: 10px;
margin-bottom: 10px;
margin-left: 9%;
padding-left: 42px;
padding-top: 7px;
height: 25px;
background-position: 0 -126px
}
.delete a {
color: #0c8da0;
font-weight: bold;
padding-left: 1%
}
.boxy {
font-family: Courier, "Courier New", sans-serif;
background-color: #e4edec;
border: 1px solid #d0d6d6;
padding-left: 5px;
padding-right: 5px;
padding-top: 2px;
padding-bottom: 2px;
font-weight: 100
}
.btn {
display: inline-block;
background: #001c20;
color: #fff;
border-radius: .3em;
padding: .6em 1.2em;
line-height: 1.2;
border-radius: 6px;
margin: 1em .1em;
}
#navlist {
padding: 1em;
margin: 0em;
float: right;
}
#navlist li {
display:inline;
}
#navlist li a
{
padding: 0.2em 1em;
color: white;
text-decoration: none;
float: left;
}
.logo {
float: left;
}
#authenticate-form ul {
padding: 0em;
margin-bottom: .5em;
}
#authenticate-form li {
padding: .5em 0em;
}
#authenticate-form input {
height: .75em;
padding: 1em 0em;
width: 300px;
}
#authenticate-form label {
width:150px;
margin-top: 3px;
display:inline-block;
float:left;
padding:3px;
}
#authenticate-form button {
margin-left:156px;
border: none;
font-weight: normal;
font-family: inherit;
font-size: 15px;
}
#authenticate-form a {
margin: 0px 0px 0px 156px;
border: none;
width: 262px;
text-align: center;
}
.error {
margin-top: 1em;
padding: .5em;
background:#ffecec;
border:1px solid #f5aca6;
}
config/passport.js
module.exports.passport = {
local: {
strategy: require('passport-local').Strategy
},
facebook: {
name: 'Facebook',
protocol: 'oauth2',
strategy: require('passport-facebook').Strategy,
options: {
clientID: 'your-client-id',
clientSecret: 'your-client-secret'
}
}
config/policies.js
module.exports.policies = {
// Default policy for all controllers and actions
// (`true` allows public access)
// '*': true,
'*': [ 'passport' ]
/views/auth/register
<!-- Register page -->
<link href='http://sailsjs.org/styles/fonts.css' rel='stylesheet'/>
<link href='../styles/main.css' rel='stylesheet'/>
<div class="container clearfix">
<div class="main">
<h1 id="main-title">Please Register</h1>
<form role="form" action="/auth/local/register" method="post" id="authenticate-form">
<ul>
<li>
<label for="username">Username:</label>
<input type="text" name="username" placeholder="Username" name="username"/>
</li>
<li>
<label for="email">Email:</label>
<input type="text" name="email" placeholder="Email" name="email"/>
</li>
<li>
<label for="password">Password:</label>
<input type="password" name="password" placeholder="Password" name="password"/>
</li>
<li>
<button type="submit" class="btn">Sign Up</button>
</li>
</ul>
</form>
<div class="delete">
<div class="sprite">You're looking at: <span class="boxy">views/auth/register.ejs</span></div>
</div>
</div>
</div>
module.exports.routes = {
// Make the view located at `views/homepage.ejs` (or `views/homepage.jade`, etc. depending on your
// default view engine) your home page.
//
// (Alternatively, remove this and add an `index.html` file in your `assets` directory)
'/' : {
controller : 'auth',
action : 'login'
},
'/account' : {
controller : 'auth',
action : 'index'
},
// Custom routes here...
'get /login': 'AuthController.login',
'get /logout': 'AuthController.logout',
'get /register': 'AuthController.register',
'post /auth/local': 'AuthController.callback',
'post /auth/local/:action': 'AuthController.callback',
'get /auth/:provider': 'AuthController.provider',
'get /auth/:provider/callback': 'AuthController.callback',
// If a request to a URL doesn't match any of the custom routes above,
// it is matched against Sails route blueprints. See `config/blueprints.js`
// for configuration options and examples.
};
Derek@GETY ~/Desktop/workspace
$ sails new sails-auth-example
info: Created a new Sails app `sails-auth-example`!
Derek@GETY ~/Desktop/workspace
$ cd sails-auth-example/
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ npm install --save
npm http GET https://registry.npmjs.org/sails-disk
npm http GET https://registry.npmjs.org/include-all
npm http GET https://registry.npmjs.org/ejs
.....
$ npm install sails-generate-auth
npm http GET https://registry.npmjs.org/sails-generate-auth
npm http 304 https://registry.npmjs.org/sails-generate-auth
.....
sails-generate-auth@0.1.4 node_modules\sails-generate-auth
├── merge-defaults@0.1.0
└── lodash@2.4.1
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ npm install sails-generate-auth --save
npm http GET https://registry.npmjs.org/sails-generate-auth
npm http 304 https://registry.npmjs.org/sails-generate-auth
.....
sails-generate-auth@0.1.4 node_modules\sails-generate-auth
├── merge-defaults@0.1.0
└── lodash@2.4.1
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ sails generate auth
Running generator (sails-generate-auth) @ `c:\Users\Derek\Desktop\workspace\sail
s-auth-example`...
error: Something else already exists at ::c:\Users\Derek\Desktop\workspace\sails
-auth-example\config\locales\en.json
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ rm c:\Users\Derek\Desktop\workspace\sails-auth-example\config\locales\en.json
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ sails generate auth
Running generator (sails-generate-auth) @ `c:\Users\Derek\Desktop\workspace\sail
s-auth-example`...
error: Something else already exists at ::c:\Users\Derek\Desktop\workspace\sails
-auth-example\api\services\protocols
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ rm c:\Users\Derek\Desktop\workspace\sails-auth-example\api\services\protocols
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ sails generate auth
Running generator (sails-generate-auth) @ `c:\Users\Derek\Desktop\workspace\sail
s-auth-example`...
info: Created a new auth!
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ npm install bcrypt --save; npm install passport --save; npm install validator
--save; npm install passport-facebook --save; npm install passport-local --save
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ sails generate model user
info: Created a new model ("User") at api/models/User.js!
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ rm ./views/homepage.ejs
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ mkdir ./views/auth;
Derek@GETY ~/Desktop/workspace/sails-auth-example
$ touch ./views/auth/login.ejs; touch ./views/auth/register.ejs; touch ./views/auth/index.ejs; touch ./assets/styles/main.css
api/models/User.js
module.exports = {
// Enforce model schema in the case of schemaless databases
schema: true,
attributes: {
username : { type: 'string', unique: true },
email : { type: 'email', unique: true },
passports : { collection: 'Passport', via: 'user' }
}
};
@orgjscom
Copy link

orgjscom commented Jul 6, 2015

sudo chmod 777 sails-auth-example -R

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment