Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. angular
  2. .module('app')
  3. // Auth Factory
  4. .factory("Auth", ["$firebaseAuth", "FIREBASE_URI",
  5. function($firebaseAuth, FIREBASE_URI) {
  6. var ref = new Firebase(FIREBASE_URI);
  7. return $firebaseAuth(ref);
  8. }
  9. ])
  10.  
  11. function authenticationCtrl($scope, $state, authService) {
  12. var authenticationCtrl = this;
  13.  
  14. // LOGIN
  15. var login = function (userObject) {
  16. authService.loginWithPassword(userObject, function () {
  17. $state.go('[...]');
  18. },
  19. function (errorText) {
  20. // ERROR
  21. console.error("Error logging in user:", errorText)
  22. });
  23. };
  24.  
  25. // LOGOUT
  26. var logout = function () {
  27. // Clear locally logged in user
  28. $scope.authData = null;
  29. authService.logout();
  30. };
  31.  
  32. // PUBLIC
  33. return {
  34. login: login,
  35. logout: logout
  36. };
  37. };
  38.  
  39. angular
  40. .module('app')
  41. .controller('authenticationCtrl', authenticationCtrl)
  42.  
  43. function authService($state, FIREBASE_URI, Auth) {
  44. var model = this,
  45. ref = new Firebase(FIREBASE_URI);
  46.  
  47. // LOGIN
  48. model.loginWithPassword = function(credentials, callBack, errorCallBack) {
  49. Auth.$authWithPassword(credentials)
  50. .then(function(authData) {
  51. model.cachedUser = authData;
  52. callBack();
  53. }).catch(function(error) {
  54. console.error("Authentication failed:", error);
  55. });
  56. };
  57.  
  58. // LOGOUT
  59. model.logout = function() {
  60. Auth.$unauth();
  61. model.cachedUser = null;
  62. $state.go('common.login');
  63. };
  64. };
  65.  
  66. angular
  67. .module('app')
  68. .service('authService', authService)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement