|
|
@ -1,38 +1,48 @@ |
|
|
|
import Cookies from 'js-cookie'; |
|
|
|
import Cookies from 'js-cookie'; |
|
|
|
import { TOKEN_COOKIE_NAME } from '../../lib/constants'; |
|
|
|
import { TOKEN_COOKIE_NAME } from '../../lib/constants'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
function showHideAuthElements(hideOrShow: 'hide' | 'show' = 'hide') { |
|
|
|
* Prepares the UI for the user who is logged in |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function handleLoggedOut() { |
|
|
|
|
|
|
|
document.querySelectorAll('[data-auth-required]').forEach((el) => { |
|
|
|
document.querySelectorAll('[data-auth-required]').forEach((el) => { |
|
|
|
|
|
|
|
if (hideOrShow === 'hide') { |
|
|
|
el.classList.add('hidden'); |
|
|
|
el.classList.add('hidden'); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
el.classList.remove('hidden'); |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function showHideGuestElements(hideOrShow: 'hide' | 'show' = 'hide') { |
|
|
|
document.querySelectorAll('[data-guest-required]').forEach((el) => { |
|
|
|
document.querySelectorAll('[data-guest-required]').forEach((el) => { |
|
|
|
|
|
|
|
if (hideOrShow === 'hide') { |
|
|
|
|
|
|
|
el.classList.add('hidden'); |
|
|
|
|
|
|
|
} else { |
|
|
|
el.classList.remove('hidden'); |
|
|
|
el.classList.remove('hidden'); |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Prepares the UI for the user who is logged out |
|
|
|
* Prepares the UI for the user who is logged in |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function handleLoggedIn() { |
|
|
|
function handleGuest() { |
|
|
|
document.querySelectorAll('[data-auth-required]').forEach((el) => { |
|
|
|
showHideAuthElements('hide'); |
|
|
|
el.classList.remove('hidden'); |
|
|
|
showHideGuestElements('show'); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
document.querySelectorAll('[data-guest-required]').forEach((el) => { |
|
|
|
/** |
|
|
|
el.classList.add('hidden'); |
|
|
|
* Prepares the UI for the user who is logged out |
|
|
|
}); |
|
|
|
*/ |
|
|
|
|
|
|
|
function handleAuthenticated() { |
|
|
|
|
|
|
|
showHideAuthElements('show'); |
|
|
|
|
|
|
|
showHideGuestElements('hide'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function handleAuthRequired() { |
|
|
|
function handleAuthRequired() { |
|
|
|
const token = Cookies.get(TOKEN_COOKIE_NAME); |
|
|
|
const token = Cookies.get(TOKEN_COOKIE_NAME); |
|
|
|
if (token) { |
|
|
|
if (token) { |
|
|
|
handleLoggedIn(); |
|
|
|
handleAuthenticated(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
handleLoggedOut(); |
|
|
|
handleGuest(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|