Yuri Lee d8dd1683c9
Add Sign in with passkey Button (#14577)
* Sign in with passkey (PoC)

* 💄 Added "Login with Passkey" Button

* refactor: Improve error response when WebAuthn challenge fails

* signinResponse should be placed under the SigninWithPasskeyResponse object.

* Frontend fix

* Fix: Rate limiting key for passkey signin

Use specific rate limiting key: 'signin-with-passkey'  for passkey sign-in API to avoid collisions with signin rate-limit.

* Refactor: enhance Passkey sign-in flow and error handling

- Increased the rate limit for Passkey sign-in attempts to accommodate the two API calls needed per sign-in.
- Improved error messages and handling in both the `WebAuthnService` and the `SigninWithPasskeyApiService`, providing more context and better usability.
- Updated error messages to provide more specific and helpful details to the user.

These changes aim to enhance the Passkey sign-in experience by providing more robust error handling, improving security by limiting API calls, and delivering a more user-friendly interface.

* Refactor: Streamline 2FA flow and remove redundant Passkey button.

- Separate the flow of 1FA and 2FA.
- Remove duplicate passkey buttons

* Fix: Add error messages to MkSignin

* chore: Hide passkey button if the entered user does not use passkey login

* Update CHANGELOG.md

* Refactor: Rename functions and Add comments

* Update locales/ja-JP.yml

Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>

* Fix: Update translation

- update index.d.ts
- update ko-KR.yml, en-US.yml
- Fix: Reflect Changed i18n key on MkSignin

---------

Co-authored-by: Squarecat-meow <kw7551@gmail.com>
Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
2024-09-26 08:25:33 +09:00
..
2024-09-22 12:32:01 +09:00
2024-08-12 14:03:16 +09:00
2023-12-14 11:35:17 +09:00
2024-09-25 03:43:12 +00:00

misskey.js

Strongly-typed official Misskey SDK for browsers/Node.js.

Test codecov

NPM

JavaScript(TypeScript)用の公式MisskeySDKです。ブラウザ/Node.js上で動作します。

以下が提供されています:

  • ユーザー認証
  • APIリクエスト
  • ストリーミング
  • ユーティリティ関数
  • Misskeyの各種型定義

対応するMisskeyのバージョンは12以上です。

Install

npm i misskey-js

Usage

インポートは以下のようにまとめて行うと便利です。

import * as Misskey from 'misskey-js';

便宜上、以後のコード例は上記のように* as Misskeyとしてインポートしている前提のものになります。

ただし、このインポート方法だとTree-Shakingできなくなるので、コードサイズが重要なユースケースでは以下のような個別インポートをお勧めします。

import { api as misskeyApi } from 'misskey-js';

Authenticate

todo

API request

APIを利用する際は、利用するサーバーの情報とアクセストークンを与えてAPIClientクラスのインスタンスを初期化し、そのインスタンスのrequestメソッドを呼び出してリクエストを行います。

const cli = new Misskey.api.APIClient({
	origin: 'https://misskey.test',
	credential: 'TOKEN',
});

const meta = await cli.request('meta', { detail: true });

requestの第一引数には呼び出すエンドポイント名、第二引数にはパラメータオブジェクトを渡します。レスポンスはPromiseとして返ります。

Streaming

misskey.jsのストリーミングでは、二つのクラスが提供されます。 ひとつは、ストリーミングのコネクション自体を司るStreamクラスと、もうひとつはストリーミング上のチャンネルの概念を表すChannelクラスです。 ストリーミングを利用する際は、まずStreamクラスのインスタンスを初期化し、その後でStreamインスタンスのメソッドを利用してChannelクラスのインスタンスを取得する形になります。

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
	console.log('notification received', notification);
});

コネクションが途切れても自動で再接続されます。

チャンネルへの接続

チャンネルへの接続はStreamクラスのuseChannelメソッドを使用します。

パラメータなし

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });

const mainChannel = stream.useChannel('main');

パラメータあり

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });

const messagingChannel = stream.useChannel('messaging', {
	otherparty: 'xxxxxxxxxx',
});

チャンネルから切断

Channelクラスのdisposeメソッドを呼び出します。

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });

const mainChannel = stream.useChannel('main');

mainChannel.dispose();

メッセージの受信

ChannelクラスはEventEmitterを継承しており、メッセージがサーバーから受信されると受け取ったイベント名でペイロードをemitします。

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
	console.log('notification received', notification);
});

メッセージの送信

Channelクラスのsendメソッドを使用してメッセージをサーバーに送信することができます。

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const messagingChannel = stream.useChannel('messaging', {
	otherparty: 'xxxxxxxxxx',
});

messagingChannel.send('read', {
	id: 'xxxxxxxxxx'
});

コネクション確立イベント

Streamクラスの_connected_イベントが利用可能です。

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
stream.on('_connected_', () => {
	console.log('connected');
});

コネクション切断イベント

Streamクラスの_disconnected_イベントが利用可能です。

const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
stream.on('_disconnected_', () => {
	console.log('disconnected');
});

コネクションの状態

Streamクラスのstateプロパティで確認できます。

  • initializing: 接続確立前
  • connected: 接続完了
  • reconnecting: 再接続中