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