Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- //Define Routing for app
- angular.module('myApp', []).config(['$routeProvider', '$locationProvider',
- function($routeProvider,$locationProvider) {
- $routeProvider
- .when('/login', {
- templateUrl: 'login.html',
- controller: 'LoginController'
- })
- .when('/register', {
- templateUrl: 'register.html',
- controller: 'RegisterController'
- })
- .when('/forgotPassword', {
- templateUrl: 'forgotpassword.html',
- controller: 'forgotController'
- })
- .when('/home', {
- templateUrl: 'views/home.html',
- controller: 'homeController'
- })
- .otherwise({
- redirectTo: '/login'
- });
- // $locationProvider.html5Mode(true); //Remove the '#' from URL.
- }]);
- angular.module('myApp').factory("page", function($rootScope){
- var page={};
- var user={};
- page.setPage=function(title,bodyClass){
- $rootScope.pageTitle = title;
- $rootScope.bodylayout=bodyClass;
- };
- page.setUser=function(user){
- $rootScope.user=user;
- }
- return page;
- });
- 'use strict';
- angular.module('myApp').controller('LoginController', function($scope, $location, $window,page) {
- page.setPage("Login","login-layout");
- $scope.user = {};
- $scope.loginUser=function()
- {
- var username=$scope.user.name;
- var password=$scope.user.password;
- if(username=="admin" && password=="admin123")
- {
- page.setUser($scope.user);
- $location.path( "/home" );
- }
- else
- {
- $scope.message="Error";
- $scope.messagecolor="alert alert-danger";
- }
- }
- });
- <span class="user-info">
- <small>Welcome,</small>
- {{user.name}}
- </span>
- <span class="logout"><a href="" ng-click="logoutUser()">Logout</a></span>
- app.run(['$rootScope', '$location', 'Auth', function ($rootScope, $location, Auth) {
- $rootScope.$on('$routeChangeStart', function (event) {
- if (!Auth.isLoggedIn()) {
- console.log('DENY');
- event.preventDefault();
- $location.path('/login');
- }
- else {
- console.log('ALLOW');
- $location.path('/home');
- }
- });
- }]);
- .factory('Auth', function(){
- var user;
- return{
- setUser : function(aUser){
- user = aUser;
- },
- isLoggedIn : function(){
- return(user)? user : false;
- }
- }
- })
- .controller('loginCtrl', [ '$scope', 'Auth', function ($scope, Auth) {
- //submit
- $scope.login = function () {
- // Ask to the server, do your job and THEN set the user
- Auth.setUser(user); //Update the state of the user in the app
- };
- }])
- .controller('mainCtrl', ['$scope', 'Auth', '$location', function ($scope, Auth, $location) {
- $scope.$watch(Auth.isLoggedIn, function (value, oldValue) {
- if(!value && oldValue) {
- console.log("Disconnect");
- $location.path('/login');
- }
- if(value) {
- console.log("Connect");
- //Do something when the user is connected
- }
- }, true);
- angular.module('yourApp').config(['$routeProvider', function($routeProvider) {
- $routeProvider
- .when('/home', {
- templateUrl: 'templates/home.html',
- requireAuth: true
- })
- .when('/login', {
- templateUrl: 'templates/login.html',
- })
- .otherwise({
- redirectTo: '/home'
- });
- }])
- angular.module('YourApp').controller('YourController', ['$scope', 'session', '$location',
- function($scope, session, $location) {
- $scope.$on('$routeChangeStart', function(angularEvent, newUrl) {
- if (newUrl.requireAuth && !session.user) {
- // User isn’t authenticated
- $location.path("/login");
- }
- });
- }
- ]);
- (function () {
- 'use strict';
- angular
- .module('app', ['ngRoute', 'ngCookies'])
- .config(config)
- .run(run);
- config.$inject = ['$routeProvider', '$locationProvider'];
- function config($routeProvider, $locationProvider) {
- $routeProvider
- .when('/', {
- controller: 'HomeController',
- templateUrl: 'home/home.view.html',
- controllerAs: 'vm'
- })
- .when('/login', {
- controller: 'LoginController',
- templateUrl: 'login/login.view.html',
- controllerAs: 'vm'
- })
- .when('/register', {
- controller: 'RegisterController',
- templateUrl: 'register/register.view.html',
- controllerAs: 'vm'
- })
- .otherwise({ redirectTo: '/login' });
- }
- run.$inject = ['$rootScope', '$location', '$cookieStore', '$http'];
- function run($rootScope, $location, $cookieStore, $http) {
- // keep user logged in after page refresh
- $rootScope.globals = $cookieStore.get('globals') || {};
- if ($rootScope.globals.currentUser) {
- $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata; // jshint ignore:line
- }
- $rootScope.$on('$locationChangeStart', function (event, next, current) {
- // redirect to login page if not logged in and trying to access a restricted page
- var restrictedPage = $.inArray($location.path(), ['/login', '/register']) === -1;
- var loggedIn = $rootScope.globals.currentUser;
- if (restrictedPage && !loggedIn) {
- $location.path('/login');
- }
- });
- }
- })();
- allowAnonymous: true
- $stateProvider.state('login', {
- url: '/login',
- allowAnonymous: true, //if you move this, don't forget to update
- //variable path in the force-page check.
- views: {
- root: {
- templateUrl: "app/auth/login/login.html",
- controller: 'LoginCtrl'
- }
- }
- //Any other config
- }
- //I put it right after the main app module config. I.e. This thing:
- angular.module('app', [ /* your dependencies*/ ])
- .config(function (/* you injections */) { /* your config */ })
- //Make sure there's no ';' ending the previous line. We're chaining. (or just use a variable)
- //
- //Then force the logon page
- .run(function ($rootScope, $state, $location, User /* My custom session obj */) {
- $rootScope.$on('$stateChangeStart', function(event, newState) {
- if (!User.authenticated && newState.allowAnonymous != true) {
- //Don't use: $state.go('login');
- //Apparently you can't set the $state while in a $state event.
- //It doesn't work properly. So we use the other way.
- $location.path("/login");
- }
- });
- });
- angular.module("app").config(['$routeProvider',
- function ($routeProvider) {
- $routeProvider.
- when('/ap', {
- templateUrl: 'template1.html',
- controller: 'template1',
- isAp: 'ap',
- }).
- when('/auc', {
- templateUrl: 'template2.html',
- controller: 'template2',
- isAp: 'common',
- }).
- when('/ic', {
- templateUrl: 'template3.html',
- controller: 'template3',
- isAp: 'auc',
- }).
- when('/mup', {
- templateUrl: 'template4.html',
- controller: 'template4',
- isAp: 'ap',
- }).
- when('/mnu', {
- templateUrl: 'template5.html',
- controller: 'template5',
- isAp: 'common',
- }).
- otherwise({
- redirectTo: '/ap',
- });
- }]);
- .run(['$rootScope', '$location', function ($rootScope, $location) {
- $rootScope.$on("$routeChangeStart", function (event, next, current) {
- if (next.$$route.isAp != 'common') {
- if ($rootScope.userTypeGlobal == 1) {
- if (next.$$route.isAp != 'ap') {
- $location.path("/ap");
- }
- }
- else {
- if (next.$$route.isAp != 'auc') {
- $location.path("/auc");
- }
- }
- }
- });
- }]);
- Note :- To Save user's data you may use `resolve` feature of `ui-router`.
- Check cookie if it exist load template , if even cookies doesn't exist than
- there is no chance of logged in , simply redirect to login template/page.
- <?php/ANY_LANGUAGE
- session_start();//You may use your language specific function if required
- if(isset($_SESSION["logged_in"])){
- set_header("200 OK");//this is not right syntax , it is just to hint
- }
- else{
- set_header("-1 NOT LOGGED_IN");//you may set any code but compare that same
- //code on client side to check if user is logged in or not.
- }
- //thanks.....
- $http.get(dataUrl)
- .success(function (data){
- $scope.templateData = data;
- })
- .error(function (error, status){
- $scope.data.error = { message: error, status: status};
- console.log($scope.data.error.status);
- if(status == CODE_CONFIGURED_ON_SERVER_SIDE_FOR_NON_LOGGED_IN){
- //redirect to login
- });
- 'use strict';
- // Declare app level module which depends on filters, and services
- var app= angular.module('myApp', ['ngRoute','angularUtils.directives.dirPagination','ngLoadingSpinner']);
- app.config(['$routeProvider', function($routeProvider) {
- $routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'loginCtrl'});
- $routeProvider.when('/home', {templateUrl: 'partials/home.html', controller: 'homeCtrl'});
- $routeProvider.when('/salesnew', {templateUrl: 'partials/salesnew.html', controller: 'salesnewCtrl'});
- $routeProvider.when('/salesview', {templateUrl: 'partials/salesview.html', controller: 'salesviewCtrl'});
- $routeProvider.when('/users', {templateUrl: 'partials/users.html', controller: 'usersCtrl'});
- $routeProvider.when('/forgot', {templateUrl: 'partials/forgot.html', controller: 'forgotCtrl'});
- $routeProvider.otherwise({redirectTo: '/login'});
- }]);
- app.run(function($rootScope, $location, loginService){
- var routespermission=['/home']; //route that require login
- var salesnew=['/salesnew'];
- var salesview=['/salesview'];
- var users=['/users'];
- $rootScope.$on('$routeChangeStart', function(){
- if( routespermission.indexOf($location.path()) !=-1
- || salesview.indexOf($location.path()) !=-1
- || salesnew.indexOf($location.path()) !=-1
- || users.indexOf($location.path()) !=-1)
- {
- var connected=loginService.islogged();
- connected.then(function(msg){
- if(!msg.data)
- {
- $location.path('/login');
- }
- });
- }
- });
- });
- 'use strict';
- app.factory('loginService',function($http, $location, sessionService){
- return{
- login:function(data,scope){
- var $promise=$http.post('data/user.php',data); //send data to user.php
- $promise.then(function(msg){
- var uid=msg.data;
- if(uid){
- scope.msgtxt='Correct information';
- sessionService.set('uid',uid);
- $location.path('/home');
- }
- else {
- scope.msgtxt='incorrect information';
- $location.path('/login');
- }
- });
- },
- logout:function(){
- sessionService.destroy('uid');
- $location.path('/login');
- },
- islogged:function(){
- var $checkSessionServer=$http.post('data/check_session.php');
- return $checkSessionServer;
- /*
- if(sessionService.get('user')) return true;
- else return false;
- */
- }
- }
- });
- 'use strict';
- app.factory('sessionService', ['$http', function($http){
- return{
- set:function(key,value){
- return sessionStorage.setItem(key,value);
- },
- get:function(key){
- return sessionStorage.getItem(key);
- },
- destroy:function(key){
- $http.post('data/destroy_session.php');
- return sessionStorage.removeItem(key);
- }
- };
- }])
- 'use strict';
- app.controller('loginCtrl', ['$scope','loginService', function ($scope,loginService) {
- $scope.msgtxt='';
- $scope.login=function(data){
- loginService.login(data,$scope); //call login service
- };
- }]);
- angular.module('app',[])
- .config(function($routeProvider)
- {
- $routeProvider
- .when('/', {
- templateUrl : 'app/views/login.html',
- controller : 'YourController',
- controllerAs : 'Your',
- resolve: {
- factory : checkLoginRedirect
- }
- })
- }
- function checkLoginRedirect($location){
- var user = firebase.auth().currentUser;
- if (user) {
- // User is signed in.
- if ($location.path() == "/"){
- $location.path('dash');
- }
- return true;
- }else{
- // No user is signed in.
- $location.path('/');
- return false;
- }
- }
- .run(function(){
- firebase.auth().onAuthStateChanged(function(user) {
- if (user) {
- console.log('User is signed in.');
- } else {
- console.log('No user is signed in.');
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement