Open2

NextAuth/Auth.jsのMisskeyログインを実装してみる

泡沫京水泡沫京水

いったんChatGPTに聞いてみる
https://chatgpt.com/share/67bb001a-8954-800a-ab50-313308eab04d

import NextAuth from "next-auth";
import type { NextAuthOptions } from "next-auth";
import { OAuthConfig, User } from "next-auth/providers/oauth";

const MisskeyProvider = (options: { instanceUrl: string }) => {
  return {
    id: "misskey",
    name: "Misskey",
    type: "oauth",
    authorization: {
      url: `${options.instanceUrl}/oauth/authorize`,
      params: { scope: "read:account" }, // 必要に応じてスコープを変更
    },
    token: `${options.instanceUrl}/oauth/token`,
    userinfo: `${options.instanceUrl}/api/i`, // Misskeyのユーザー情報API
    clientId: process.env.MISSKEY_CLIENT_ID,
    clientSecret: process.env.MISSKEY_CLIENT_SECRET,
    profile(profile) {
      return {
        id: profile.id,
        name: profile.username,
        email: profile.email || null,
        image: profile.avatarUrl,
      };
    },
  } satisfies OAuthConfig<User>;
};

export const authOptions: NextAuthOptions = {
  providers: [
    MisskeyProvider({ instanceUrl: "https://misskey.io" }), // インスタンスURLを変更可能
  ],
  secret: process.env.NEXTAUTH_SECRET,
};

export default NextAuth(authOptions);