Skip to content

Commit

Permalink
fixed login service
Browse files Browse the repository at this point in the history
  • Loading branch information
jonniespratley committed Dec 9, 2014
1 parent 64aeaff commit 44ad469
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
26 changes: 1 addition & 25 deletions app/scripts/controllers/login.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,7 @@ angular.module('angularCmsApp').controller 'LoginCtrl', ($scope, $rootScope, $co
setting the session and changing the location of the page.
###
$scope.login = (u) ->
cmsAuthService.authorize(u).then(
(res)->

#Welcome the user
cmsNotify( '.login-message', 'success', 'Success!', "Welcome back.", 5000)

#Set user session
session =
user: res.data.result
authorized: true

#Set user cookie
cmsSessionService.setSession(session)

#Set session on scope
$rootScope.App.session = session

#Change location
$rootScope.App.location.path('/dashboard')
,
(err)->
console.error(err)
cmsNotify( '.login-message', 'danger', 'Error!', err.data.message)
)
cmsAuthService.login(u)

###
Login Method to set the session.
Expand Down Expand Up @@ -72,7 +49,6 @@ angular.module('angularCmsApp').controller 'LoginCtrl', ($scope, $rootScope, $co

$scope.logout = (user) ->


#Clear cookie
$cookieStore.put('App.session', null)

Expand Down
37 changes: 29 additions & 8 deletions app/scripts/services/cmsauthservice.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,61 @@ This service will take care of authentication of a user, common methods include:
* currentUser
###
# AngularJS will instantiate a singleton by calling "new" on this function
angular.module('angularCmsApp').service 'cmsAuthService', ($q, $http, $log, $rootScope, $cookieStore, $location, cmsSessionService) ->

angular.module('angularCmsApp').service 'cmsAuthService', ($q, $http, $log, $rootScope, $cookieStore, $location, cmsSessionService, cmsNotify) ->
cmsAuthService =

#Endpoint location
endpoint: '/api/v2'

###*
authorize - I handle authorizing a user.
###
authorize: (user) ->
return $http.post( @endpoint+"/login", user )
return $http.post(@endpoint + "/login", user)

###*
session - I handle getting a session.
###
session: () ->
return $http.get( @endpoint+"/session" )
return $http.get(@endpoint + "/session")

###*
register - I handle register a user.
###
register: (user) ->
return $http.post( @endpoint+"/register", user )
return $http.post(@endpoint + "/register", user)

###*
Logout method to clear the session.
@param {Object} user - A user model containing remember
###
logout: (user) ->

#Clear cookie
cmsSessionService.logout(null)

$rootScope.apply(()->
$location.reload()
)
###*
Login
###
login: (user) ->
@authorize(user).then((res) ->
#Welcome the user
cmsNotify('.login-message', 'success', 'Success!', "Welcome back.", 5000)

#Set user session
session =
user: res.data.result
authorized: true

#Set user cookie
cmsSessionService.setSession(session)

#Set session on scope
$rootScope.App.session = session

#Change location
$rootScope.App.location.path('/dashboard')
, (err)->
console.error(err)
cmsNotify('.login-message', 'danger', 'Error!', err.data.message)
)
1 change: 1 addition & 0 deletions test/protractor/pages/register-page.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RegisterPage =
@password2.sendKeys(password)
@agree.click().then(()=>
@submit.click()
browser.sleep(1000)
)


Expand Down
2 changes: 1 addition & 1 deletion test/protractor/spec/login-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe 'Login:', ->
loginPage.get()
afterEach ->
loginPage.logout()
it 'should have Username and password inputs with a button to submit the form', ->
it 'should allow a user to login', ->
loginPage.login('[email protected]', 'test').then(()->
expect(browser.getLocationAbsUrl()).toContain '/dashboard'
)

0 comments on commit 44ad469

Please sign in to comment.