Created
September 29, 2015 21:21
-
-
Save sebastianhaas/0772d34c1b14f7d0bca5 to your computer and use it in GitHub Desktop.
LoopBack + Angular authentication
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
(function() { | |
'use strict'; | |
angular | |
.module('app.auth') | |
.factory('authService', authService); | |
authService.$inject = ['User', 'authEvents']; | |
/* @ngInject */ | |
function authService(User, authEvents) { | |
var service = { | |
login: login, | |
logout: logout, | |
register: register, | |
isAuthenticated: isAuthenticated, | |
getCurrentUser: getCurrentUser | |
}; | |
return service; | |
//////////////// | |
function login(email, password) { | |
return User | |
.login({email: email, password: password}); | |
} | |
function logout() { | |
return User | |
.logout(); | |
} | |
function register(email, password) { | |
return User | |
.create({ | |
email: email, | |
password: password | |
}); | |
} | |
function isAuthenticated() { | |
return User.isAuthenticated(); | |
} | |
function getCurrentUser() { | |
return User.getCurrent(); | |
} | |
} | |
})(); |
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
(function() { | |
'use strict'; | |
angular | |
.module('app.auth') | |
.controller('LoginController', LoginController); | |
LoginController.$inject = ['authService', '$state']; | |
/* @ngInject */ | |
function LoginController(authService, $state) { | |
var vm = this; | |
vm.login = login; | |
vm.credentials = {}; | |
//////////////// | |
function login() { | |
authService.login(vm.credentials.email, vm.credentials.password) | |
.then(function() { | |
$state.go('app.mainScreen'); | |
}); | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment