diff --git a/app/scripts/controllers/login.coffee b/app/scripts/controllers/login.coffee index 331ae09..f4e70a2 100644 --- a/app/scripts/controllers/login.coffee +++ b/app/scripts/controllers/login.coffee @@ -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. @@ -72,7 +49,6 @@ angular.module('angularCmsApp').controller 'LoginCtrl', ($scope, $rootScope, $co $scope.logout = (user) -> - #Clear cookie $cookieStore.put('App.session', null) diff --git a/app/scripts/services/cmsauthservice.coffee b/app/scripts/services/cmsauthservice.coffee index 29d2851..4cf8db2 100644 --- a/app/scripts/services/cmsauthservice.coffee +++ b/app/scripts/services/cmsauthservice.coffee @@ -10,10 +10,8 @@ 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' @@ -21,29 +19,52 @@ angular.module('angularCmsApp').service 'cmsAuthService', ($q, $http, $log, $roo 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) + ) diff --git a/test/protractor/pages/register-page.coffee b/test/protractor/pages/register-page.coffee index 321dba9..15c3d7e 100644 --- a/test/protractor/pages/register-page.coffee +++ b/test/protractor/pages/register-page.coffee @@ -19,6 +19,7 @@ RegisterPage = @password2.sendKeys(password) @agree.click().then(()=> @submit.click() + browser.sleep(1000) ) diff --git a/test/protractor/spec/login-spec.coffee b/test/protractor/spec/login-spec.coffee index 1827900..2439953 100644 --- a/test/protractor/spec/login-spec.coffee +++ b/test/protractor/spec/login-spec.coffee @@ -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('test@email.com', 'test').then(()-> expect(browser.getLocationAbsUrl()).toContain '/dashboard' )