Skip to content

Instantly share code, notes, and snippets.

@clonn
Last active December 31, 2015 09:58
Show Gist options
  • Save clonn/7969977 to your computer and use it in GitHub Desktop.
Save clonn/7969977 to your computer and use it in GitHub Desktop.
talk for user login, demo for ncue
<div class="container" ng-app ng-controller="rootController">
<div class="starter-template">
<h1>Nice talking bar</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
<div>
<table class="table" style="margin-bottom: 300px;">
<thead>
<tr>
<th>User</th>
<th>message</th>
<th>time</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="message in messages">
<td>{{message.username}}</td>
<td>{{message.message}}</td>
<td>{{message.createdAt}}</td>
</tr>
</tbody>
</table>
</div>
<div style="position: fixed; bottom: 20px;">
<div class="form-group">
<label for="username">User name</label>
<span ng-model="username" ng-init="username='<%= user.username%>'"><%= user.username %></span>
</div>
<div class="form-group">
<label for="message">Message</label>
<input type="text" class="form-control" id="message" ng-model="message">
</div>
<div>
<button type="button" ng-click="sendMessage()" class="btn btn-default">Submit</button>
</div>
</div>
</div><!-- /.container -->
// path: /api/policies/isAuthenticated.js
module.exports = function(req, res, next) {
// User is allowed, proceed to the next policy,
// or if this is the last policy, the controller
if (req.session.user) {
return next();
}
// User is not allowed
// (default res.forbidden() behavior can be overridden in `config/403.js`)
return res.redirect("/login");
};
<div class="container">
<form action="/user/auth" method="POST" class="form-signin" role="form">
<h2 class="form-signin-heading">Please Login in</h2>
<input type="text" name="username" class="form-control" placeholder="Email address" required="" autofocus="">
<input type="password" name="password" class="form-control" placeholder="Password" required="">
<input type="submit" class="btn btn-lg btn-primary btn-block" value="submit">
</form>
</div>
<link href="http://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
// path: /config/policies.js
module.exports.policies = {
// Default policy for all controllers and actions
// (`true` allows public access)
'*': true,
UserController: {
'app': ["isAuthenticated"],
}
};
// path: /config/routes.js
module.exports.routes = {
// By default, your root route (aka home page) points to a view
// located at `views/home/index.ejs`
//
// (This would also work if you had a file at: `/views/home.ejs`)
'/': {
controller: 'UserController',
action: 'app',
},
'/login': {
view: 'home/login'
},
'/signup': {
view: 'home/signup'
}
};
<div class="container">
<form action="/user/signup" method="POST" class="form-signin" role="form">
<h2 class="form-signin-heading">Please Sgin up</h2>
<input type="text" name="username" class="form-control" placeholder="Email address" required="" autofocus="">
<input type="password" name="password" class="form-control" placeholder="Password" required="">
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Signup">
</form>
</div>
<link href="http://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
// Path: /api/models/user.js
module.exports = {
attributes: {
/* e.g.
nickname: 'string'
*/
username: "string",
password: "string"
}
};
//path: /api/controllers/UserController.js
module.exports = {
signup: function (req, res) {
var username = req.body.username;
var password = req.body.password;
if ( ! username || username == "") {
return res.redirect("/signup");
}
if ( ! password || password == "") {
return res.redirect("/signup");
}
User.find({
username: username
}).done(function (err, user) {
if (err) {
return res.redirect("/signup");
}
if (user && user.length > 0) {
return res.redirect("/signup");
}
User.create({
username: username,
password: password
}).done(function (err, value) {
return res.redirect("/");
});
});
},
auth: function (req, res) {
var username = req.body.username;
var password = req.body.password;
if ( ! username || username == "") {
return res.redirect("/login");
}
if ( ! password || password == "") {
return res.redirect("/login");
}
User.find({
username: username,
password: password
}).done(function (err, user) {
if (err) {
return res.redirect("/login");
}
if (! user || user.length <= 0) {
return res.redirect("/login");
}
req.session.user = {
username: username
};
return res.redirect("/");
});
},
app: function (req, res) {
res.view("home/index.ejs", {
user: req.session.user
});
},
/**
* Overrides for the settings in `config/controllers.js`
* (specific to UserController)
*/
_config: {}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment