Fix issue #19: Add unified campaign sync
This commit is contained in:
@@ -1,260 +0,0 @@
|
||||
<script setup>
|
||||
import { onMounted, ref } from 'vue';
|
||||
import Server from '~/services/Server';
|
||||
import { GetUser, SetUser } from '~/services/User';
|
||||
import { AddSound } from '~/services/Sound';
|
||||
import Spinner from '../partials/Spinner.vue';
|
||||
|
||||
// Pantalla autònoma que el bot obre des de Discord: l'usuari s'identifica aquí
|
||||
// i autoritza el servidor de Discord que s'ha rebut per la URL.
|
||||
useHead({ title: 'Connect Dragonroll to Discord' });
|
||||
|
||||
const card = ref(null);
|
||||
|
||||
const code = ref('');
|
||||
const guildName = ref('');
|
||||
const discordUserName = ref('');
|
||||
const signedInAs = ref('');
|
||||
const connectedGuild = ref('');
|
||||
|
||||
// 'checking' | 'login' | 'approve' | 'connected' | 'error'
|
||||
const status = ref('checking');
|
||||
const messageKey = ref('');
|
||||
|
||||
const username = ref('');
|
||||
const password = ref('');
|
||||
const busy = ref(false);
|
||||
|
||||
function readCode() {
|
||||
const hash = new URLSearchParams(window.location.hash.slice(1));
|
||||
const search = new URLSearchParams(window.location.search);
|
||||
return hash.get('code') || search.get('code') || '';
|
||||
}
|
||||
|
||||
function ShowError(key) {
|
||||
messageKey.value = key;
|
||||
}
|
||||
|
||||
function PrepareApproval() {
|
||||
const user = GetUser();
|
||||
if (!user) {
|
||||
status.value = 'login';
|
||||
messageKey.value = '';
|
||||
return;
|
||||
}
|
||||
signedInAs.value = user.username || user.name || user.email || 'Dragonroll';
|
||||
status.value = 'approve';
|
||||
messageKey.value = '';
|
||||
}
|
||||
|
||||
async function LoadRequest() {
|
||||
const response = await Server().post('/discord/link/details', { code: code.value });
|
||||
const details = response.data;
|
||||
if (details.status !== 'ok') {
|
||||
status.value = 'error';
|
||||
ShowError('discord.errors.link-expired');
|
||||
return;
|
||||
}
|
||||
|
||||
guildName.value = details.guildName;
|
||||
discordUserName.value = details.discordUserName;
|
||||
PrepareApproval();
|
||||
}
|
||||
|
||||
async function SignIn() {
|
||||
busy.value = true;
|
||||
messageKey.value = '';
|
||||
try {
|
||||
const response = await Server().post('/user/login', {
|
||||
usermail: username.value,
|
||||
password: password.value
|
||||
});
|
||||
|
||||
if (response.data.status !== 'ok' || !response.data.token) {
|
||||
ShowError(response.data.msg || 'discord.errors.sign-in-failed');
|
||||
return;
|
||||
}
|
||||
|
||||
// El token passa al servei d'usuari perquè el client el reutilitzi en autoritzar.
|
||||
SetUser(response.data.token);
|
||||
password.value = '';
|
||||
PrepareApproval();
|
||||
} catch {
|
||||
ShowError('discord.errors.sign-in-failed');
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function Authorize() {
|
||||
busy.value = true;
|
||||
try {
|
||||
const response = await Server().post('/discord/link/complete', { code: code.value });
|
||||
|
||||
if (response.data.status !== 'ok') {
|
||||
status.value = 'error';
|
||||
ShowError('discord.errors.link-expired');
|
||||
return;
|
||||
}
|
||||
|
||||
connectedGuild.value = response.data.guildName;
|
||||
status.value = 'connected';
|
||||
messageKey.value = '';
|
||||
// El codi ja s'ha fet servir, treu-lo de la barra d'adreces.
|
||||
window.history.replaceState(null, '', window.location.pathname);
|
||||
} catch (error) {
|
||||
if (error?.response?.status === 401) {
|
||||
status.value = 'login';
|
||||
ShowError('discord.errors.session-expired');
|
||||
return;
|
||||
}
|
||||
status.value = 'error';
|
||||
ShowError('discord.errors.link-expired');
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
AddSound(card.value);
|
||||
code.value = readCode();
|
||||
|
||||
if (!code.value) {
|
||||
status.value = 'error';
|
||||
ShowError('discord.errors.missing-code');
|
||||
return;
|
||||
}
|
||||
|
||||
LoadRequest().catch(() => {
|
||||
status.value = 'error';
|
||||
ShowError('discord.errors.internal');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="discord-authorize">
|
||||
<main class="authorize-card" ref="card">
|
||||
<h1>{{ $t('discord.title') }}</h1>
|
||||
|
||||
<p v-if="guildName" class="summary">
|
||||
{{ $t('discord.summary', { guild: guildName, user: discordUserName }) }}
|
||||
</p>
|
||||
<p v-else-if="status !== 'error'" class="summary">
|
||||
<Spinner v-if="status === 'checking'"></Spinner>
|
||||
<span v-else>{{ $t('discord.checking') }}</span>
|
||||
</p>
|
||||
|
||||
<form v-if="status === 'login'" v-on:submit.prevent="SignIn">
|
||||
<p>{{ $t('discord.sign-in-lead') }}</p>
|
||||
<div class="form-field">
|
||||
<label for="discord-authorize-usermail">{{ $t('login.username') }}</label>
|
||||
<input
|
||||
id="discord-authorize-usermail"
|
||||
type="text"
|
||||
:placeholder="$t('login.username-placeholder')"
|
||||
name="usermail"
|
||||
autocomplete="username"
|
||||
v-model="username"
|
||||
required
|
||||
>
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<label for="discord-authorize-password">{{ $t('login.password') }}</label>
|
||||
<input
|
||||
id="discord-authorize-password"
|
||||
type="password"
|
||||
name="password"
|
||||
autocomplete="current-password"
|
||||
v-model="password"
|
||||
required
|
||||
>
|
||||
</div>
|
||||
<button class="btn-primary sound-click" type="submit" :disabled="busy">
|
||||
<Spinner v-if="busy"></Spinner>
|
||||
<span v-else>{{ $t('login.log-in') }}</span>
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<section v-if="status === 'approve'">
|
||||
<p>{{ $t('discord.signed-in-as', { user: signedInAs }) }}</p>
|
||||
<p class="hint">{{ $t('discord.next-step') }}</p>
|
||||
<p class="hint">{{ $t('discord.replace-warning') }}</p>
|
||||
<button class="btn-primary sound-click" type="button" :disabled="busy" v-on:click="Authorize">
|
||||
<Spinner v-if="busy"></Spinner>
|
||||
<span v-else>{{ $t('discord.approve') }}</span>
|
||||
</button>
|
||||
</section>
|
||||
|
||||
<section v-if="status === 'connected'">
|
||||
<p class="success">{{ $t('discord.connected', { guild: connectedGuild }) }}</p>
|
||||
<p class="hint">{{ $t('discord.next-step') }}</p>
|
||||
</section>
|
||||
|
||||
<p v-if="messageKey" class="error" role="status">
|
||||
{{ $t(messageKey) }}
|
||||
</p>
|
||||
</main>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.discord-authorize {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 100%;
|
||||
min-height: 100vh;
|
||||
padding: 24px;
|
||||
box-sizing: border-box;
|
||||
background-color: var(--color-background);
|
||||
color: var(--color-text);
|
||||
}
|
||||
|
||||
.authorize-card {
|
||||
width: min(100%, 460px);
|
||||
padding: 28px;
|
||||
box-sizing: border-box;
|
||||
background-color: var(--color-window-background);
|
||||
border: 1px solid var(--color-window-border);
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 8px 24px var(--color-window-shadow);
|
||||
text-align: center;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.authorize-card h1 {
|
||||
margin-top: 0;
|
||||
font-family: 'MrEavesRemake', serif;
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.authorize-card p {
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.summary {
|
||||
color: var(--color-gray);
|
||||
}
|
||||
|
||||
form,
|
||||
section {
|
||||
margin-top: 18px;
|
||||
}
|
||||
|
||||
.hint {
|
||||
color: var(--color-gray);
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.success {
|
||||
color: var(--color-green);
|
||||
}
|
||||
|
||||
.error {
|
||||
color: var(--color-red);
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
395
frontend/app/components/views/ServiceLinkAuthorize.vue
Normal file
395
frontend/app/components/views/ServiceLinkAuthorize.vue
Normal file
@@ -0,0 +1,395 @@
|
||||
<script setup>
|
||||
import { computed, onMounted, ref } from 'vue';
|
||||
import Server from '~/services/Server';
|
||||
import { GetUser, SetUser } from '~/services/User';
|
||||
import { AddSound } from '~/services/Sound';
|
||||
import Spinner from '../partials/Spinner.vue';
|
||||
|
||||
// Pantalla única d'enllaç de serveis (Discord, Foundry, Obsidian): el servei demana
|
||||
// l'enllaç al backend, l'usuari hi entra, s'identifica i tria la campanya que el
|
||||
// servei podrà fer servir. Abans cada servei tenia la seva pròpia pantalla.
|
||||
const card = ref(null);
|
||||
|
||||
const service = ref('');
|
||||
const serviceName = ref('Dragonroll');
|
||||
const code = ref('');
|
||||
const targetName = ref('');
|
||||
const targetUser = ref('');
|
||||
const system = ref('');
|
||||
const signedInAs = ref('');
|
||||
|
||||
const campaigns = ref([]);
|
||||
const selectedCampaign = ref('');
|
||||
const connectedCampaign = ref('');
|
||||
|
||||
// 'checking' | 'login' | 'pick' | 'connected' | 'error'
|
||||
const status = ref('checking');
|
||||
const messageKey = ref('');
|
||||
|
||||
const username = ref('');
|
||||
const password = ref('');
|
||||
const busy = ref(false);
|
||||
|
||||
// El nom propi del servei és l'últim recurs: el backend el dona amb la petició.
|
||||
const serviceLabel = computed(() => serviceName.value || serviceNameOf(service.value));
|
||||
// Cada servei explica d'una manera què és el que demana i què cal fer després.
|
||||
const targetKey = computed(() => `link.target.${service.value || 'other'}`);
|
||||
const nextKey = computed(() => `link.next.${service.value || 'other'}`);
|
||||
|
||||
function serviceNameOf(value) {
|
||||
if (value === 'discord') return 'Discord';
|
||||
if (value === 'foundry') return 'Foundry VTT';
|
||||
if (value === 'obsidian') return 'Obsidian';
|
||||
return 'Dragonroll';
|
||||
}
|
||||
|
||||
// El codi viatja al fragment de la URL, així que el navegador no l'envia mai al
|
||||
// servidor. El servei, que és qui demana l'enllaç, hi posa també el seu nom.
|
||||
function readLinkParams() {
|
||||
const hash = new URLSearchParams(window.location.hash.slice(1));
|
||||
const search = new URLSearchParams(window.location.search);
|
||||
return {
|
||||
code: hash.get('code') || search.get('code') || '',
|
||||
service: hash.get('service') || search.get('service') || ''
|
||||
};
|
||||
}
|
||||
|
||||
function ShowError(key) {
|
||||
messageKey.value = key;
|
||||
}
|
||||
|
||||
function AskForLogin() {
|
||||
const user = GetUser();
|
||||
if (!user) {
|
||||
status.value = 'login';
|
||||
messageKey.value = '';
|
||||
return;
|
||||
}
|
||||
signedInAs.value = user.username || user.name || user.email || 'Dragonroll';
|
||||
status.value = 'pick';
|
||||
messageKey.value = '';
|
||||
LoadCampaigns();
|
||||
}
|
||||
|
||||
async function LoadLinkDetails() {
|
||||
const response = await Server().post('/link/details', { code: code.value });
|
||||
const details = response.data;
|
||||
if (details.status !== 'ok') {
|
||||
status.value = 'error';
|
||||
ShowError('link.errors.expired');
|
||||
return;
|
||||
}
|
||||
|
||||
service.value = details.service || service.value;
|
||||
serviceName.value = details.serviceName || serviceNameOf(service.value);
|
||||
targetName.value = details.targetName || '';
|
||||
targetUser.value = details.targetUser || '';
|
||||
system.value = details.system || '';
|
||||
AskForLogin();
|
||||
}
|
||||
|
||||
async function LoadCampaigns() {
|
||||
try {
|
||||
const response = await Server().get('/campaign/list');
|
||||
campaigns.value = (response.data.status === 'ok' && Array.isArray(response.data.campaigns))
|
||||
? response.data.campaigns
|
||||
: [];
|
||||
if (campaigns.value.length === 1) selectedCampaign.value = String(campaigns.value[0]._id);
|
||||
} catch {
|
||||
campaigns.value = [];
|
||||
}
|
||||
}
|
||||
|
||||
async function SignIn() {
|
||||
busy.value = true;
|
||||
messageKey.value = '';
|
||||
try {
|
||||
const response = await Server().post('/user/login', {
|
||||
usermail: username.value,
|
||||
password: password.value
|
||||
});
|
||||
|
||||
if (response.data.status !== 'ok' || !response.data.token) {
|
||||
ShowError(response.data.msg || 'link.errors.sign-in-failed');
|
||||
return;
|
||||
}
|
||||
|
||||
// El token passa al servei d'usuari perquè el client el reutilitzi en connectar.
|
||||
SetUser(response.data.token);
|
||||
password.value = '';
|
||||
AskForLogin();
|
||||
} catch {
|
||||
ShowError('link.errors.sign-in-failed');
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// A campaign that already has this very service on it is being re-connected, and
|
||||
// saying so is what keeps the picker honest.
|
||||
function AlreadyLinked(campaign) {
|
||||
if (service.value === 'foundry') return !!campaign.foundryWorldId;
|
||||
if (service.value === 'discord') return !!campaign.discordGuildId;
|
||||
return false;
|
||||
}
|
||||
|
||||
async function Connect() {
|
||||
if (!selectedCampaign.value) {
|
||||
ShowError('link.errors.no-campaign');
|
||||
return;
|
||||
}
|
||||
|
||||
busy.value = true;
|
||||
messageKey.value = '';
|
||||
try {
|
||||
const response = await Server().post('/link/complete', {
|
||||
code: code.value,
|
||||
campaignId: selectedCampaign.value
|
||||
});
|
||||
|
||||
if (response.data.status !== 'ok') {
|
||||
ShowError(response.data.msg || 'link.errors.expired');
|
||||
return;
|
||||
}
|
||||
|
||||
connectedCampaign.value = response.data.campaign?.name || '';
|
||||
status.value = 'connected';
|
||||
// El codi ja s'ha fet servir, treu-lo de la barra d'adreces.
|
||||
window.history.replaceState(null, '', window.location.pathname);
|
||||
} catch (error) {
|
||||
if (error?.response?.status === 401) {
|
||||
status.value = 'login';
|
||||
ShowError('link.errors.session-expired');
|
||||
return;
|
||||
}
|
||||
ShowError('link.errors.internal');
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
useHead({
|
||||
title: computed(() => `Connect ${serviceLabel.value} to Dragonroll`)
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
AddSound(card.value);
|
||||
const params = readLinkParams();
|
||||
code.value = params.code;
|
||||
service.value = params.service || (window.location.pathname.startsWith('/discord') ? 'discord' : '');
|
||||
|
||||
if (!code.value) {
|
||||
status.value = 'error';
|
||||
ShowError('link.errors.missing-code');
|
||||
return;
|
||||
}
|
||||
|
||||
LoadLinkDetails().catch(() => {
|
||||
status.value = 'error';
|
||||
ShowError('link.errors.internal');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="service-link">
|
||||
<main class="link-card" ref="card">
|
||||
<h1>{{ $t('link.title', { service: serviceLabel }) }}</h1>
|
||||
|
||||
<p v-if="targetName" class="summary">
|
||||
{{ $t(targetKey, {
|
||||
target: targetName,
|
||||
user: targetUser,
|
||||
system: system ? $t('link.system', { system }) : ''
|
||||
}) }}
|
||||
</p>
|
||||
<p v-else-if="status !== 'error'" class="summary">
|
||||
<Spinner v-if="status === 'checking'"></Spinner>
|
||||
<span v-else>{{ $t('link.checking') }}</span>
|
||||
</p>
|
||||
|
||||
<form v-if="status === 'login'" v-on:submit.prevent="SignIn">
|
||||
<p>{{ $t('link.sign-in-lead', { service: serviceLabel }) }}</p>
|
||||
<div class="form-field">
|
||||
<label for="service-link-usermail">{{ $t('login.username') }}</label>
|
||||
<input
|
||||
id="service-link-usermail"
|
||||
type="text"
|
||||
:placeholder="$t('login.username-placeholder')"
|
||||
name="usermail"
|
||||
autocomplete="username"
|
||||
v-model="username"
|
||||
required
|
||||
>
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<label for="service-link-password">{{ $t('login.password') }}</label>
|
||||
<input
|
||||
id="service-link-password"
|
||||
type="password"
|
||||
name="password"
|
||||
autocomplete="current-password"
|
||||
v-model="password"
|
||||
required
|
||||
>
|
||||
</div>
|
||||
<button class="btn-primary sound-click" type="submit" :disabled="busy">
|
||||
<Spinner v-if="busy"></Spinner>
|
||||
<span v-else>{{ $t('login.log-in') }}</span>
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<section v-if="status === 'pick'">
|
||||
<p>{{ $t('link.signed-in-as', { user: signedInAs }) }}</p>
|
||||
<p class="question">{{ $t('link.pick-campaign', { service: serviceLabel }) }}</p>
|
||||
|
||||
<p v-if="!campaigns.length" class="hint">{{ $t('link.no-campaigns') }}</p>
|
||||
<ul v-else class="campaigns">
|
||||
<li v-for="campaign in campaigns" :key="campaign._id">
|
||||
<label class="campaign" :class="{ selected: selectedCampaign === String(campaign._id) }">
|
||||
<input
|
||||
type="radio"
|
||||
name="campaign"
|
||||
:value="String(campaign._id)"
|
||||
v-model="selectedCampaign"
|
||||
>
|
||||
<span class="swatch" :style="{ backgroundColor: campaign.color || 'var(--color-accent)' }"></span>
|
||||
<span class="campaign-name">{{ campaign.name }}</span>
|
||||
<span v-if="AlreadyLinked(campaign)" class="badge">
|
||||
{{ $t('link.already-linked', { service: serviceLabel }) }}
|
||||
</span>
|
||||
</label>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p class="hint">{{ $t('link.trust-warning', { service: serviceLabel }) }}</p>
|
||||
<p class="hint">{{ $t('link.replace-warning') }}</p>
|
||||
<button
|
||||
class="btn-primary sound-click"
|
||||
type="button"
|
||||
:disabled="busy || !selectedCampaign"
|
||||
v-on:click="Connect"
|
||||
>
|
||||
<Spinner v-if="busy"></Spinner>
|
||||
<span v-else>{{ $t('link.connect', { service: serviceLabel }) }}</span>
|
||||
</button>
|
||||
</section>
|
||||
|
||||
<section v-if="status === 'connected'">
|
||||
<p class="success">{{ $t('link.connected', { campaign: connectedCampaign }) }}</p>
|
||||
<p class="hint">{{ $t(nextKey, { service: serviceLabel }) }}</p>
|
||||
</section>
|
||||
|
||||
<p v-if="messageKey" class="error" role="status">
|
||||
{{ $t(messageKey) }}
|
||||
</p>
|
||||
</main>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.service-link {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 100%;
|
||||
min-height: 100vh;
|
||||
padding: 24px;
|
||||
box-sizing: border-box;
|
||||
background-color: var(--color-background);
|
||||
color: var(--color-text);
|
||||
}
|
||||
|
||||
.link-card {
|
||||
width: min(100%, 460px);
|
||||
padding: 28px;
|
||||
box-sizing: border-box;
|
||||
background-color: var(--color-window-background);
|
||||
border: 1px solid var(--color-window-border);
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 8px 24px var(--color-window-shadow);
|
||||
text-align: center;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.link-card h1 {
|
||||
margin-top: 0;
|
||||
font-family: 'MrEavesRemake', serif;
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.link-card p {
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.summary {
|
||||
color: var(--color-gray);
|
||||
}
|
||||
|
||||
form,
|
||||
section {
|
||||
margin-top: 18px;
|
||||
}
|
||||
|
||||
.question {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.campaigns {
|
||||
list-style: none;
|
||||
margin: 0 0 14px 0;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.campaign {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 10px;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.campaign.selected {
|
||||
border-color: var(--color-accent);
|
||||
background-color: var(--color-background-soft);
|
||||
}
|
||||
|
||||
.swatch {
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
border-radius: 4px;
|
||||
flex: 0 0 auto;
|
||||
border: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.campaign-name {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.badge {
|
||||
font-size: 0.8rem;
|
||||
color: var(--color-gray);
|
||||
}
|
||||
|
||||
.hint {
|
||||
color: var(--color-gray);
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.success {
|
||||
color: var(--color-green);
|
||||
}
|
||||
|
||||
.error {
|
||||
color: var(--color-red);
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user