/
nalsur
/
winter
Обзор
Документация
Войти
/
nalsur
/
winter
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/main/resources/static/index.js
92 строки
3 KB
Ruslan Bulatov
Правки по второму уроку
12 окт 2024, 19:51
12 окт 2024, 19:51
7eb1a4f
Код
Авторство
О чём код?
angular.module('app', ['ngStorage']).controller('indexController', function($scope, $http, $localStorage) { const contextPath = 'http://localhost:8189/winter'; $scope.tryToAuth = function () { $http.post(contextPath + '/auth', $scope.user) .then(function successCallback(response) { if (response.data.token) { $http.defaults.headers.common.Authorization = 'Bearer ' + response.data.token; $localStorage.winterMArketUser = {username: $scope.user.username, token: response.data.token}; $scope.user.username = null; $scope.user.password = null; } }, function errorCallback(response){ }); } $scope.tryToLogout = function () { $scope.clearUser(); $scope.user = null; } $scope.clearUser = function () { delete $localStorage.winterMArketUser; $http.defaults.headers.common.Authorization = ''; } $scope.isUserLoggedIn = function () { if ($localStorage.winterMArketUser){ return true; } else { return false; } } $scope.checkAuth = function () { $http.get(contextPath + '/auth_check').then( function (response) { alert(response.data.value); }); } if ($localStorage.winterMArketUser){ try { let jwt = $localStorage.winterMArketUser.token; let payload = JSON.parse(atob(jwt.split('.')[1])) let currentTime = parseInt(new Date().getTime() / 1000); if (currentTime > payload.exp) { console.log("Token is expired!!!"); delete $localStorage.winterMArketUser; $http.defaults.headers.common.Authorization = ''; } } catch (e){ } $http.defaults.headers.common.Authorization = 'Bearer ' + $localStorage.winterMArketUser.token; } $scope.loadProducts = function () { $http.get(contextPath + '/api/v1/products').then( function (response) { $scope.productsList = response.data; }); } $scope.clearCart = function(){ $http.get(contextPath + '/api/v1/cart/clear').then( function (response) { $scope.loadCart(); }); } $scope.removeFromCart = function(productId){ $http.get(contextPath + '/api/v1/cart/remove/' + productId).then( function (response) { $scope.loadCart(); }); } $scope.addProductToCart = function(productId){ $http.get(contextPath + '/api/v1/cart/add/' + productId).then( function (response) { $scope.loadCart(); }); } $scope.loadCart = function () { $http.get(contextPath + '/api/v1/cart').then( function (response) { $scope.cart = response.data; }); } $scope.loadProducts(); $scope.loadCart(); });