Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(misskey-js): MiAuth認証ラッパー #13784

Closed

Conversation

kakkokari-gtyih
Copy link
Contributor

@kakkokari-gtyih kakkokari-gtyih commented May 2, 2024

What

Step 1: 認証URLを生成

APIClientクラスのgetMiAuthURLメソッドを使用して認証URLを生成します(これは同期関数です)。生成したURLにユーザーを誘導し、認可させてください。

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

const { url } = cli.getMiAuthURL({
	name: 'My app',
	callback: 'https://example.com/callback',
	permission: ['read:account'],
});

// URLに飛ばす(例)
location.href = url;

Step 2: セッションIDからアクセストークンを取得

APIClientクラスのauthWithMiAuthメソッドを使用してセッションIDからアクセストークンを取得します。アクセストークンは返却されるほか、以降同一のインスタンスを利用したリクエストに自動で設定されます(この挙動は第2引数にfalseを与えることで回避できます)。

コールバックURLを指定した場合、セッションIDはURLパラメータのsessionから取得できます。

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

const { token } = await cli.authWithMiAuth(sessionId);

// 以後、同じAPIClientを使い続ける場合はトークンが自動で設定されます
const i = await cli.request('i');

Why

認証を簡単にする

Additional info (optional)

APIClient に統一しないほうが良かったかも

Checklist

  • Read the contribution guide
  • Test working in a local environment
  • (If needed) Add story of storybook
  • (If needed) Update CHANGELOG.md
  • (If possible) Add tests

Copy link

codecov bot commented May 2, 2024

Codecov Report

Attention: Patch coverage is 12.96296% with 47 lines in your changes are missing coverage. Please review.

Project coverage is 66.45%. Comparing base (c530a46) to head (fe0ace5).

Files Patch % Lines
packages/misskey-js/src/api.ts 12.96% 47 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #13784      +/-   ##
===========================================
+ Coverage    65.15%   66.45%   +1.30%     
===========================================
  Files          985      988       +3     
  Lines       112122   116715    +4593     
  Branches      5742     5769      +27     
===========================================
+ Hits         73050    77561    +4511     
- Misses       37638    37720      +82     
  Partials      1434     1434              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant