Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* AuthService service definition */
- app.factory('AuthService', [
- '$q', '$location', '$toastr', '$cookies', '$sails',
- function ($q, $location, $toastr, $cookies, $sails) {
- /*
- The cookie we use to store session information in
- */
- var authService, cookieName;
- cookieName = '_ideaSrc_authenticated_user';
- /*
- The service object iself
- */
- authService = {
- /*
- Used to do an initial setting
- */
- _initial: false,
- /*
- Stores all the subscribers to the authentication user change event
- */
- subscribers: [],
- /*
- Gets the current user session
- @force get from API directly
- */
- current: function (force) {
- /* Telling whoever cares that we've done this at least once */
- this._initial = true;
- /*
- If forced, we have to make an actual socket call, otherwise
- we'll use our trusty cookies to get the session
- */
- if (force != null) {
- return this.getSession();
- } else {
- return $cookies.get(cookieName);
- }
- },
- /*
- Updates the current user session
- NB: Uses lodash debounce to spare this thing from being called non-stop
- */
- updateCurrent: _.debounce(function (current) {
- /*
- If we've removed the session, remove the cookie
- Else update the cookie with the new session
- */
- var i, l;
- if (current == null) {
- $cookies.remove(cookieName);
- } else {
- $cookies.put(cookieName, current.user, {
- expires: current.expires
- });
- }
- /* Notify everybody */
- i = 0;
- l = this.subscribers.length;
- /* FYI function.apply() executes a function. */
- while (i < l) {
- this.subscribers[i++].apply();
- }
- return true;
- }, 250),
- /*
- Subscribe to the authentication user change event
- */
- subscribe: function (cb) {
- return this.subscribers.push(cb);
- },
- /*
- Gets the current session. Be careful of when and where you're calling
- this. It tends to go infinite loop a lot and I have no idea why.
- Probable cause is Angular's $digest cycle being a dick...
- */
- getSession: function () {
- return $sails.get('/users/session')
- .success((function (_this) {
- return function (session) {
- /*
- Updating the current session.
- No errors -> session. Errors -> no session
- */
- _this.updatecurrent(session.error == null ? session : null);
- /* Returning the current session value or nada */
- if (session.error == null) {
- return _this.current(false);
- } else {
- return null;
- }
- };
- })(this))
- .error(function () {
- return null;
- });
- },
- /*
- Used to authenticate a request. Used as a "allowed" check mostly.
- @goToLogin if set to true will kick unauthorized people to the index page
- */
- authenticate: function (goToLogin) {
- /* Directly getting the current user */
- $sails.get('/users/current')
- .success(function (response) {
- /* We have a problem Houston */
- if (response.error && goToLogin) {
- $toastr('error', 'Not Authorized', 'You are not authorized to access this page.');
- /* Go to the index page, pls */
- $location.path('/');
- /* Denied. */
- return defer.reject('Not Authorized');
- } else {
- /*
- Updating the current session.
- No errors -> session. Errors -> no session
- The reason we update the session here is to keep data consistency,
- maybe it updated since last time we checked.
- */
- this.updatecurrent(response.error == null ? response.session : null);
- /* Tell whoever cares the good news */
- return defer.resolve(response);
- }
- })
- .error(function () {
- /* Straight up deny requests on error */
- return defer.reject('Not authorized');
- });
- return defer.promise;
- }
- };
- /* END OF LINE */
- return authService;
- }
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement