forked from spacebar/SplatNet
feat: implement logging in & out of twitter using header
This commit is contained in:
parent
67933ec76a
commit
ca0cc9db44
5 changed files with 209 additions and 10 deletions
|
|
@ -9,6 +9,55 @@ window.loadHeader(async function(headerContainer) {
|
|||
});
|
||||
}
|
||||
|
||||
const twitterBtn = document.getElementById('twitter-link-btn');
|
||||
const twitterLabel = document.getElementById('twitter-label-img');
|
||||
|
||||
const handleTwitterAction = () => {
|
||||
const isLinked = twitterBtn.dataset.linked === "true";
|
||||
if (isLinked) {
|
||||
if (!confirm("Unlink Twitter account?")) return;
|
||||
fetch("/api/v1/me/twitter/unlink", { method: "POST", credentials: 'include' })
|
||||
.then(() => window.location.reload());
|
||||
} else {
|
||||
twitterBtn.style.opacity = "0.5";
|
||||
twitterBtn.style.pointerEvents = "none";
|
||||
fetch("/api/v1/me/twitter/link", { credentials: 'include' })
|
||||
.then(res => res.status === 401 ? (window.location.href = "/sign_in/") : res.json())
|
||||
.then(data => data?.url ? (window.location.href = data.url) : window.location.reload())
|
||||
.catch(() => window.location.reload());
|
||||
}
|
||||
};
|
||||
|
||||
if (twitterBtn) {
|
||||
twitterBtn.addEventListener('click', handleTwitterAction);
|
||||
}
|
||||
|
||||
const updateTwitterUI = () => {
|
||||
if (!twitterBtn || !twitterLabel) return;
|
||||
fetch("/api/v1/me/twitter/status", { credentials: 'include' })
|
||||
.then(res => res.ok ? res.json() : null)
|
||||
.then(data => {
|
||||
if (data && data.is_linked) {
|
||||
twitterBtn.dataset.linked = "true";
|
||||
twitterLabel.src = "/assets/en/svg/text/menu/tx_twitter_Cancel-34186e4e830964405f2528210d7278206d255bf40b69559babc36a7a041e0394.svg";
|
||||
twitterLabel.alt = "Unlink Twitter";
|
||||
twitterLabel.style.scale = "1.6";
|
||||
twitterLabel.style.marginLeft = "30px";
|
||||
twitterLabel.style.translate = "-10px -2px";
|
||||
} else {
|
||||
twitterBtn.dataset.linked = "false";
|
||||
twitterLabel.src = "/assets/en/svg/text/menu/tx_twitter-47ce083cc4514ab6aeb75b7dd9f71ce89ddc54a18d930d90cf4df62047413831.svg";
|
||||
twitterLabel.alt = "Link Twitter";
|
||||
twitterLabel.style.scale = "";
|
||||
twitterLabel.style.marginLeft = "";
|
||||
twitterLabel.style.translate = "";
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
twitterBtn.dataset.linked = "false";
|
||||
});
|
||||
};
|
||||
|
||||
const extractMiiName = (miiDataB64) => {
|
||||
try {
|
||||
const binaryString = atob(miiDataB64.replace(/-/g, '+').replace(/_/g, '/'));
|
||||
|
|
@ -222,6 +271,7 @@ window.loadHeader(async function(headerContainer) {
|
|||
</div>
|
||||
`;
|
||||
}
|
||||
updateTwitterUI();
|
||||
};
|
||||
|
||||
const init = async () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue