Skip to content

Instantly share code, notes, and snippets.

@fnakstad
fnakstad / gist:5383750
Created April 14, 2013 18:43
Attaches a function called "seed" to Mongoose models, which basically wraps the create function, but returns a promise. This way seeding can be done through a promise chain.
var mongoose = require('mongoose');
mongoose.Model.seed = function(entities) {
var promise = new mongoose.Promise;
this.create(entities, function(err) {
if(err) { promise.reject(err); }
else { promise.resolve(); }
});
return promise;
};
(function(exports){
exports.config = {
apiKey: "sgsag",
foo: "bar
};
})(typeof exports === 'undefined'? this['routingConfig']={}: exports);
<div accessLevel="{{ accessLevels.anon }}">
<!-- ... Login stuff -->
</div>
<div accessLevel="{{ accessLevels.user }}">
Welcome {{ user.name }}!
</div>
module.exports.allowCrossDomain = function(req, res, next) {
var oneof = false
, clientUrl = 'http://myclient.com';
if (req.headers.origin) {
res.header('Access-Control-Allow-Origin', clientUrl);
res.header('Access-Control-Allow-Credentials', true);
oneof = true;
}
if (req.headers['access-control-request-method']) {
(function(exports){
var userRoles = {
public: 1, // 001
user: 2, // 010
admin: 4 // 100
};
exports.userRoles = userRoles;
exports.accessLevels = {
@fnakstad
fnakstad / app.js
Last active December 19, 2015 23:09
angular.module('myApp', ['myApp.services', 'ngCookies'])
.config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
// ...
var access = routingConfig.accessLevels;
$routeProvider.when('/register',
{
app.get('/*', function(req, res){
var role = userRoles.public, username = '';
if(req.user) {
role = req.user.role;
username = req.user.username;
}
res.cookie('user', JSON.stringify({
'username': username,
'role': role
@fnakstad
fnakstad / app.js
Last active December 19, 2015 23:09
angular.module('angular-client-side-auth')
.factory('Auth', function($http, $rootScope, $cookieStore){
var accessLevels = routingConfig.accessLevels
, userRoles = routingConfig.userRoles
, currentUser = $cookieStore.get('user') ||
{ username: '', role: userRoles.public };
// ...
angular.module('angular-client-side-auth')
.factory('Auth', function($http, $rootScope, $cookieStore){
// ...
$rootScope.accessLevels = accessLevels;
$rootScope.userRoles = userRoles;
return {
authorize: function(accessLevel, role) {
angular.module('angular-client-side-auth', ['ngCookies'])
.run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if (!Auth.authorize(next.access)) {
if(Auth.isLoggedIn()) $location.path('/');
else $location.path('/login');
}
});