forked from FoundKeyGang/FoundKey
refactor pages/auth.vue to composition API
This commit is contained in:
parent
ddebf5a049
commit
2e1385930c
1 changed files with 54 additions and 65 deletions
|
@ -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,
|
|
||||||
},
|
|
||||||
props: ['token'],
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
state: null,
|
|
||||||
session: null,
|
|
||||||
fetching: true,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
mounted() {
|
|
||||||
if (!this.$i) return;
|
|
||||||
|
|
||||||
// Fetch session
|
let state: 'fetching' | 'waiting' | 'denied' | 'accepted' | 'fetch-session-error' = $ref('fetching');
|
||||||
os.api('auth/session/show', {
|
let session = $ref(null);
|
||||||
token: this.token,
|
|
||||||
}).then(session => {
|
|
||||||
this.session = session;
|
|
||||||
this.fetching = false;
|
|
||||||
|
|
||||||
// 既に連携していた場合
|
onMounted(() => {
|
||||||
if (this.session.app.isAuthorized) {
|
if (!$i) return;
|
||||||
os.api('auth/accept', {
|
|
||||||
token: this.session.token,
|
// Fetch session
|
||||||
}).then(() => {
|
os.api('auth/session/show', {
|
||||||
this.accepted();
|
token: props.token,
|
||||||
});
|
}).then(fetchedSession => {
|
||||||
} else {
|
session = fetchedSession;
|
||||||
this.state = 'waiting';
|
|
||||||
}
|
// 既に連携していた場合
|
||||||
}).catch(error => {
|
if (session.app.isAuthorized) {
|
||||||
this.state = 'fetch-session-error';
|
os.api('auth/accept', {
|
||||||
this.fetching = false;
|
token: session.token,
|
||||||
});
|
}).then(() => {
|
||||||
},
|
this.accepted();
|
||||||
methods: {
|
});
|
||||||
accepted() {
|
} else {
|
||||||
this.state = 'accepted';
|
state = 'waiting';
|
||||||
if (this.session.app.callbackUrl) {
|
}
|
||||||
location.href = `${this.session.app.callbackUrl}?token=${this.session.token}`;
|
}).catch(error => {
|
||||||
}
|
state = 'fetch-session-error';
|
||||||
}, 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>
|
|
||||||
|
|
Loading…
Reference in a new issue