Skip to content

Commit

Permalink
feat(app-auth): Improve client-side Auth service
Browse files Browse the repository at this point in the history
Changes:
- getCurrentUser, isLoggedIn, and isAdmin are now sync if no arg and async with an arg
- Use Error first callback signature where applicable
- Remove unused arguments from Auth service
- Remove isLoggedInAsync
- Switch use of isLoggedInAsync to isLoggedIn
- Add/Improve comments
- Fix client/app/account(auth)/settings/settings.controller(js).js

Breaking Changes:
- Callbacks that return Errors, use 'Error first' signature

Closes #456
  • Loading branch information
kingcody authored and DaftMonk committed Sep 17, 2014
1 parent 502be54 commit 65d03fc
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 105 deletions.
4 changes: 2 additions & 2 deletions app/templates/client/app/app(coffee).coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ angular.module '<%= scriptAppName %>', [<%= angularModules %>]
.run ($rootScope, $location, Auth) ->
# Redirect to login if route requires auth and you're not logged in
$rootScope.$on <% if(filters.ngroute) { %>'$routeChangeStart'<% } %><% if(filters.uirouter) { %>'$stateChangeStart'<% } %>, (event, next) ->
Auth.isLoggedInAsync (loggedIn) ->
Auth.isLoggedIn (loggedIn) ->
$location.path "/login" if next.authenticate and not loggedIn
<% } %>
<% } %>
4 changes: 2 additions & 2 deletions app/templates/client/app/app(js).js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ angular.module('<%= scriptAppName %>', [<%= angularModules %>])
.run(function ($rootScope, $location, Auth) {
// Redirect to login if route requires auth and you're not logged in
$rootScope.$on(<% if(filters.ngroute) { %>'$routeChangeStart'<% } %><% if(filters.uirouter) { %>'$stateChangeStart'<% } %>, function (event, next) {
Auth.isLoggedInAsync(function(loggedIn) {
Auth.isLoggedIn(function(loggedIn) {
if (next.authenticate && !loggedIn) {
$location.path('/login');
}
});
});
})<% } %>;
})<% } %>;
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
'use strict'

angular.module '<%= scriptAppName %>'
.factory 'Auth', ($location, $rootScope, $http, User, $cookieStore, $q) ->
.factory 'Auth', ($http, User, $cookieStore, $q) ->
currentUser = if $cookieStore.get 'token' then User.get() else {}

###
Authenticate user and save token
@param {Object} user - login info
@param {Function} callback - optional
@param {Function} callback - optional, function(error)
@return {Promise}
###
login: (user, callback) ->
deferred = $q.defer()
$http.post '/auth/local',
email: user.email
password: user.password

.success (data) ->
$cookieStore.put 'token', data.token
.then (res) ->
$cookieStore.put 'token', res.data.token
currentUser = User.get()
deferred.resolve data
callback?()
res.data

.error (err) =>
, (err) =>
@logout()
deferred.reject err
callback? err

deferred.promise
callback? err.data
$q.reject err.data


###
Delete access token and user info
@param {Function}
###
logout: ->
$cookieStore.remove 'token'
Expand All @@ -46,15 +41,15 @@ angular.module '<%= scriptAppName %>'
Create a new user
@param {Object} user - user info
@param {Function} callback - optional
@param {Function} callback - optional, function(error, user)
@return {Promise}
###
createUser: (user, callback) ->
User.save user,
(data) ->
$cookieStore.put 'token', data.token
currentUser = User.get()
callback? user
callback? null, user

, (err) =>
@logout()
Expand All @@ -68,7 +63,7 @@ angular.module '<%= scriptAppName %>'
@param {String} oldPassword
@param {String} newPassword
@param {Function} callback - optional
@param {Function} callback - optional, function(error, user)
@return {Promise}
###
changePassword: (oldPassword, newPassword, callback) ->
Expand All @@ -79,7 +74,7 @@ angular.module '<%= scriptAppName %>'
newPassword: newPassword

, (user) ->
callback? user
callback? null, user

, (err) ->
callback? err
Expand All @@ -88,45 +83,61 @@ angular.module '<%= scriptAppName %>'


###
Gets all available info on authenticated user
Gets all available info on a user
(synchronous|asynchronous)
@return {Object} user
@param {Function|*} callback - optional, funciton(user)
@return {Object|Promise}
###
getCurrentUser: ->
currentUser
getCurrentUser: (callback) ->
return currentUser if arguments.length is 0

value = if (currentUser.hasOwnProperty("$promise")) then currentUser.$promise else currentUser
$q.when value

###
Check if a user is logged in synchronously
.then (user) ->
callback? user
user

@return {Boolean}
###
isLoggedIn: ->
currentUser.hasOwnProperty 'role'
, ->
callback? {}
{}


###
Waits for currentUser to resolve before checking if user is logged in
Check if a user is logged in
(synchronous|asynchronous)
@param {Function|*} callback - optional, function(is)
@return {Bool|Promise}
###
isLoggedInAsync: (callback) ->
if currentUser.hasOwnProperty '$promise'
currentUser.$promise.then ->
callback? true
return
.catch ->
callback? false
return
isLoggedIn: (callback) ->
return currentUser.hasOwnProperty("role") if arguments.length is 0

@getCurrentUser null

.then (user) ->
is_ = user.hasOwnProperty("role")
callback? is_
is_

else
callback? currentUser.hasOwnProperty 'role'

###
Check if a user is an admin
(synchronous|asynchronous)
@return {Boolean}
@param {Function|*} callback - optional, function(is)
@return {Bool|Promise}
###
isAdmin: ->
currentUser.role is 'admin'
isAdmin: (callback) ->
return currentUser.role is "admin" if arguments_.length is 0

@getCurrentUser null

.then (user) ->
is_ = user.role is "admin"
callback? is_
is_


###
Expand Down
Loading

0 comments on commit 65d03fc

Please sign in to comment.