Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Mar 28, 2024
2 parents b5697e9 + eda8396 commit a12175a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changesets/w34ma.minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add AniList provider.
1 change: 1 addition & 0 deletions docs/malta.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"title": "Providers",
"pages": [
["Amazon Cognito", "/providers/amazon-cognito"],
["AniList", "/providers/anilist"],
["Apple", "/providers/apple"],
["Atlassian", "/providers/atlassian"],
["Auth0", "/providers/auth0"],
Expand Down
18 changes: 18 additions & 0 deletions docs/pages/providers/anilist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: "AniList"
---

# AniList

For usage, see [OAuth 2.0 provider](/guides/oauth2).

```ts
import { AniList } from "arctic";

const anilist = new AniList(clientId, clientSecret, redirectURI);
```

```ts
const url: URL = await anilist.createAuthorizationURL(state);
const tokens: AniListTokens = await anilist.validateAuthorizationCode(code);
```
2 changes: 1 addition & 1 deletion docs/pages/providers/atlassian.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const url = await atlassian.createAuthorizationURL(state, {
```

```ts
const tokens = await apple.validateAuthorizationCode(code);
const tokens = await atlassian.validateAuthorizationCode(code);
const response = await fetch("https://api.atlassian.com/me", {
headers: {
Authorization: `Bearer ${tokens.accessToken}`
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { AmazonCognito } from "./providers/amazon-cognito.js";
export { AniList } from "./providers/anilist.js";
export { Apple } from "./providers/apple.js";
export { Atlassian } from "./providers/atlassian.js";
export { Auth0 } from "./providers/auth0.js";
Expand Down Expand Up @@ -44,6 +45,7 @@ export type {
AmazonCognitoRefreshedTokens,
AmazonCognitoTokens
} from "./providers/amazon-cognito.js";
export type { AniListTokens } from "./providers/anilist.js";
export type { AppleCredentials, AppleRefreshedTokens, AppleTokens } from "./providers/apple.js";
export type { AtlassianTokens } from "./providers/atlassian.js";
export type { Auth0Tokens } from "./providers/auth0.js";
Expand Down
41 changes: 41 additions & 0 deletions src/providers/anilist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { OAuth2Client } from "oslo/oauth2";
import type { OAuth2Provider } from "../index.js";

const authorizeEndpoint = "https://anilist.co/api/v2/oauth/authorize";
const tokenEndpoint = "https://anilist.co/api/v2/oauth/token";

export class AniList implements OAuth2Provider {
private client: OAuth2Client;
private clientSecret: string;

constructor(clientId: string, clientSecret: string, redirectURI: string) {
this.client = new OAuth2Client(clientId, authorizeEndpoint, tokenEndpoint, {
redirectURI
});
this.clientSecret = clientSecret;
}

public async createAuthorizationURL(state: string): Promise<URL> {
return await this.client.createAuthorizationURL({
state
});
}

public async validateAuthorizationCode(code: string): Promise<AniListTokens> {
const result = await this.client.validateAuthorizationCode<TokenResponseBody>(code, {
credentials: this.clientSecret
});
const tokens: AniListTokens = {
accessToken: result.access_token
};
return tokens;
}
}

interface TokenResponseBody {
access_token: string;
}

export interface AniListTokens {
accessToken: string;
}

0 comments on commit a12175a

Please sign in to comment.