refactor pages/auth.vue to composition API

This commit is contained in:
Johann150 2022-07-26 14:11:16 +02:00
parent ddebf5a049
commit 2e1385930c
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -1,26 +1,24 @@
<template> <template>
<div v-if="$i && fetching" class=""> <div v-if="$i">
<MkLoading/> <MkLoading v-if="state == 'fetching'"/>
</div>
<div v-else-if="$i">
<XForm <XForm
v-if="state == 'waiting'" v-else-if="state == 'waiting'"
ref="form" ref="form"
class="form" class="form"
:session="session" :session="session"
@denied="state = 'denied'" @denied="state = 'denied'"
@accepted="accepted" @accepted="accepted"
/> />
<div v-if="state == 'denied'" class="denied"> <div v-else-if="state == 'denied'" class="denied">
<h1>{{ $ts._auth.denied }}</h1> <h1>{{ i18n.ts._auth.denied }}</h1>
</div> </div>
<div v-if="state == 'accepted'" class="accepted"> <div v-else-if="state == 'accepted'" class="accepted">
<h1>{{ session.app.isAuthorized ? $t('already-authorized') : $ts.allowed }}</h1> <h1>{{ session.app.isAuthorized ? i18n.t('already-authorized') : i18n.ts.allowed }}</h1>
<p v-if="session.app.callbackUrl">{{ $ts._auth.callback }}<MkEllipsis/></p> <p v-if="session.app.callbackUrl">{{ i18n.ts._auth.callback }}<MkEllipsis/></p>
<p v-if="!session.app.callbackUrl">{{ $ts._auth.pleaseGoBack }}</p> <p v-if="!session.app.callbackUrl">{{ i18n.ts._auth.pleaseGoBack }}</p>
</div> </div>
<div v-if="state == 'fetch-session-error'" class="error"> <div v-else-if="state == 'fetch-session-error'" class="error">
<p>{{ $ts.somethingHappened }}</p> <p>{{ i18n.ts.somethingHappened }}</p>
</div> </div>
</div> </div>
<div v-else class="signin"> <div v-else class="signin">
@ -28,64 +26,55 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { onMounted } from 'vue';
import XForm from './auth.form.vue'; import XForm from './auth.form.vue';
import MkSignin from '@/components/signin.vue'; import MkSignin from '@/components/signin.vue';
import * as os from '@/os'; import * as os from '@/os';
import { login } from '@/account'; import { login } from '@/account';
import { i18n } from '@/i18n';
import { $i } from '@/account';
import { query, appendQuery } from '@/scripts/url';
export default defineComponent({ const props = defineProps<{
components: { token: string;
XForm, }>();
MkSignin,
}, let state: 'fetching' | 'waiting' | 'denied' | 'accepted' | 'fetch-session-error' = $ref('fetching');
props: ['token'], let session = $ref(null);
data() {
return { onMounted(() => {
state: null, if (!$i) return;
session: null,
fetching: true,
};
},
mounted() {
if (!this.$i) return;
// Fetch session // Fetch session
os.api('auth/session/show', { os.api('auth/session/show', {
token: this.token, token: props.token,
}).then(session => { }).then(fetchedSession => {
this.session = session; session = fetchedSession;
this.fetching = false;
// //
if (this.session.app.isAuthorized) { if (session.app.isAuthorized) {
os.api('auth/accept', { os.api('auth/accept', {
token: this.session.token, token: session.token,
}).then(() => { }).then(() => {
this.accepted(); this.accepted();
}); });
} else { } else {
this.state = 'waiting'; state = 'waiting';
} }
}).catch(error => { }).catch(error => {
this.state = 'fetch-session-error'; state = 'fetch-session-error';
this.fetching = false;
}); });
},
methods: {
accepted() {
this.state = 'accepted';
if (this.session.app.callbackUrl) {
location.href = `${this.session.app.callbackUrl}?token=${this.session.token}`;
}
}, onLogin(res) {
login(res.i);
},
},
}); });
function accepted() {
state = 'accepted';
if (session.app.callbackUrl) {
location.href = appendQuery(session.app.callbackUrl, query({ token: session.token }));
}
}
function onLogin(res) {
login(res.i);
}
</script> </script>
<style lang="scss" scoped>
</style>