script_profile.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const btnLoginPage = document.querySelector('.btn-login');
  2. const btnUserProfile = document.querySelector('.btn-userProfile');
  3. const btnLogout = document.querySelector('.btn-logout');
  4. function getCookie(name) {
  5. const value = `; ${document.cookie}`;
  6. const parts = value.split(`; ${name}=`);
  7. if (parts.length === 2) return parts.pop().split(';').shift();
  8. }
  9. function renderView() {
  10. axios({
  11. method: 'get',
  12. url: 'http://www.choozmo.com:8887/user_profile',
  13. headers: {
  14. 'accept': 'text/html',
  15. }
  16. }).then(res => {
  17. console.log(res);
  18. }).catch(err => {
  19. console.log(err);
  20. })
  21. }
  22. renderView();
  23. function checkLogin() {
  24. let token = getCookie('jwt_token');
  25. if(token) {
  26. btnLoginPage.style.display = 'none';
  27. btnLogout.style.display = 'block';
  28. btnUserProfile.style.display = 'block';
  29. } else {
  30. window.location.href = 'login.html';
  31. btnLoginPage.style.display = 'block';
  32. btnLogout.style.display = 'none';
  33. btnUserProfile.style.display = 'none';
  34. }
  35. }
  36. checkLogin();