Skip to content

Instantly share code, notes, and snippets.

@tai-cha
Last active July 26, 2024 09:34
Show Gist options
  • Save tai-cha/aeed508d9959dd0fc8e9c60361759139 to your computer and use it in GitHub Desktop.
Save tai-cha/aeed508d9959dd0fc8e9c60361759139 to your computer and use it in GitHub Desktop.
misskey providor example for next-auth

About This Gist

This gist is a modified version of next-auth's mastodon provider. The original project is licensed under the ISC License.

Modifications

  • Port Provider to Misskey (draft)

About the Original Project

The original project is next-auth. We appreciate the excellent work done by the original authors.

ISC License
Copyright (c) 2022-2024, Balázs Orbán
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import type { OAuthConfig, OAuthUserConfig } from "next-auth/providers/oauth";
export interface MisskeyProfile extends Record<string, any> {
id: string
name: string
username: string
avatarUrl: string
description: string
isBot: boolean
isCat: boolean
isLocked: boolean
isSilenced: boolean
isSuspendend: boolean
createdAt: string
followersCount: number
followingCount: number
notesCount: number
email: string | null
isDeleted: boolean
}
/**
* Add Misskey login to your page.
*
* ### Setup
*
* #### Callback URL
* ```
* https://example.com/api/auth/callback/misskey
* ```
*
* #### Configuration
*```js
* import Auth from "@auth/core"
* import Misskey from "@/providers/misskey"
*
* const request = new Request(origin)
* const response = await Auth(request, {
* providers: [Misskey({ clientId: MISSKEY_CLIENT_ID, issuer: MISSKEY_ISSUER })],
* })
* ```
*
* ### Resources
*
* - [Misskey OAuth documentation](https://misskey-hub.net/ja/docs/for-developers/api/token/oauth/)
*
*/
export default function Misskey<P extends MisskeyProfile>(
options: Omit<Partial<OAuthUserConfig<P>>, "options" | "type" > & {
clientId: string
scope?: string
issuer: string
}
): OAuthConfig<P> {
return {
id: "misskey",
name: "Misskey",
type: "oauth",
version: "2.0",
wellKnown: `${options.issuer}/.well-known/oauth-authorization-server`,
checks: ["pkce", "state"],
clientId: options.clientId ?? "http://localhost:3000/",
authorization: {
params: {
client_id: options.clientId ?? "http://localhost:3000/",
scope: options.scope ?? "read:account",
response_type: "code",
}
},
userinfo: `${options.issuer}/api/i`,
profile(profile) {
return {
id: profile.id,
name: profile.username,
image: profile.avatar_static,
email: null,
}
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment