feat: testing
This commit is contained in:
parent
7a526f9983
commit
74f8ae6d84
|
@ -15,5 +15,6 @@
|
|||
<h1>Please wait...</h1>
|
||||
|
||||
<script src="js/auth.js"></script>
|
||||
<script src="js/main.js"></script>
|
||||
</body>
|
||||
</html>
|
|
@ -58,7 +58,9 @@
|
|||
</nav>
|
||||
|
||||
<main class="pageContent" id="content">
|
||||
CONTENT HERE
|
||||
<script>
|
||||
document.write(JSON.stringify(DiscordApi.getUser()));
|
||||
</script>
|
||||
</main>
|
||||
|
||||
<footer class="pageFooter container" id="footer">
|
||||
|
|
11
js/auth.js
11
js/auth.js
|
@ -10,14 +10,7 @@ if(typeof authCode !== undefined) {
|
|||
})
|
||||
.then(res => res.json())
|
||||
.then(res => {
|
||||
fetch('https://discord.com/api/v10/users/@me', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${res.access_token}`
|
||||
}
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(res => {
|
||||
console.log(res)
|
||||
})
|
||||
localStorage.setItem('token', res.access_token)
|
||||
localStorage.setItem('refresh', res.refresh_token)
|
||||
})
|
||||
}
|
33
js/main.js
33
js/main.js
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue