feat: return user info when authenticating
This commit is contained in:
39
src/controllers/auth/anilist/getUser.ts
Normal file
39
src/controllers/auth/anilist/getUser.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
|
||||
const GetUserQuery = graphql(`
|
||||
query GetUser {
|
||||
Viewer {
|
||||
name
|
||||
avatar {
|
||||
medium
|
||||
large
|
||||
}
|
||||
statistics {
|
||||
anime {
|
||||
minutesWatched
|
||||
episodesWatched
|
||||
count
|
||||
meanScore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
export function getUser(aniListToken: string) {
|
||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||
|
||||
return client
|
||||
.request(GetUserQuery, undefined, {
|
||||
Authorization: `Bearer ${aniListToken}`,
|
||||
})
|
||||
.then((data) => data?.Viewer)
|
||||
.catch((err) => {
|
||||
if (err.response?.status === 401 || err.response?.status === 429) {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user