2023-07-27 05:31:52 +00:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2018-02-10 07:22:14 +00:00
|
|
|
<template>
|
2023-05-14 01:21:56 +00:00
|
|
|
<form :class="{ signing, totpLogin }" @submit.prevent="onSubmit">
|
|
|
|
<div class="_gaps_m">
|
2023-09-08 05:05:03 +00:00
|
|
|
<div v-show="withAvatar" :class="$style.avatar" :style="{ backgroundImage: user ? `url('${ user.avatarUrl }')` : undefined, marginBottom: message ? '1.5em' : undefined }"></div>
|
2022-05-19 11:28:08 +00:00
|
|
|
<MkInfo v-if="message">
|
|
|
|
{{ message }}
|
|
|
|
</MkInfo>
|
2023-01-06 04:40:17 +00:00
|
|
|
<div v-if="!totpLogin" class="normal-signin _gaps_m">
|
2023-09-08 05:05:03 +00:00
|
|
|
<MkInput v-model="username" :placeholder="i18n.ts.username" type="text" pattern="^[a-zA-Z0-9_]+$" :spellcheck="false" autocomplete="username webauthn" autofocus required data-cy-signin-username @update:modelValue="onUsernameChange">
|
2020-10-31 00:39:22 +00:00
|
|
|
<template #prefix>@</template>
|
|
|
|
<template #suffix>@{{ host }}</template>
|
|
|
|
</MkInput>
|
2023-09-08 05:05:03 +00:00
|
|
|
<MkInput v-if="!user || user && !user.usePasswordLessLogin" v-model="password" :placeholder="i18n.ts.password" type="password" autocomplete="current-password webauthn" :withPasswordToggle="true" required data-cy-signin-password>
|
2023-09-30 19:53:52 +00:00
|
|
|
<template #prefix><i class="ph-lock ph-bold ph-lg"></i></template>
|
2022-05-19 11:28:08 +00:00
|
|
|
<template #caption><button class="_textButton" type="button" @click="resetPassword">{{ i18n.ts.forgotPassword }}</button></template>
|
2020-10-17 11:12:00 +00:00
|
|
|
</MkInput>
|
2023-01-08 05:17:56 +00:00
|
|
|
<MkButton type="submit" large primary rounded :disabled="signing" style="margin: 0 auto;">{{ signing ? i18n.ts.loggingIn : i18n.ts.login }}</MkButton>
|
2020-10-31 00:39:22 +00:00
|
|
|
</div>
|
2021-11-19 10:36:12 +00:00
|
|
|
<div v-if="totpLogin" class="2fa-signin" :class="{ securityKeys: user && user.securityKeys }">
|
2020-10-31 00:39:22 +00:00
|
|
|
<div v-if="user && user.securityKeys" class="twofa-group tap-group">
|
2023-09-08 05:05:03 +00:00
|
|
|
<p>{{ i18n.ts.useSecurityKey }}</p>
|
2021-11-19 10:36:12 +00:00
|
|
|
<MkButton v-if="!queryingKey" @click="queryKey">
|
2022-05-19 11:28:08 +00:00
|
|
|
{{ i18n.ts.retry }}
|
2020-10-31 00:39:22 +00:00
|
|
|
</MkButton>
|
|
|
|
</div>
|
2021-11-19 10:36:12 +00:00
|
|
|
<div v-if="user && user.securityKeys" class="or-hr">
|
2022-05-19 11:28:08 +00:00
|
|
|
<p class="or-msg">{{ i18n.ts.or }}</p>
|
2020-10-31 00:39:22 +00:00
|
|
|
</div>
|
|
|
|
<div class="twofa-group totp-group">
|
2023-09-08 05:05:03 +00:00
|
|
|
<p style="margin-bottom:0;">{{ i18n.ts['2fa'] }}</p>
|
2023-05-19 04:58:09 +00:00
|
|
|
<MkInput v-if="user && user.usePasswordLessLogin" v-model="password" type="password" autocomplete="current-password" :withPasswordToggle="true" required>
|
2022-05-19 11:28:08 +00:00
|
|
|
<template #label>{{ i18n.ts.password }}</template>
|
2023-09-30 19:53:52 +00:00
|
|
|
<template #prefix><i class="ph-lock ph-bold ph-lg"></i></template>
|
2020-10-31 00:39:22 +00:00
|
|
|
</MkInput>
|
2023-08-28 09:25:31 +00:00
|
|
|
<MkInput v-model="token" type="text" pattern="^([0-9]{6}|[A-Z0-9]{32})$" autocomplete="one-time-code" :spellcheck="false" required>
|
2022-05-19 11:28:08 +00:00
|
|
|
<template #label>{{ i18n.ts.token }}</template>
|
2023-11-03 22:20:53 +00:00
|
|
|
<template #prefix><i class="ph-keyhole ph-bold ph-lg"></i></template>
|
2020-10-31 00:39:22 +00:00
|
|
|
</MkInput>
|
2023-01-08 05:17:56 +00:00
|
|
|
<MkButton type="submit" :disabled="signing" large primary rounded style="margin: 0 auto;">{{ signing ? i18n.ts.loggingIn : i18n.ts.login }}</MkButton>
|
2020-10-31 00:39:22 +00:00
|
|
|
</div>
|
2019-07-03 11:18:07 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2018-02-10 07:22:14 +00:00
|
|
|
</form>
|
|
|
|
</template>
|
|
|
|
|
2022-05-19 11:28:08 +00:00
|
|
|
<script lang="ts" setup>
|
2023-12-07 05:42:09 +00:00
|
|
|
import { defineAsyncComponent, ref } from 'vue';
|
2021-04-04 04:00:39 +00:00
|
|
|
import { toUnicode } from 'punycode/';
|
2023-09-20 07:44:12 +00:00
|
|
|
import * as Misskey from 'misskey-js';
|
2023-09-08 05:05:03 +00:00
|
|
|
import { supported as webAuthnSupported, get as webAuthnRequest, parseRequestOptionsFromJSON } from '@github/webauthn-json/browser-ponyfill';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { showSuspendedDialog } from '@/scripts/show-suspended-dialog.js';
|
2022-09-06 09:21:49 +00:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-01-07 06:09:46 +00:00
|
|
|
import MkInput from '@/components/MkInput.vue';
|
2022-09-06 09:21:49 +00:00
|
|
|
import MkInfo from '@/components/MkInfo.vue';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { host as configHost } from '@/config.js';
|
|
|
|
import * as os from '@/os.js';
|
|
|
|
import { login } from '@/account.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
2018-02-10 07:22:14 +00:00
|
|
|
|
2023-12-07 05:42:09 +00:00
|
|
|
const signing = ref(false);
|
|
|
|
const user = ref<Misskey.entities.UserDetailed | null>(null);
|
|
|
|
const username = ref('');
|
|
|
|
const password = ref('');
|
|
|
|
const token = ref('');
|
|
|
|
const host = ref(toUnicode(configHost));
|
|
|
|
const totpLogin = ref(false);
|
|
|
|
const queryingKey = ref(false);
|
|
|
|
const credentialRequest = ref<CredentialRequestOptions | null>(null);
|
2018-12-18 15:40:29 +00:00
|
|
|
|
2022-05-19 11:28:08 +00:00
|
|
|
const emit = defineEmits<{
|
|
|
|
(ev: 'login', v: any): void;
|
|
|
|
}>();
|
2018-12-18 15:40:29 +00:00
|
|
|
|
2022-05-19 11:28:08 +00:00
|
|
|
const props = defineProps({
|
|
|
|
withAvatar: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
2022-12-20 06:15:34 +00:00
|
|
|
default: true,
|
2020-02-10 14:17:42 +00:00
|
|
|
},
|
2022-05-19 11:28:08 +00:00
|
|
|
autoSet: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
message: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
2022-12-20 06:15:34 +00:00
|
|
|
default: '',
|
|
|
|
},
|
2022-05-19 11:28:08 +00:00
|
|
|
});
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
function onUsernameChange(): void {
|
2022-05-19 11:28:08 +00:00
|
|
|
os.api('users/show', {
|
2023-12-07 05:42:09 +00:00
|
|
|
username: username.value,
|
2022-05-23 20:43:13 +00:00
|
|
|
}).then(userResponse => {
|
2023-12-07 05:42:09 +00:00
|
|
|
user.value = userResponse;
|
2022-05-19 11:28:08 +00:00
|
|
|
}, () => {
|
2023-12-07 05:42:09 +00:00
|
|
|
user.value = null;
|
2022-05-19 11:28:08 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-03 11:18:07 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
function onLogin(res: any): Promise<void> | void {
|
2022-05-19 11:28:08 +00:00
|
|
|
if (props.autoSet) {
|
|
|
|
return login(res.i);
|
|
|
|
}
|
|
|
|
}
|
2021-09-18 17:23:12 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
async function queryKey(): Promise<void> {
|
2023-12-07 05:42:09 +00:00
|
|
|
queryingKey.value = true;
|
|
|
|
await webAuthnRequest(credentialRequest.value)
|
2023-09-08 05:05:03 +00:00
|
|
|
.catch(() => {
|
2023-12-07 05:42:09 +00:00
|
|
|
queryingKey.value = false;
|
2023-09-08 05:05:03 +00:00
|
|
|
return Promise.reject(null);
|
|
|
|
}).then(credential => {
|
2023-12-07 05:42:09 +00:00
|
|
|
credentialRequest.value = null;
|
|
|
|
queryingKey.value = false;
|
|
|
|
signing.value = true;
|
2023-09-08 05:05:03 +00:00
|
|
|
return os.api('signin', {
|
2023-12-07 05:42:09 +00:00
|
|
|
username: username.value,
|
|
|
|
password: password.value,
|
2023-09-08 05:05:03 +00:00
|
|
|
credential: credential.toJSON(),
|
|
|
|
});
|
|
|
|
}).then(res => {
|
|
|
|
emit('login', res);
|
|
|
|
return onLogin(res);
|
|
|
|
}).catch(err => {
|
|
|
|
if (err === null) return;
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: i18n.ts.signinFailed,
|
|
|
|
});
|
2023-12-07 05:42:09 +00:00
|
|
|
signing.value = false;
|
2022-05-19 11:28:08 +00:00
|
|
|
});
|
|
|
|
}
|
2021-09-18 17:23:12 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
function onSubmit(): void {
|
2023-12-07 05:42:09 +00:00
|
|
|
signing.value = true;
|
|
|
|
if (!totpLogin.value && user.value && user.value.twoFactorEnabled) {
|
|
|
|
if (webAuthnSupported() && user.value.securityKeys) {
|
2022-05-19 11:28:08 +00:00
|
|
|
os.api('signin', {
|
2023-12-07 05:42:09 +00:00
|
|
|
username: username.value,
|
|
|
|
password: password.value,
|
2022-05-19 11:28:08 +00:00
|
|
|
}).then(res => {
|
2023-12-07 05:42:09 +00:00
|
|
|
totpLogin.value = true;
|
|
|
|
signing.value = false;
|
|
|
|
credentialRequest.value = parseRequestOptionsFromJSON({
|
2023-09-08 05:05:03 +00:00
|
|
|
publicKey: res,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => queryKey())
|
|
|
|
.catch(loginFailed);
|
2022-05-19 11:28:08 +00:00
|
|
|
} else {
|
2023-12-07 05:42:09 +00:00
|
|
|
totpLogin.value = true;
|
|
|
|
signing.value = false;
|
2022-05-19 11:28:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
os.api('signin', {
|
2023-12-07 05:42:09 +00:00
|
|
|
username: username.value,
|
|
|
|
password: password.value,
|
|
|
|
token: user.value?.twoFactorEnabled ? token.value : undefined,
|
2022-05-19 11:28:08 +00:00
|
|
|
}).then(res => {
|
|
|
|
emit('login', res);
|
|
|
|
onLogin(res);
|
|
|
|
}).catch(loginFailed);
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 06:05:34 +00:00
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
function loginFailed(err: any): void {
|
2022-05-19 11:28:08 +00:00
|
|
|
switch (err.id) {
|
|
|
|
case '6cc579cc-885d-43d8-95c2-b8c7fc963280': {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.loginFailed,
|
2022-12-20 06:15:34 +00:00
|
|
|
text: i18n.ts.noSuchUser,
|
2022-05-19 11:28:08 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '932c904e-9460-45b7-9ce6-7ed33be7eb2c': {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.loginFailed,
|
|
|
|
text: i18n.ts.incorrectPassword,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'e03a5f46-d309-4865-9b69-56282d94e1eb': {
|
|
|
|
showSuspendedDialog();
|
|
|
|
break;
|
|
|
|
}
|
2022-05-28 03:06:47 +00:00
|
|
|
case '22d05606-fbcf-421a-a2db-b32610dcfd1b': {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.loginFailed,
|
|
|
|
text: i18n.ts.rateLimitExceeded,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2022-05-19 11:28:08 +00:00
|
|
|
default: {
|
2023-09-08 05:05:03 +00:00
|
|
|
console.error(err);
|
2022-05-19 11:28:08 +00:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
title: i18n.ts.loginFailed,
|
2022-12-20 06:15:34 +00:00
|
|
|
text: JSON.stringify(err),
|
2022-05-19 11:28:08 +00:00
|
|
|
});
|
2018-02-10 07:22:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 11:28:08 +00:00
|
|
|
|
2023-12-07 05:42:09 +00:00
|
|
|
totpLogin.value = false;
|
|
|
|
signing.value = false;
|
2022-05-19 11:28:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 05:05:03 +00:00
|
|
|
function resetPassword(): void {
|
2022-08-30 15:24:33 +00:00
|
|
|
os.popup(defineAsyncComponent(() => import('@/components/MkForgotPassword.vue')), {}, {
|
2022-05-19 11:28:08 +00:00
|
|
|
}, 'closed');
|
|
|
|
}
|
2018-02-10 07:22:14 +00:00
|
|
|
</script>
|
|
|
|
|
2023-05-14 01:21:56 +00:00
|
|
|
<style lang="scss" module>
|
|
|
|
.avatar {
|
|
|
|
margin: 0 auto 0 auto;
|
|
|
|
width: 64px;
|
|
|
|
height: 64px;
|
|
|
|
background: #ddd;
|
|
|
|
background-position: center;
|
|
|
|
background-size: cover;
|
2023-10-31 18:44:34 +00:00
|
|
|
border-radius: var(--radius-full);
|
2020-01-29 19:37:25 +00:00
|
|
|
}
|
2018-02-10 07:22:14 +00:00
|
|
|
</style>
|