script_profile.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. 'Authorization' : `Bearer ${getCookie('jwt_token')}`
  16. }
  17. }).then(res => {
  18. console.log(res);
  19. }).catch(err => {
  20. console.log(err);
  21. })
  22. }
  23. renderView();
  24. function checkLogin() {
  25. let token = getCookie('jwt_token');
  26. if(token) {
  27. btnLoginPage.style.display = 'none';
  28. btnLogout.style.display = 'block';
  29. btnUserProfile.style.display = 'block';
  30. } else {
  31. window.location.href = 'login.html';
  32. btnLoginPage.style.display = 'block';
  33. btnLogout.style.display = 'none';
  34. btnUserProfile.style.display = 'none';
  35. }
  36. }
  37. checkLogin();