mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-06 15:15:37 +01:00
Update
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
.authorize__container {
|
.flex__container__center {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
@@ -14,3 +14,16 @@ a.authorize__twitch_btn {
|
|||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
font-size: 1.5rem;
|
font-size: 1.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.settings__container {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.settings__header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: end;
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
{
|
{
|
||||||
"imports": {
|
"imports": {
|
||||||
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.prod.js",
|
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.prod.js",
|
||||||
"vue-router": "https://unpkg.com/vue-router@4.5.0/dist/vue-router.esm-browser.prod.js"
|
"vue-router": "https://unpkg.com/vue-router@4.5.0/dist/vue-router.esm-browser.prod.js",
|
||||||
|
"jwt-decode": "https://www.unpkg.com/jwt-decode@4.0.0/build/esm/index.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,6 +1,48 @@
|
|||||||
import { createApp, ref, onMounted } from 'vue';
|
import { createApp, ref, onMounted } from 'vue';
|
||||||
import { createRouter, createWebHistory, RouterView, useRouter } from 'vue-router';
|
import { createRouter, createWebHistory, RouterView, useRouter } from 'vue-router';
|
||||||
|
|
||||||
|
import { jwtDecode } from "jwt-decode";
|
||||||
|
|
||||||
|
|
||||||
|
class TokenManager {
|
||||||
|
static TOKEN_KEY = "token";
|
||||||
|
|
||||||
|
static getToken() {
|
||||||
|
return localStorage.getItem(this.TOKEN_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
static getAndValidate() {
|
||||||
|
const token = this.getToken();
|
||||||
|
|
||||||
|
if (token === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let decoded;
|
||||||
|
|
||||||
|
try {
|
||||||
|
decoded = jwtDecode(token);
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (decoded.exp < Date.now() / 1000) {
|
||||||
|
this.removeToken();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
static setToken(token) {
|
||||||
|
localStorage.setItem(this.TOKEN_KEY, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
static removeToken() {
|
||||||
|
localStorage.removeItem(this.TOKEN_KEY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const Authorize = {
|
const Authorize = {
|
||||||
setup() {
|
setup() {
|
||||||
@@ -19,7 +61,7 @@ const Authorize = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
template: `
|
template: `
|
||||||
<div class="authorize__container">
|
<div class="flex__container__center">
|
||||||
<a v-if="loginLink" :href="loginLink" class="authorize__twitch_btn">Login with Twitch</a>
|
<a v-if="loginLink" :href="loginLink" class="authorize__twitch_btn">Login with Twitch</a>
|
||||||
<div v-else>Loading...</div>
|
<div v-else>Loading...</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -28,8 +70,24 @@ const Authorize = {
|
|||||||
|
|
||||||
|
|
||||||
const Settings = {
|
const Settings = {
|
||||||
|
setup() {
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const logout = () => {
|
||||||
|
TokenManager.removeToken();
|
||||||
|
router.push('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
logout,
|
||||||
|
};
|
||||||
|
},
|
||||||
template: `
|
template: `
|
||||||
<div>Settings</div>
|
<div class="settings__container">
|
||||||
|
<div class="settings__header">
|
||||||
|
<button @click="logout">Logout</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +98,7 @@ const Main = {
|
|||||||
Settings
|
Settings
|
||||||
},
|
},
|
||||||
setup() {
|
setup() {
|
||||||
const authorized = localStorage.getItem('token') !== null;
|
const authorized = TokenManager.getAndValidate() !== null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
authorized
|
authorized
|
||||||
@@ -63,14 +121,16 @@ const AuthCallbackTwitch = {
|
|||||||
fetch('/api/auth/callback/twitch/' + window.location.search)
|
fetch('/api/auth/callback/twitch/' + window.location.search)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
localStorage.setItem('token', data.token);
|
localStorage.setItem(TOKEN_KEY, data.token);
|
||||||
|
|
||||||
router.push('/');
|
router.push('/');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
template: `
|
template: `
|
||||||
<div>AuthCallbackTwitch</div>
|
<div class="flex__container__center">
|
||||||
|
<div>Loading...</div>
|
||||||
|
</div>
|
||||||
`
|
`
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class TwitchSerializer(BaseModel):
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class StreamerSerializer(BaseModel):
|
class StreamerSerializer(BaseModel):
|
||||||
pass
|
twitch: TwitchSerializer
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends
|
|||||||
from authx import RequestToken
|
from authx import RequestToken
|
||||||
|
|
||||||
from modules.web_app.auth.authx import auth
|
from modules.web_app.auth.authx import auth
|
||||||
from modules.web_app.serializers.streamer import StreamerSerializer
|
from modules.web_app.serializers.streamer import StreamerSerializer, TwitchSerializer
|
||||||
from repositories.streamers import StreamerConfigRepository
|
from repositories.streamers import StreamerConfigRepository
|
||||||
from repositories.users import UserRepository
|
from repositories.users import UserRepository
|
||||||
from domain.auth import OAuthProvider
|
from domain.auth import OAuthProvider
|
||||||
@@ -35,3 +35,28 @@ async def get_streamers(
|
|||||||
)]
|
)]
|
||||||
|
|
||||||
return [StreamerSerializer(**streamer.model_dump()) for streamer in streamers]
|
return [StreamerSerializer(**streamer.model_dump()) for streamer in streamers]
|
||||||
|
|
||||||
|
|
||||||
|
@streamer_router.get("/me/")
|
||||||
|
async def get_me(
|
||||||
|
token: RequestToken = Depends(RequestToken)
|
||||||
|
) -> StreamerSerializer:
|
||||||
|
payload = auth.verify_token(token)
|
||||||
|
|
||||||
|
u_id = payload.sub
|
||||||
|
user = await UserRepository.get(u_id)
|
||||||
|
|
||||||
|
twith_oauth = user.oauths.get(OAuthProvider.TWITCH)
|
||||||
|
if not twith_oauth:
|
||||||
|
raise Exception("Twitch account not linked")
|
||||||
|
|
||||||
|
streamer = await StreamerConfigRepository.get_by_twitch_id(
|
||||||
|
int(twith_oauth.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
return StreamerSerializer(
|
||||||
|
twitch=TwitchSerializer(
|
||||||
|
id=streamer.twitch.id,
|
||||||
|
name=streamer.twitch.name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user