Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html data-ng-app="angular-client-side-auth" lang="en">
<head>
<meta charset="utf-8">
<title>Angular Auth Example</title>
<link href="/css/app.css" rel="stylesheet">
<link href="/components/bootstrap/dist/css/bootstrap.min.css" rel=
"stylesheet">
@fnakstad
fnakstad / circShift.js
Created December 8, 2014 04:28
Circular shift
// First creates a shallow copy of array,
// then shifts contents circularly according to
// number of steps and direction given
function circularShift(arrayPar, steps, shiftLeft) {
var array = arrayPar.slice(0);
for(var i = 0; i < steps; i++) {
if(shiftLeft)
array.push(array.shift());
else
@fnakstad
fnakstad / gist:9fc39aae92842d84df4e
Last active November 28, 2015 04:25
Check network availability in Swift
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
(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']) {
@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) {
@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