feat: testing

This commit is contained in:
TheClashFruit 2023-08-24 16:38:04 +02:00
parent 7a526f9983
commit 74f8ae6d84
Signed by: TheClashFruit
GPG key ID: CF4A319B9A73290C
4 changed files with 39 additions and 10 deletions

View file

@ -15,5 +15,6 @@
<h1>Please wait...</h1> <h1>Please wait...</h1>
<script src="js/auth.js"></script> <script src="js/auth.js"></script>
<script src="js/main.js"></script>
</body> </body>
</html> </html>

View file

@ -58,7 +58,9 @@
</nav> </nav>
<main class="pageContent" id="content"> <main class="pageContent" id="content">
CONTENT HERE <script>
document.write(JSON.stringify(DiscordApi.getUser()));
</script>
</main> </main>
<footer class="pageFooter container" id="footer"> <footer class="pageFooter container" id="footer">

View file

@ -10,14 +10,7 @@ if(typeof authCode !== undefined) {
}) })
.then(res => res.json()) .then(res => res.json())
.then(res => { .then(res => {
fetch('https://discord.com/api/v10/users/@me', { localStorage.setItem('token', res.access_token)
headers: { localStorage.setItem('refresh', res.refresh_token)
'Authorization': `Bearer ${res.access_token}`
}
})
.then(res => res.json())
.then(res => {
console.log(res)
})
}) })
} }

View file

@ -0,0 +1,33 @@
const DiscordApi = {
getUser: async () => {
if (typeof localStorage.getItem('token') === undefined)
return null;
const req = await fetch('https://discord.com/api/v10/users/@me', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
});
DiscordApi.refreshToken();
return await req.json();
},
refreshToken: () => {
if (typeof localStorage.getItem('refresh') === undefined)
return null;
fetch('https://crss-api.theclashfruit.workers.dev/refresh', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ authCode: localStorage.getItem('refresh') })
})
.then(res => res.json())
.then(res => {
localStorage.setItem('token', res.access_token)
localStorage.setItem('refresh', res.refresh_token)
})
}
}