Skip to content

Commit

Permalink
fix(frontend): ユーザー登録完了時にサインインAPIを別途使用していたのを修正 (#14738)
Browse files Browse the repository at this point in the history
* fix(frontend): ユーザー登録完了時にサインインAPIを別途使用していたのを修正

* emitされるオブジェクトの型を変更したことに伴う修正

* Update Changelog
  • Loading branch information
kakkokari-gtyih authored Oct 10, 2024
1 parent 4a356f1 commit a624546
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 60 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-

### Client
-
- メールアドレス不要でCaptchaが有効な場合にアカウント登録完了後自動でのログインに失敗する問題を修正

### Server
-
Expand Down
8 changes: 4 additions & 4 deletions packages/frontend/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export async function openAccountMenu(opts: {

function showSigninDialog() {
const { dispose } = popup(defineAsyncComponent(() => import('@/components/MkSigninDialog.vue')), {}, {
done: res => {
done: (res: Misskey.entities.SigninFlowResponse & { finished: true }) => {
addAccount(res.id, res.i);
success();
},
Expand All @@ -236,9 +236,9 @@ export async function openAccountMenu(opts: {

function createAccount() {
const { dispose } = popup(defineAsyncComponent(() => import('@/components/MkSignupDialog.vue')), {}, {
done: res => {
addAccount(res.id, res.i);
switchAccountWithToken(res.i);
done: (res: Misskey.entities.SignupResponse) => {
addAccount(res.id, res.token);
switchAccountWithToken(res.token);
},
closed: () => dispose(),
});
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/components/MkSignin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import type { AuthenticationPublicKeyCredential } from '@github/webauthn-json/br
import type { OpenOnRemoteOptions } from '@/scripts/please-login.js';

const emit = defineEmits<{
(ev: 'login', v: Misskey.entities.SigninFlowResponse): void;
(ev: 'login', v: Misskey.entities.SigninFlowResponse & { finished: true }): void;
}>();

const props = withDefaults(defineProps<{
Expand Down Expand Up @@ -276,7 +276,7 @@ async function tryLogin(req: Partial<Misskey.entities.SigninFlowRequest>): Promi
});
}

async function onLoginSucceeded(res: Misskey.entities.SigninFlowResponse & { finished: true; }) {
async function onLoginSucceeded(res: Misskey.entities.SigninFlowResponse & { finished: true }) {
if (props.autoSet) {
await login(res.i);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/frontend/src/components/MkSigninDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ SPDX-License-Identifier: AGPL-3.0-only
</template>

<script lang="ts" setup>
import * as Misskey from 'misskey-js';
import { shallowRef } from 'vue';
import type { OpenOnRemoteOptions } from '@/scripts/please-login.js';
import MkSignin from '@/components/MkSignin.vue';
Expand All @@ -40,7 +41,7 @@ withDefaults(defineProps<{
});

const emit = defineEmits<{
(ev: 'done', v: any): void;
(ev: 'done', v: Misskey.entities.SigninFlowResponse & { finished: true }): void;
(ev: 'closed'): void;
(ev: 'cancelled'): void;
}>();
Expand All @@ -52,7 +53,7 @@ function onClose() {
if (modal.value) modal.value.close();
}

function onLogin(res) {
function onLogin(res: Misskey.entities.SigninFlowResponse & { finished: true }) {
emit('done', res);
if (modal.value) modal.value.close();
}
Expand Down
84 changes: 47 additions & 37 deletions packages/frontend/src/components/MkSignupDialog.form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const props = withDefaults(defineProps<{
});

const emit = defineEmits<{
(ev: 'signup', user: Misskey.entities.SigninFlowResponse): void;
(ev: 'signup', user: Misskey.entities.SignupResponse): void;
(ev: 'signupEmailPending'): void;
}>();

Expand Down Expand Up @@ -250,52 +250,62 @@ async function onSubmit(): Promise<void> {
if (submitting.value) return;
submitting.value = true;

try {
await misskeyApi('signup', {
username: username.value,
password: password.value,
emailAddress: email.value,
invitationCode: invitationCode.value,
'hcaptcha-response': hCaptchaResponse.value,
'm-captcha-response': mCaptchaResponse.value,
'g-recaptcha-response': reCaptchaResponse.value,
'turnstile-response': turnstileResponse.value,
});
if (instance.emailRequiredForSignup) {
const signupPayload: Misskey.entities.SignupRequest = {
username: username.value,
password: password.value,
emailAddress: email.value,
invitationCode: invitationCode.value,
'hcaptcha-response': hCaptchaResponse.value,
'm-captcha-response': mCaptchaResponse.value,
'g-recaptcha-response': reCaptchaResponse.value,
'turnstile-response': turnstileResponse.value,
};

const res = await fetch(`${config.apiUrl}/signup`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(signupPayload),
}).catch(() => {
onSignupApiError();
return null;
});

if (res) {
if (res.status === 204 || instance.emailRequiredForSignup) {
os.alert({
type: 'success',
title: i18n.ts._signup.almostThere,
text: i18n.tsx._signup.emailSent({ email: email.value }),
});
emit('signupEmailPending');
} else {
const res = await misskeyApi('signin-flow', {
username: username.value,
password: password.value,
});
emit('signup', res);

if (props.autoSet && res.finished) {
return login(res.i);
} else {
os.alert({
type: 'error',
text: i18n.ts.somethingHappened,
});
const resJson = (await res.json()) as Misskey.entities.SignupResponse;
if (_DEV_) console.log(resJson);

emit('signup', resJson);

if (props.autoSet) {
await login(resJson.token);
}
}
} catch {
submitting.value = false;
hcaptcha.value?.reset?.();
mcaptcha.value?.reset?.();
recaptcha.value?.reset?.();
turnstile.value?.reset?.();

os.alert({
type: 'error',
text: i18n.ts.somethingHappened,
});
}

submitting.value = false;
}

function onSignupApiError() {
submitting.value = false;
hcaptcha.value?.reset?.();
mcaptcha.value?.reset?.();
recaptcha.value?.reset?.();
turnstile.value?.reset?.();

os.alert({
type: 'error',
text: i18n.ts.somethingHappened,
});
}
</script>

Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/components/MkSignupDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ const props = withDefaults(defineProps<{
});

const emit = defineEmits<{
(ev: 'done', res: Misskey.entities.SigninFlowResponse): void;
(ev: 'done', res: Misskey.entities.SignupResponse): void;
(ev: 'closed'): void;
}>();

const dialog = shallowRef<InstanceType<typeof MkModalWindow>>();

const isAcceptedServerRule = ref(false);

function onSignup(res: Misskey.entities.SigninFlowResponse) {
function onSignup(res: Misskey.entities.SignupResponse) {
emit('done', res);
dialog.value?.close();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/MkUserCardMini.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { acct } from '@/filters/user.js';

const props = withDefaults(defineProps<{
user: Misskey.entities.User;
withChart: boolean;
withChart?: boolean;
}>(), {
withChart: true,
});
Expand Down
18 changes: 9 additions & 9 deletions packages/frontend/src/pages/settings/accounts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const init = async () => {
});
};

function menu(account, ev) {
function menu(account: Misskey.entities.UserDetailed, ev: MouseEvent) {
os.popupMenu([{
text: i18n.ts.switch,
icon: 'ti ti-switch-horizontal',
Expand All @@ -58,7 +58,7 @@ function menu(account, ev) {
}], ev.currentTarget ?? ev.target);
}

function addAccount(ev) {
function addAccount(ev: MouseEvent) {
os.popupMenu([{
text: i18n.ts.existingAccount,
action: () => { addExistingAccount(); },
Expand All @@ -68,14 +68,14 @@ function addAccount(ev) {
}], ev.currentTarget ?? ev.target);
}

async function removeAccount(account) {
async function removeAccount(account: Misskey.entities.UserDetailed) {
await _removeAccount(account.id);
accounts.value = accounts.value.filter(x => x.id !== account.id);
}

function addExistingAccount() {
const { dispose } = os.popup(defineAsyncComponent(() => import('@/components/MkSigninDialog.vue')), {}, {
done: async res => {
done: async (res: Misskey.entities.SigninFlowResponse & { finished: true }) => {
await addAccounts(res.id, res.i);
os.success();
init();
Expand All @@ -86,17 +86,17 @@ function addExistingAccount() {

function createAccount() {
const { dispose } = os.popup(defineAsyncComponent(() => import('@/components/MkSignupDialog.vue')), {}, {
done: async res => {
await addAccounts(res.id, res.i);
switchAccountWithToken(res.i);
done: async (res: Misskey.entities.SignupResponse) => {
await addAccounts(res.id, res.token);
switchAccountWithToken(res.token);
},
closed: () => dispose(),
});
}

async function switchAccount(account: any) {
const fetchedAccounts: any[] = await getAccounts();
const token = fetchedAccounts.find(x => x.id === account.id).token;
const fetchedAccounts = await getAccounts();
const token = fetchedAccounts.find(x => x.id === account.id)!.token;
switchAccountWithToken(token);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/misskey-js/etc/misskey-js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3095,7 +3095,9 @@ type SigninWithPasskeyRequest = {

// @public (undocumented)
type SigninWithPasskeyResponse = {
signinResponse: SigninFlowResponse;
signinResponse: SigninFlowResponse & {
finished: true;
};
};

// @public (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion packages/misskey-js/src/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export type SigninWithPasskeyInitResponse = {
};

export type SigninWithPasskeyResponse = {
signinResponse: SigninFlowResponse;
signinResponse: SigninFlowResponse & { finished: true };
};

type Values<T extends Record<PropertyKey, unknown>> = T[keyof T];
Expand Down

0 comments on commit a624546

Please sign in to comment.