aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList/identity.ts
blob: 5844bd6687e0809ed35c666df573d9efff6a5a4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export interface UserIdentity {
	id: number;
	name: string;
}

export interface AniListAuthorisation {
	tokenType: string;
	accessToken: string;
	expiresIn: number;
	refreshToken: string;
}

export const userIdentity = async (
	anilistAuthorisation: AniListAuthorisation
): Promise<UserIdentity> => {
	const userIdResponse = await (
		await fetch('https://graphql.anilist.co', {
			method: 'POST',
			headers: {
				Authorization: `${anilistAuthorisation.tokenType} ${anilistAuthorisation.accessToken}`,
				'Content-Type': 'application/json',
				Accept: 'application/json'
			},
			body: JSON.stringify({ query: `{ Viewer { id name } }` })
		})
	).json();

	console.log(userIdResponse['data']['Viewer']['name']);

	return {
		id: userIdResponse['data']['Viewer']['id'],
		name: userIdResponse['data']['Viewer']['name']
	};
};