Advertisement
Guest User

Untitled

a guest
Apr 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. .controller('AdminLoginCtrl', ['$scope', '$rootScope', '$location', '$cookies', 'AuthService', 'flashMessageService', '$log',
  2. function($scope, $rootScope, $location, $cookies, AuthService, flashMessageService, $log) {
  3. // $scope.adminLoggedIn = false;
  4.  
  5. $scope.credentials = {
  6. username: '',
  7. password: ''
  8. };
  9.  
  10. $scope.login = function(credentials) {
  11. console.log('in login....');
  12. AuthService.login(credentials).then(
  13. function(res) {
  14.  
  15. // THIS SHOULD BE WATCHED INSIDE DIRECTIVE TO SHOW/HIDE TEMPLATE
  16. $cookies.put('loggedInUser', res.data);
  17.  
  18. // USER SAVED TO COOKIES FINE HERE
  19. console.log('User from Cookies...', $cookies.get("loggedInUser"));
  20.  
  21. $location.path('/admin/pages');
  22. },
  23. function(err) {
  24. flashMessageService.setMessage(err.data);
  25. $log.log(err);
  26. });
  27. };
  28. }
  29. ])
  30.  
  31. angular.module('myApp.directives', ["ngCookies"])
  32. .directive('adminLogin', ['$rootScope','$cookies','AuthService','flashMessageService','$location',
  33. function($rootScope,$cookies,AuthService,flashMessageService,$location) {
  34. return {
  35. controller: function($scope, $cookies,$rootScope) {
  36.  
  37. // CONSOLE LOG IN DIRECTIVE CONFIRMS LOGGED IN USER IS SAVED IN COOKIES
  38. console.log("Func called in DIRECTIVE ", $cookies.get("loggedInUser"));
  39. $scope.loggedInUser = $cookies.get("loggedInUser");
  40.  
  41. /* NEED HELP WITH WATCH IMPLEMENTATION */
  42. $scope.$watch(/* what to watch */, function(newVal, oldVal) {
  43. console.log('WATCH ', nVal, oVal);
  44. /* ??? */
  45. })
  46.  
  47. console.log('logInUser In Dir ', $scope.loggedInUser);
  48.  
  49. $scope.logout = function() {
  50. console.log('Logout in DIR shud b remove cook ', $cookies);
  51. AuthService.logout().then(
  52. function() {
  53. $cookies.remove('loggedInUser'); // Want this to be watched
  54. $scope.loggedInUser = false; // Works but not ideal!
  55. $location.path('/admin/login');
  56. flashMessageService.setMessage("Successfully logged out");
  57.  
  58. }, function(err) {
  59. console.log('there was an error tying to logout', err);
  60. });
  61. };
  62.  
  63. },
  64. templateUrl: 'partials/directives/admin-login.html'
  65. };
  66. }
  67. ])
  68.  
  69. <div ng-if='loggedInUser'>
  70. Welcome {{loggedInUser}} | <a href="admin/pages">My Admin</a> | <a href ng-click='logout()'>Logout</a>
  71. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement