
/ user, which is not related to the application. When switching to /app/ server, the app.html file is app.html , which contains the entire head, all the scripts at the end of the body, and a single div with a modest ui-view attribute. It is in this div that the entire application is loaded. Depending on whether the user is logged in or not, they are shown different fillings of this div 'a./app/ , then the next layer is loaded inside: index.html . This file contains the static part of the application, which surrounds the entire workspace: heder, footer and sidebar. In index.html is also a div with the ui-view attribute, into which another application level will be loaded, specifically, various screens (in our case it is: main screen, detailed server screen, billing screen, backup restore screen and others) .
app.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) { $stateProvider .state('index', { url: '/', templateUrl: '/static/views/index.html' }) .state('index.main', { url: '^/main', templateUrl: '/static/views/pages/main.html' }) .state('index.client', { url: '^/main/c/:id', templateUrl: '/static/views/pages/client.html' }) .state('index.billing', { url: '^/billing', templateUrl: '/static/views/pages/billing.html' }) .state('index.restore', { url: '^/restore', templateUrl: '/static/views/pages/restore.html' }); $urlRouterProvider.otherwise('/main'); // , /main }]) $stateProvider .state('login', { url: '/login', templateUrl: '/static/views/login.html' }) .state('signup', { url: '/signup', templateUrl: '/static/views/signup.html' }) .state('recovery', { url: '/recovery', templateUrl: '/static/views/recovery.html' }); AuthModule.factory('Auth', ['$cookieStore', function ($cookieStore) { var currentUser = $cookieStore.get('login') || 0, publicStates = ['login', 'signup', 'recovery']; return { authorize: function(state) { return (this.isLoggedIn() && (publicStates.indexOf(state) < 0)) || (!this.isLoggedIn() && (publicStates.indexOf(state) >= 0)) }, isLoggedIn: function() { return !!currentUser; } } }]) isLoggedIn method returns true if the user is logged in, or false otherwise. The authorize method determines for the current state whether the user has the right to be in it.$stateChangeStart event handler, which occurs when the state changes start: $rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) { // if (!Auth.authorize(toState.name)) { // event.preventDefault(); // ( /app/ - ) if (fromState.url === '^') { if (Auth.isLoggedIn()) { $state.go('index.main'); } else { $state.go('auth'); } } } }); Auth factory: login: function (user, success, error) { $http.post('/login/', user) .success(function () { currentUser = 1; success(); }) .error(error); } username , password and callbacks: Auth.login({ username: $scope.login.username, password: $scope.login.password }, function () { $state.go('index.main'); }, function () { $scope.login.error = true; }); django.contrib.auth . from django.contrib.auth import authenticate, login def login_service(request): data = json.loads(request.body) user = authenticate(username=data['username'], password=data['password']) if user is not None: login(request, user) return HttpResponse(status=200) else: return HttpResponse('Login error', status=401) $cookieStore.get('login') .Source: https://habr.com/ru/post/239479/
All Articles