Advertisement
Guest User

Untitled

a guest
Jan 10th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. var app = angular.module('LoginApp', ["firebase", "ngRoute"])
  2.  
  3. app.config(function ($routeProvider) {
  4. $routeProvider
  5. .when('/', {
  6. templateUrl: 'registration.html',
  7. controller: 'AuthCtrl'
  8. })
  9. .when('/logIn', {
  10. templateUrl: 'login.html',
  11. controller: 'AuthCtrl'
  12. })
  13.  
  14. .when('/User', {
  15. templateUrl: "User.html",
  16. controller: 'AuthCtrl'
  17. })
  18. .otherwise({
  19. redirectTo: '/'
  20. });
  21.  
  22.  
  23. });
  24.  
  25.  
  26. app.factory("Auth", ["$firebaseAuth",
  27. function($firebaseAuth) {
  28. var ref = new Firebase("https://uniquecoders.firebaseio.com/");
  29. return $firebaseAuth(ref);
  30. }
  31. ]);
  32.  
  33.  
  34. app.controller("AuthCtrl", ["$scope", "Auth","$rootScope",
  35. function($scope, Auth, $rootScope) {
  36. var User = {}
  37.  
  38. $scope.createUser = function(username, email, password) {
  39. $rootScope.usernames = username
  40. User.username = username
  41. $scope.message = null;
  42. $scope.error = null;
  43. var ref2 = new Firebase("https://uniquecoders.firebaseio.com/");
  44. ref2.createUser({
  45. email: $scope.email,
  46. password: $scope.password
  47. }, function(error, userData) {
  48. if (error) {
  49. switch (error.code) {
  50. case "EMAIL_TAKEN":
  51. alert("The new user account cannot be created because the email is already in use. Try to login");
  52. break;
  53. case "INVALID_EMAIL":
  54. alert("The specified email is not a valid email.");
  55. break;
  56. case "INVALID_PASSWORD":
  57. alert("The Specified Passowrd Is not valid.")
  58. break;
  59. default:
  60. alert("Error creating user:", error);
  61. }
  62. } else {
  63. alert("Successfully created user account with username" + User.username);
  64. document.cookie ="username =" +username + "; " +"email ="+ email;
  65. window.location.hash = "/User"
  66. }
  67. });
  68.  
  69.  
  70. };
  71.  
  72. $scope.logIn = function(){
  73. function getCookie(name ) {
  74. var pairs = document.cookie.split("; "),
  75. count = pairs.length, parts;
  76. while ( count-- ) {
  77. parts = pairs[count].split("=");
  78. if ( parts[0] === name )
  79. return parts[1];
  80. }
  81. return name;
  82. }
  83. var username=getCookie(User.username);
  84. alert(username)
  85. $scope.message = null;
  86. $scope.error = null;
  87. var ref2 = new Firebase("https://uniquecoders.firebaseio.com/");
  88. ref2.authWithPassword({
  89. "email" : $scope.logInemail,
  90. "password" : $scope.logInemailpassword
  91.  
  92. }, function(error, userData){
  93.  
  94. if(error){
  95. alert("Login Failed Because : " + error)
  96. }
  97. else{
  98. alert("Logged In!")
  99. window.location.hash = "/User"
  100. }
  101.  
  102. })
  103.  
  104. }
  105.  
  106. /* $scope.removeUser = function() {
  107. $scope.message = null;
  108. $scope.error = null;
  109.  
  110. Auth.$removeUser({
  111. email: $scope.email,
  112. password: $scope.password
  113. }).then(function() {
  114. $scope.message = "User removed";
  115. }).catch(function(error) {
  116. $scope.error = error;
  117. });
  118. };*/
  119. }
  120. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement