Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .controller('AdminLoginCtrl', ['$scope', '$rootScope', '$location', '$cookies', 'AuthService', 'flashMessageService', '$log',
- function($scope, $rootScope, $location, $cookies, AuthService, flashMessageService, $log) {
- // $scope.adminLoggedIn = false;
- $scope.credentials = {
- username: '',
- password: ''
- };
- $scope.login = function(credentials) {
- console.log('in login....');
- AuthService.login(credentials).then(
- function(res) {
- // THIS SHOULD BE WATCHED INSIDE DIRECTIVE TO SHOW/HIDE TEMPLATE
- $cookies.put('loggedInUser', res.data);
- // USER SAVED TO COOKIES FINE HERE
- console.log('User from Cookies...', $cookies.get("loggedInUser"));
- $location.path('/admin/pages');
- },
- function(err) {
- flashMessageService.setMessage(err.data);
- $log.log(err);
- });
- };
- }
- ])
- angular.module('myApp.directives', ["ngCookies"])
- .directive('adminLogin', ['$rootScope','$cookies','AuthService','flashMessageService','$location',
- function($rootScope,$cookies,AuthService,flashMessageService,$location) {
- return {
- controller: function($scope, $cookies,$rootScope) {
- // CONSOLE LOG IN DIRECTIVE CONFIRMS LOGGED IN USER IS SAVED IN COOKIES
- console.log("Func called in DIRECTIVE ", $cookies.get("loggedInUser"));
- $scope.loggedInUser = $cookies.get("loggedInUser");
- /* NEED HELP WITH WATCH IMPLEMENTATION */
- $scope.$watch(/* what to watch */, function(newVal, oldVal) {
- console.log('WATCH ', nVal, oVal);
- /* ??? */
- })
- console.log('logInUser In Dir ', $scope.loggedInUser);
- $scope.logout = function() {
- console.log('Logout in DIR shud b remove cook ', $cookies);
- AuthService.logout().then(
- function() {
- $cookies.remove('loggedInUser'); // Want this to be watched
- $scope.loggedInUser = false; // Works but not ideal!
- $location.path('/admin/login');
- flashMessageService.setMessage("Successfully logged out");
- }, function(err) {
- console.log('there was an error tying to logout', err);
- });
- };
- },
- templateUrl: 'partials/directives/admin-login.html'
- };
- }
- ])
- <div ng-if='loggedInUser'>
- Welcome {{loggedInUser}} | <a href="admin/pages">My Admin</a> | <a href ng-click='logout()'>Logout</a>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement