-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Spratley, Jonnie
committed
Dec 19, 2014
1 parent
66f429d
commit 59efece
Showing
7 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
/** | ||
@module AuthService | ||
@description | ||
This service will take care of authentication of a user, common methods include: | ||
* authorize | ||
* logout | ||
* register | ||
* forgot | ||
* currentUser | ||
*/ | ||
angular.module('angularCmsApp').service('cmsAuthService', function($q, $http, $log, $rootScope, $cookieStore, $location, cmsSessionService, cmsNotify) { | ||
var cmsAuthService; | ||
return cmsAuthService = { | ||
endpoint: '/api/v2', | ||
|
||
/** | ||
authorize - I handle authorizing a user. | ||
*/ | ||
authorize: function(user) { | ||
return $http.post(this.endpoint + "/login", user); | ||
}, | ||
|
||
/** | ||
session - I handle getting a session. | ||
*/ | ||
session: function() { | ||
return $http.get(this.endpoint + "/session"); | ||
}, | ||
|
||
/** | ||
register - I handle register a user. | ||
*/ | ||
register: function(user) { | ||
$log.info('trying to register', user); | ||
return $http.post(this.endpoint + "/register", user).then((function(_this) { | ||
return function(res) { | ||
$log.info(res); | ||
return _this.authorize(res.data); | ||
}; | ||
})(this), function(err) { | ||
$log.error(err); | ||
return cmsNotify('.message', 'danger', 'Error!', err.data.message, 4000); | ||
}); | ||
}, | ||
|
||
/** | ||
Logout method to clear the session. | ||
@param {Object} user - A user model containing remember | ||
*/ | ||
logout: function(user) { | ||
cmsSessionService.setUserAuthenticated(null); | ||
return $rootScope.apply(function() { | ||
return $location.reload(); | ||
}); | ||
}, | ||
|
||
/** | ||
Login | ||
*/ | ||
login: function(user) { | ||
return this.authorize(user).then(function(res) { | ||
var session; | ||
cmsNotify('.login-message', 'success', 'Success!', "Welcome back.", 5000); | ||
session = { | ||
user: res.data, | ||
authorized: true | ||
}; | ||
cmsSessionService.setSession(session); | ||
$rootScope.App.session = session; | ||
$log.info('login-result', res); | ||
return $rootScope.App.location.path('/dashboard'); | ||
}, function(err) { | ||
$log.error(err); | ||
return cmsNotify('.login-message', 'danger', 'Error!', err.data.message); | ||
}); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
angular.module('angularCmsApp').factory('cmsDataServiceFactory', function() { | ||
var meaningOfLife; | ||
meaningOfLife = 42; | ||
return { | ||
someMethod: function() { | ||
return meaningOfLife; | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var DataServiceProvider; | ||
|
||
angular.module("angularCmsApp").provider("cmsDataServiceProvider", DataServiceProvider = function() { | ||
var DataServiceFactory, options; | ||
DataServiceFactory = void 0; | ||
options = void 0; | ||
this.options = function(value) { | ||
return options = !!value; | ||
}; | ||
this.$get = [ | ||
"options", DataServiceFactory = function(options) { | ||
return new cmsDataService(options); | ||
} | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
angular.module('angularCmsApp').factory('cmsNotify', [ | ||
'$timeout', '$q', function($timeout, $q) { | ||
var notices, notify; | ||
notices = []; | ||
notify = function(el, type, title, msg, timeout) { | ||
var alert; | ||
notices.push({ | ||
type: type, | ||
title: title, | ||
msg: msg | ||
}); | ||
alert = "<div class=\"alert alert-" + type + " alert-dismissable\"> \n <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button> \n <strong>" + title + "</strong> " + msg + "\n</div>"; | ||
if (el) { | ||
angular.element(el).prepend(alert); | ||
} else { | ||
angular.element('.container').prepend(alert); | ||
} | ||
if (timeout) { | ||
return $timeout(function() { | ||
return angular.element('.alert').fadeOut().remove(); | ||
}, timeout); | ||
} | ||
}; | ||
return notify; | ||
} | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
angular.module('angularCmsApp').service('cmsSessionService', [ | ||
'$q', '$rootScope', '$cookieStore', '$location', '$log', function($q, $rootScope, $cookieStore, $location, $log) { | ||
var SessionService, getUserAuthenticated, setUserAuthenticated, userIsAuthenticated; | ||
userIsAuthenticated = $cookieStore.get('App.session'); | ||
setUserAuthenticated = function(value) { | ||
window.sessionStorage.setItem('userIsAuthenticated', value); | ||
$cookieStore.put('App.session', value); | ||
userIsAuthenticated = value; | ||
return $log.info("user is authorized: " + userIsAuthenticated.authorized); | ||
}; | ||
getUserAuthenticated = function() { | ||
window.sessionStorage.getItem('userIsAuthenticated'); | ||
$log.info("user is authorized: " + userIsAuthenticated.authorized); | ||
return userIsAuthenticated.authorized; | ||
}; | ||
$rootScope.$on("$locationChangeStart", function(event, next, current) { | ||
var i, msg, _results; | ||
$rootScope.$emit('session:route:start', { | ||
event: event, | ||
next: next, | ||
current: current | ||
}); | ||
angular.element('.active').removeClass('active'); | ||
_results = []; | ||
for (i in window.routes) { | ||
if (next.indexOf(i) !== -1) { | ||
if (window.routes[i].requireLogin && !getUserAuthenticated()) { | ||
msg = "You need to be authenticated to see this page!"; | ||
$log.warn(msg); | ||
event.preventDefault(); | ||
$rootScope.$emit('session:unauthorized', event); | ||
_results.push($location.path('/')); | ||
} else { | ||
angular.element('a[href="#' + $location.path() + '"]').addClass('active'); | ||
_results.push($rootScope.$emit('session:authorized', event)); | ||
} | ||
} else { | ||
_results.push(void 0); | ||
} | ||
} | ||
return _results; | ||
}); | ||
SessionService = { | ||
adapter: null, | ||
session: $cookieStore.get('App.session'), | ||
isAuthenticated: false, | ||
getUserAuthenticated: getUserAuthenticated, | ||
setUserAuthenticated: setUserAuthenticated, | ||
getSession: function() { | ||
if ($cookieStore.get('App.session')) { | ||
return $cookieStore.get('App.session'); | ||
} else { | ||
return {}; | ||
} | ||
}, | ||
setSession: function(value) { | ||
return $cookieStore.put('App.session', value); | ||
}, | ||
login: function(user) { | ||
var _ref; | ||
$rootScope.$emit('session:login', user); | ||
return (_ref = SessionService.adapter) != null ? typeof _ref.login === "function" ? _ref.login(user) : void 0 : void 0; | ||
}, | ||
logout: function(user) { | ||
var _ref; | ||
$rootScope.$emit('session:logout', user); | ||
SessionService.setUserAuthenticated(user); | ||
return (_ref = SessionService.adapter) != null ? typeof _ref.logout === "function" ? _ref.logout(user) : void 0 : void 0; | ||
}, | ||
register: function(user) { | ||
var _ref; | ||
$rootScope.$emit('session:register', user); | ||
return (_ref = SessionService.adapter) != null ? typeof _ref.register === "function" ? _ref.register(user) : void 0 : void 0; | ||
}, | ||
routeResolver: function() {} | ||
}; | ||
return SessionService; | ||
} | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @ngdoc service | ||
* @name angularCmsApp.cmsSocketService | ||
* @description | ||
* # cmsSocketService | ||
* Service in the angularCmsApp. | ||
*/ | ||
angular.module('angularCmsApp').factory('cmsSocketService', function ($rootScope) { | ||
/** | ||
* I am a WebSocketClient | ||
* @param options | ||
* @returns {{instance: WebSocket, close: Function, send: Function}} | ||
* @constructor | ||
*/ | ||
var WebSocketClient = function (options) { | ||
var _ws = new WebSocket(options.endpoint, options.protocol); | ||
_ws.onmessage = function (e) { | ||
$rootScope.$emit(options.protocol, e); | ||
return console.log(e.data); | ||
}; | ||
_ws.onerror = function (e) { | ||
return console.log(e); | ||
}; | ||
_ws.onclose = function (e) { | ||
return console.log(e); | ||
}; | ||
_ws.onopen = function (e) { | ||
_ws.send('update'); | ||
}; | ||
return { | ||
instance: _ws, | ||
close: function () { | ||
return _ws.close(); | ||
}, | ||
send: function (obj) { | ||
try { | ||
_ws.send(JSON.stringify(obj)); | ||
} catch (err) { | ||
|
||
throw err; | ||
} | ||
} | ||
}; | ||
}; | ||
return WebSocketClient; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
/** | ||
@ngdoc service | ||
@name angularCmsApp.service:cmsUsersFactory | ||
@function | ||
@description | ||
This is the UsersFactory. | ||
*/ | ||
'use strict'; | ||
angular.module('angularCmsApp').factory('cmsUsersFactory', function() { | ||
var meaningOfLife; | ||
meaningOfLife = 42; | ||
return { | ||
someMethod: function() { | ||
return meaningOfLife; | ||
} | ||
}; | ||
}); |