Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.66 KB | None | 0 0
  1. 'use strict';
  2. //Define Routing for app
  3. angular.module('myApp', []).config(['$routeProvider', '$locationProvider',
  4. function($routeProvider,$locationProvider) {
  5. $routeProvider
  6. .when('/login', {
  7. templateUrl: 'login.html',
  8. controller: 'LoginController'
  9. })
  10. .when('/register', {
  11. templateUrl: 'register.html',
  12. controller: 'RegisterController'
  13. })
  14. .when('/forgotPassword', {
  15. templateUrl: 'forgotpassword.html',
  16. controller: 'forgotController'
  17. })
  18. .when('/home', {
  19. templateUrl: 'views/home.html',
  20. controller: 'homeController'
  21. })
  22. .otherwise({
  23. redirectTo: '/login'
  24. });
  25. // $locationProvider.html5Mode(true); //Remove the '#' from URL.
  26. }]);
  27.  
  28. angular.module('myApp').factory("page", function($rootScope){
  29. var page={};
  30. var user={};
  31. page.setPage=function(title,bodyClass){
  32. $rootScope.pageTitle = title;
  33. $rootScope.bodylayout=bodyClass;
  34. };
  35. page.setUser=function(user){
  36. $rootScope.user=user;
  37. }
  38. return page;
  39. });
  40.  
  41. 'use strict';
  42.  
  43. angular.module('myApp').controller('LoginController', function($scope, $location, $window,page) {
  44. page.setPage("Login","login-layout");
  45. $scope.user = {};
  46. $scope.loginUser=function()
  47. {
  48. var username=$scope.user.name;
  49. var password=$scope.user.password;
  50. if(username=="admin" && password=="admin123")
  51. {
  52. page.setUser($scope.user);
  53. $location.path( "/home" );
  54. }
  55. else
  56. {
  57. $scope.message="Error";
  58. $scope.messagecolor="alert alert-danger";
  59. }
  60. }
  61. });
  62.  
  63. <span class="user-info">
  64. <small>Welcome,</small>
  65. {{user.name}}
  66. </span>
  67. <span class="logout"><a href="" ng-click="logoutUser()">Logout</a></span>
  68.  
  69. app.run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) {
  70. $rootScope.$on('$routeChangeStart', function (event) {
  71.  
  72. if (!Auth.isLoggedIn()) {
  73. console.log('DENY');
  74. event.preventDefault();
  75. $location.path('/login');
  76. }
  77. else {
  78. console.log('ALLOW');
  79. $location.path('/home');
  80. }
  81. });
  82. }]);
  83.  
  84. .factory('Auth', function(){
  85. var user;
  86.  
  87. return{
  88. setUser : function(aUser){
  89. user = aUser;
  90. },
  91. isLoggedIn : function(){
  92. return(user)? user : false;
  93. }
  94. }
  95. })
  96.  
  97. .controller('loginCtrl', [ '$scope', 'Auth', function ($scope, Auth) {
  98. //submit
  99. $scope.login = function () {
  100. // Ask to the server, do your job and THEN set the user
  101.  
  102. Auth.setUser(user); //Update the state of the user in the app
  103. };
  104. }])
  105.  
  106. .controller('mainCtrl', ['$scope', 'Auth', '$location', function ($scope, Auth, $location) {
  107.  
  108. $scope.$watch(Auth.isLoggedIn, function (value, oldValue) {
  109.  
  110. if(!value && oldValue) {
  111. console.log("Disconnect");
  112. $location.path('/login');
  113. }
  114.  
  115. if(value) {
  116. console.log("Connect");
  117. //Do something when the user is connected
  118. }
  119.  
  120. }, true);
  121.  
  122. angular.module('yourApp').config(['$routeProvider', function($routeProvider) {
  123. $routeProvider
  124. .when('/home', {
  125. templateUrl: 'templates/home.html',
  126. requireAuth: true
  127. })
  128. .when('/login', {
  129. templateUrl: 'templates/login.html',
  130. })
  131. .otherwise({
  132. redirectTo: '/home'
  133. });
  134. }])
  135.  
  136. angular.module('YourApp').controller('YourController', ['$scope', 'session', '$location',
  137. function($scope, session, $location) {
  138.  
  139. $scope.$on('$routeChangeStart', function(angularEvent, newUrl) {
  140.  
  141. if (newUrl.requireAuth && !session.user) {
  142. // User isn’t authenticated
  143. $location.path("/login");
  144. }
  145.  
  146. });
  147. }
  148. ]);
  149.  
  150. (function () {
  151.     'use strict';
  152.  
  153.     angular
  154.         .module('app', ['ngRoute', 'ngCookies'])
  155.         .config(config)
  156.         .run(run);
  157.  
  158.     config.$inject = ['$routeProvider', '$locationProvider'];
  159.     function config($routeProvider, $locationProvider) {
  160.         $routeProvider
  161.             .when('/', {
  162.                 controller: 'HomeController',
  163.                 templateUrl: 'home/home.view.html',
  164.                 controllerAs: 'vm'
  165.             })
  166.  
  167.             .when('/login', {
  168.                 controller: 'LoginController',
  169.                 templateUrl: 'login/login.view.html',
  170.                 controllerAs: 'vm'
  171.             })
  172.  
  173.             .when('/register', {
  174.                 controller: 'RegisterController',
  175.                 templateUrl: 'register/register.view.html',
  176.                 controllerAs: 'vm'
  177.             })
  178.  
  179.             .otherwise({ redirectTo: '/login' });
  180.     }
  181.  
  182.     run.$inject = ['$rootScope', '$location', '$cookieStore', '$http'];
  183.     function run($rootScope, $location, $cookieStore, $http) {
  184.         // keep user logged in after page refresh
  185.         $rootScope.globals = $cookieStore.get('globals') || {};
  186.         if ($rootScope.globals.currentUser) {
  187.             $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata; // jshint ignore:line
  188.         }
  189.  
  190.         $rootScope.$on('$locationChangeStart', function (event, next, current) {
  191.             // redirect to login page if not logged in and trying to access a restricted page
  192.             var restrictedPage = $.inArray($location.path(), ['/login', '/register']) === -1;
  193.             var loggedIn = $rootScope.globals.currentUser;
  194.             if (restrictedPage && !loggedIn) {
  195.                 $location.path('/login');
  196.             }
  197.         });
  198.     }
  199.  
  200. })();
  201.  
  202. allowAnonymous: true
  203.  
  204. $stateProvider.state('login', {
  205. url: '/login',
  206. allowAnonymous: true, //if you move this, don't forget to update
  207. //variable path in the force-page check.
  208. views: {
  209. root: {
  210. templateUrl: "app/auth/login/login.html",
  211. controller: 'LoginCtrl'
  212. }
  213. }
  214. //Any other config
  215. }
  216.  
  217. //I put it right after the main app module config. I.e. This thing:
  218. angular.module('app', [ /* your dependencies*/ ])
  219. .config(function (/* you injections */) { /* your config */ })
  220.  
  221. //Make sure there's no ';' ending the previous line. We're chaining. (or just use a variable)
  222. //
  223. //Then force the logon page
  224. .run(function ($rootScope, $state, $location, User /* My custom session obj */) {
  225. $rootScope.$on('$stateChangeStart', function(event, newState) {
  226. if (!User.authenticated && newState.allowAnonymous != true) {
  227. //Don't use: $state.go('login');
  228. //Apparently you can't set the $state while in a $state event.
  229. //It doesn't work properly. So we use the other way.
  230. $location.path("/login");
  231. }
  232. });
  233. });
  234.  
  235. angular.module("app").config(['$routeProvider',
  236. function ($routeProvider) {
  237.  
  238. $routeProvider.
  239. when('/ap', {
  240. templateUrl: 'template1.html',
  241. controller: 'template1',
  242. isAp: 'ap',
  243. }).
  244. when('/auc', {
  245. templateUrl: 'template2.html',
  246. controller: 'template2',
  247. isAp: 'common',
  248. }).
  249. when('/ic', {
  250. templateUrl: 'template3.html',
  251. controller: 'template3',
  252. isAp: 'auc',
  253. }).
  254. when('/mup', {
  255. templateUrl: 'template4.html',
  256. controller: 'template4',
  257. isAp: 'ap',
  258. }).
  259.  
  260. when('/mnu', {
  261. templateUrl: 'template5.html',
  262. controller: 'template5',
  263. isAp: 'common',
  264. }).
  265. otherwise({
  266. redirectTo: '/ap',
  267. });
  268. }]);
  269.  
  270. .run(['$rootScope', '$location', function ($rootScope, $location) {
  271. $rootScope.$on("$routeChangeStart", function (event, next, current) {
  272. if (next.$$route.isAp != 'common') {
  273. if ($rootScope.userTypeGlobal == 1) {
  274. if (next.$$route.isAp != 'ap') {
  275. $location.path("/ap");
  276. }
  277. }
  278. else {
  279. if (next.$$route.isAp != 'auc') {
  280. $location.path("/auc");
  281. }
  282. }
  283. }
  284.  
  285. });
  286. }]);
  287.  
  288. Note :- To Save user's data you may use `resolve` feature of `ui-router`.
  289. Check cookie if it exist load template , if even cookies doesn't exist than
  290. there is no chance of logged in , simply redirect to login template/page.
  291.  
  292. <?php/ANY_LANGUAGE
  293. session_start();//You may use your language specific function if required
  294. if(isset($_SESSION["logged_in"])){
  295. set_header("200 OK");//this is not right syntax , it is just to hint
  296. }
  297. else{
  298. set_header("-1 NOT LOGGED_IN");//you may set any code but compare that same
  299. //code on client side to check if user is logged in or not.
  300. }
  301. //thanks.....
  302.  
  303. $http.get(dataUrl)
  304. .success(function (data){
  305. $scope.templateData = data;
  306. })
  307. .error(function (error, status){
  308. $scope.data.error = { message: error, status: status};
  309. console.log($scope.data.error.status);
  310. if(status == CODE_CONFIGURED_ON_SERVER_SIDE_FOR_NON_LOGGED_IN){
  311. //redirect to login
  312. });
  313.  
  314. 'use strict';
  315. // Declare app level module which depends on filters, and services
  316. var app= angular.module('myApp', ['ngRoute','angularUtils.directives.dirPagination','ngLoadingSpinner']);
  317. app.config(['$routeProvider', function($routeProvider) {
  318. $routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'loginCtrl'});
  319. $routeProvider.when('/home', {templateUrl: 'partials/home.html', controller: 'homeCtrl'});
  320. $routeProvider.when('/salesnew', {templateUrl: 'partials/salesnew.html', controller: 'salesnewCtrl'});
  321. $routeProvider.when('/salesview', {templateUrl: 'partials/salesview.html', controller: 'salesviewCtrl'});
  322. $routeProvider.when('/users', {templateUrl: 'partials/users.html', controller: 'usersCtrl'});
  323. $routeProvider.when('/forgot', {templateUrl: 'partials/forgot.html', controller: 'forgotCtrl'});
  324.  
  325.  
  326. $routeProvider.otherwise({redirectTo: '/login'});
  327.  
  328.  
  329. }]);
  330.  
  331.  
  332. app.run(function($rootScope, $location, loginService){
  333. var routespermission=['/home']; //route that require login
  334. var salesnew=['/salesnew'];
  335. var salesview=['/salesview'];
  336. var users=['/users'];
  337. $rootScope.$on('$routeChangeStart', function(){
  338. if( routespermission.indexOf($location.path()) !=-1
  339. || salesview.indexOf($location.path()) !=-1
  340. || salesnew.indexOf($location.path()) !=-1
  341. || users.indexOf($location.path()) !=-1)
  342. {
  343. var connected=loginService.islogged();
  344. connected.then(function(msg){
  345. if(!msg.data)
  346. {
  347. $location.path('/login');
  348. }
  349.  
  350. });
  351. }
  352. });
  353. });
  354.  
  355. 'use strict';
  356. app.factory('loginService',function($http, $location, sessionService){
  357. return{
  358. login:function(data,scope){
  359. var $promise=$http.post('data/user.php',data); //send data to user.php
  360. $promise.then(function(msg){
  361. var uid=msg.data;
  362. if(uid){
  363. scope.msgtxt='Correct information';
  364. sessionService.set('uid',uid);
  365. $location.path('/home');
  366. }
  367. else {
  368. scope.msgtxt='incorrect information';
  369. $location.path('/login');
  370. }
  371. });
  372. },
  373. logout:function(){
  374. sessionService.destroy('uid');
  375. $location.path('/login');
  376. },
  377. islogged:function(){
  378. var $checkSessionServer=$http.post('data/check_session.php');
  379. return $checkSessionServer;
  380. /*
  381. if(sessionService.get('user')) return true;
  382. else return false;
  383. */
  384. }
  385. }
  386.  
  387. });
  388.  
  389. 'use strict';
  390.  
  391. app.factory('sessionService', ['$http', function($http){
  392. return{
  393. set:function(key,value){
  394. return sessionStorage.setItem(key,value);
  395. },
  396. get:function(key){
  397. return sessionStorage.getItem(key);
  398. },
  399. destroy:function(key){
  400. $http.post('data/destroy_session.php');
  401. return sessionStorage.removeItem(key);
  402. }
  403. };
  404. }])
  405.  
  406. 'use strict';
  407.  
  408. app.controller('loginCtrl', ['$scope','loginService', function ($scope,loginService) {
  409. $scope.msgtxt='';
  410. $scope.login=function(data){
  411. loginService.login(data,$scope); //call login service
  412. };
  413.  
  414. }]);
  415.  
  416. angular.module('app',[])
  417. .config(function($routeProvider)
  418. {
  419. $routeProvider
  420. .when('/', {
  421. templateUrl : 'app/views/login.html',
  422. controller : 'YourController',
  423. controllerAs : 'Your',
  424. resolve: {
  425. factory : checkLoginRedirect
  426. }
  427. })
  428. }
  429.  
  430. function checkLoginRedirect($location){
  431.  
  432. var user = firebase.auth().currentUser;
  433.  
  434. if (user) {
  435. // User is signed in.
  436. if ($location.path() == "/"){
  437. $location.path('dash');
  438. }
  439.  
  440. return true;
  441. }else{
  442. // No user is signed in.
  443. $location.path('/');
  444. return false;
  445. }
  446. }
  447.  
  448. .run(function(){
  449.  
  450. firebase.auth().onAuthStateChanged(function(user) {
  451. if (user) {
  452. console.log('User is signed in.');
  453. } else {
  454. console.log('No user is signed in.');
  455. }
  456. });
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement