feat: return user info when authenticating
This commit is contained in:
@@ -1,22 +1,34 @@
|
|||||||
import { graphql } from "gql.tada";
|
import { graphql } from "gql.tada";
|
||||||
import { GraphQLClient } from "graphql-request";
|
import { GraphQLClient } from "graphql-request";
|
||||||
|
|
||||||
const GetUsernameQuery = graphql(`
|
const GetUserQuery = graphql(`
|
||||||
query GetUsername {
|
query GetUser {
|
||||||
Viewer {
|
Viewer {
|
||||||
name
|
name
|
||||||
|
avatar {
|
||||||
|
medium
|
||||||
|
large
|
||||||
|
}
|
||||||
|
statistics {
|
||||||
|
anime {
|
||||||
|
minutesWatched
|
||||||
|
episodesWatched
|
||||||
|
count
|
||||||
|
meanScore
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
|
||||||
export function getUsername(aniListToken: string) {
|
export function getUser(aniListToken: string) {
|
||||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||||
|
|
||||||
return client
|
return client
|
||||||
.request(GetUsernameQuery, undefined, {
|
.request(GetUserQuery, undefined, {
|
||||||
Authorization: `Bearer ${aniListToken}`,
|
Authorization: `Bearer ${aniListToken}`,
|
||||||
})
|
})
|
||||||
.then((data) => data?.Viewer?.name)
|
.then((data) => data?.Viewer)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
if (err.response?.status === 401 || err.response?.status === 429) {
|
if (err.response?.status === 401 || err.response?.status === 429) {
|
||||||
return null;
|
return null;
|
||||||
@@ -12,7 +12,7 @@ import { EpisodesResponseSchema } from "~/types/episode";
|
|||||||
import { ErrorResponse, ErrorResponseSchema } from "~/types/schema";
|
import { ErrorResponse, ErrorResponseSchema } from "~/types/schema";
|
||||||
import { Title } from "~/types/title";
|
import { Title } from "~/types/title";
|
||||||
|
|
||||||
import { getUsername } from "./getUsername";
|
import { getUser } from "./getUser";
|
||||||
import { getWatchingTitles } from "./getWatchingTitles";
|
import { getWatchingTitles } from "./getWatchingTitles";
|
||||||
|
|
||||||
const route = createRoute({
|
const route = createRoute({
|
||||||
@@ -69,22 +69,28 @@ app.openapi(route, async (c) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const username = await getUsername(aniListToken);
|
const user = await getUser(aniListToken);
|
||||||
if (!username) {
|
if (!user) {
|
||||||
return c.json(ErrorResponse, { status: 401 });
|
return c.json(ErrorResponse, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
await associateDeviceIdWithUsername(env(c, "workerd"), deviceId, username);
|
await associateDeviceIdWithUsername(
|
||||||
|
env(c, "workerd"),
|
||||||
|
deviceId!,
|
||||||
|
user.name,
|
||||||
|
);
|
||||||
|
|
||||||
return streamSSE(
|
return streamSSE(
|
||||||
c,
|
c,
|
||||||
async (stream) => {
|
async (stream) => {
|
||||||
|
stream.writeSSE({ event: "user", data: JSON.stringify(user) });
|
||||||
|
|
||||||
let currentPage = 1;
|
let currentPage = 1;
|
||||||
let hasNextPage = true;
|
let hasNextPage = true;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
const { mediaList, pageInfo } = await getWatchingTitles(
|
const { mediaList, pageInfo } = await getWatchingTitles(
|
||||||
username,
|
user.name,
|
||||||
currentPage++,
|
currentPage++,
|
||||||
c.executionCtx,
|
c.executionCtx,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user