Skip to content

Commit

Permalink
Preparing for 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kokeksibir committed Aug 1, 2015
1 parent ea4668d commit 2ad7bcc
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 70 deletions.
25 changes: 15 additions & 10 deletions module.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
angular.module('basyt.angular', ['ui.router'])
angular.module('basyt-angular', ['ui.router'])
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('AuthInterceptor');
$httpProvider.interceptors.push('BasytAuthInterceptor');
}])
.value('BasytAnonState', 'login')
.value('BasytAuthMessages', {
'loginRequired': 'Login Required',
'loginSuccess': 'Login Successful',
'loginFailed': 'Login Failed',
'logoutSuccess': 'Logout Successful',
'authFailed': 'Authorization Failed'
loginRequired: 'Login Required',
loginSuccess: 'Login Successful',
loginFailed: 'Login Failed',
logoutSuccess: 'Logout Successful',
authFailed: 'Authorization Failed'
})
.run(['$rootScope', 'Auth', '$state','BasytAnonState', '$injector', 'BasytAuthMessages', function($rootScope, Auth, $state, BasytAnonState, $injector, BasytAuthMessages){
.value('BasytServer', {
host: window.API_URL,
socket: window.SOCKET_URL,
socketOptions: window.SOCKET_OPTS
})
.run(['$rootScope', '$state', '$injector', 'BasytAuth', 'BasytAnonState', 'BasytAuthMessages', function($rootScope, $state, BasytAuth, $injector, BasytAnonState, BasytAuthMessages){
var $alert = $injector.get('$alert');
$rootScope.$on("$stateChangeStart", function(event, next) {
if (next.role) {
if (!Auth.isAuthenticated(next.role)) {
if (!BasytAuth.isAuthenticated(next.role)) {
if($alert) {
$alert({
title: BasytAuthMessages.authFailed,
Expand All @@ -38,4 +43,4 @@ angular.module('basyt.angular', ['ui.router'])
}
$state.go(BasytAnonState);
});
}]);
}]);
19 changes: 6 additions & 13 deletions services/Auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module('basyt.angular')
.factory('Auth', ['BasytLocalStore', 'Request', '$q', '$rootScope', '$injector', 'BasytAuthMessages', function (BasytLocalStore, Request, $q, $rootScope, $injector, BasytAuthMessages) {
angular.module('basyt-angular')
.factory('BasytAuth', ['BasytLocalStore', 'BasytRequest', '$q', '$rootScope', '$injector', 'BasytAuthMessages', function (BasytLocalStore, BasytRequest, $q, $rootScope, $injector, BasytAuthMessages) {
var AnonUser = {
user_state: 'ANON'
},
Expand Down Expand Up @@ -54,13 +54,6 @@ angular.module('basyt.angular')
return deferred.promise;
},
Auth = {
authorize: function (access) {
if (access) {
return this.isAuthenticated(access);
} else {
return true;
}
},
isAuthenticated: function (access, remote) {
if (BasytLocalStore.get('auth_token')) {
if (remote) Auth.authenticate();
Expand Down Expand Up @@ -102,18 +95,18 @@ angular.module('basyt.angular')
login: function (credentials, rememberMe) {
logout(true);
//if(rememberMe)
return Request('user:login', {data: credentials}).then(login, logoutReject);
return BasytRequest('user:login', {data: credentials}).then(login, logoutReject);
},
logout: logout,
register: function (formData) {
logout(true);
return Request('user:register', {user: formData}).then(login, logoutReject);
return BasytRequest('user:register', {user: formData}).then(login, logoutReject);
},
authenticate: function () {
return Request('user:authorize').then(function () {
return BasytRequest('user:authenticate').then(function () {
}, logout);
}
};
$rootScope.activeUser = User;
return Auth;
}]);
}]);
6 changes: 3 additions & 3 deletions services/AuthInterceptor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module('basyt.angular')
.factory('AuthInterceptor', ['$q', 'BasytLocalStore', '$rootScope', function ($q, BasytLocalStore, $rootScope) {
angular.module('basyt-angular')
.factory('BasytAuthInterceptor', ['$q', 'BasytLocalStore', '$rootScope', function ($q, BasytLocalStore, $rootScope) {
return {
request: function (config) {
var token;
Expand All @@ -20,4 +20,4 @@ angular.module('basyt.angular')
return $q.reject(response);
}
};
}]);
}]);
18 changes: 9 additions & 9 deletions services/DataSource.js → services/EntityBridge.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
angular.module('basyt.angular')
.service('DataSource', ['Request', '$rootScope', 'filterFilter', 'Socket', '$q', function (Request, $rootScope, filterFilter, Socket, $q) {
var DataSource = function (entityName, map) {
angular.module('basyt-angular')
.factory('BasytEntityBridge', ['BasytRequest', 'BasytSocket', '$rootScope', 'filterFilter', '$q', function (BasytRequest, BasytSocket, $rootScope, filterFilter, $q) {
var EntityBridge = function (entityName, map) {
var that = this;
this.endpoint = entityName + ':list';
this.socketChannel = 'entity:' + entityName;
this.map = map;
this.isLoaded = false;
this.listeners = [];
this.value = [];
Socket.on('entity:update:' + entityName, function (message) {
BasytSocket.on('entity:update:' + entityName, function (message) {
that.reload(false);
});
this.reload(true);
};

DataSource.prototype.bind = function () {
EntityBridge.prototype.bind = function () {
var deferred = $q.defer(), that = this, promise = deferred.promise;
this.listeners.push(deferred);
promise.list = this.value;
Expand All @@ -33,9 +33,9 @@ angular.module('basyt.angular')
};
return promise;
};
DataSource.prototype.reload = function (subscribe) {
EntityBridge.prototype.reload = function (subscribe) {
var that = this;
Request(this.endpoint, {params: {deep: true}})
BasytRequest(this.endpoint, {params: {deep: true}})
.then(function (data) {
if (that.map) {
angular.forEach(data.result, that.map);
Expand All @@ -46,7 +46,7 @@ angular.module('basyt.angular')
deferred.notify(that.value);
});
if (subscribe) {
Socket.subscribe(that.socketChannel);
BasytSocket.subscribe(that.socketChannel);
}
},
function () {
Expand All @@ -55,5 +55,5 @@ angular.module('basyt.angular')
});
};

return DataSource;
return EntityBridge;
}]);
12 changes: 6 additions & 6 deletions services/LocalStore.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
angular.module('basyt.angular')
.factory('BasytLocalStore', function() {
angular.module('basyt-angular')
.factory('BasytLocalStore', ['$window', function($window) {
return {
get: function(key) {
return localStorage.getItem(key);
return $window.localStorage.getItem(key);
},
set: function(key, val) {
return localStorage.setItem(key, val);
return $window.localStorage.setItem(key, val);
},
unset: function(key) {
return localStorage.removeItem(key);
return $window.localStorage.removeItem(key);
}
};
});
}]);
11 changes: 3 additions & 8 deletions services/Request.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
angular.module('basyt.angular')
.constant('BasytServer', {
host: window.API_URL,
socket: window.SOCKET_URL,
socketOptions: window.SOCKET_OPTS
})
.factory('Request', ['$http', 'BasytServer', '$q', '$rootScope', '$urlMatcherFactory', function ($http, BasytServer, $q, $rootScope, $urlMatcherFactory) {
angular.module('basyt-angular')
.factory('BasytRequest', ['$http', '$q', '$rootScope', '$urlMatcherFactory', 'BasytServer', function ($http, $q, $rootScope, $urlMatcherFactory, BasytServer) {
var endpoints, initialized = false, future,
deferred = $q.defer();
$http.get(BasytServer.host)
Expand Down Expand Up @@ -63,4 +58,4 @@ angular.module('basyt.angular')
}
);
};
}]);
}]);
17 changes: 9 additions & 8 deletions services/Socket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module('basyt.angular')
.factory('Socket', ['BasytServer', 'BasytLocalStore', '$rootScope', '$q', 'Auth', function (BasytServer, BasytLocalStore, $rootScope, $q) {
angular.module('basyt-angular')
.factory('BasytSocket', ['$rootScope', '$q', 'BasytServer', 'BasytLocalStore', function ($rootScope, $q, BasytServer, BasytLocalStore) {
var connection, future = $q.defer();
connect = function () {
connection = io.connect(BasytServer.socket, BasytServer.socketOptions);
Expand All @@ -11,7 +11,8 @@ angular.module('basyt.angular')
.emit('authenticate', {token: BasytLocalStore.get('auth_token')}); //send the jwt

};
connect();
if(angular.isDefined(io))
connect();
return {
on: function (event, cb) {
if (connection) {
Expand All @@ -26,15 +27,15 @@ angular.module('basyt.angular')
connection.removeAllListeners(event, cb);
}
},
subscribe: function (event, data) {
connection.emit('subscribe', {resource: event, data: data});
subscribe: function (channel, data) {
connection.emit('subscribe', {resource: channel, data: data});
},
unsubscribe: function (event, data) {
connection.emit('unsubscribe', {resource: event, data: data});
unsubscribe: function (channel, data) {
connection.emit('unsubscribe', {resource: channel, data: data});
},
emit: function (label, data) {
connection.emit(label, data);
},
connect: connect
};
}]);
}]);
31 changes: 18 additions & 13 deletions services/UserSettings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module('basyt.angular')
.factory('UserSettings', ['Request', '$rootScope', function (Request, $rootScope) {
angular.module('basyt-angular')
.factory('BasytUserSettings', ['$rootScope', 'BasytRequest', 'BasytAuth', function ($rootScope, BasytRequest, BasytAuth) {
var settings, ready = false,
service = {
isReady: function () {
Expand All @@ -9,20 +9,25 @@ angular.module('basyt.angular')
return ready ? settings : {};
},
reload: function () {
Request('user_settings:get')
.then(
function (data) {
BasytRequest('user_settings:get')
.then(function (data) {
settings = data.result || {};
ready = true;
$rootScope.$broadcast('user:registered');
},
function(){
$rootScope.$broadcast('user:anonymous');
$rootScope.$broadcast('basyt:user_settings:ready');
});
}
};

service.reload();

if(BasytAuth.isAuthenticated) {
service.reload();
}
$rootScope.$on('user:login', function(){
service.reload();
});
$rootScope.$on('user:logout', function(){
ready = false;
});
$rootScope.$on('user:anonymous', function(){
ready = false;
});
return service;
}]);
}]);

0 comments on commit 2ad7bcc

Please sign in to comment.