From f056a1e19470967198b1dab61cf6133047f07afd Mon Sep 17 00:00:00 2001 From: Fuwn Date: Fri, 15 Dec 2023 23:54:53 -0800 Subject: feat(notifications): refresh token --- src/lib/AniList/notifications.ts | 33 +++++++++-------- src/routes/api/oauth/refresh/+server.ts | 43 +++++++++------------- src/routes/feeds/activity-notifications/+server.ts | 14 ++++++- src/routes/settings/+page.svelte | 6 ++- 4 files changed, 52 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/lib/AniList/notifications.ts b/src/lib/AniList/notifications.ts index 4fdb9cb5..bebf1b42 100644 --- a/src/lib/AniList/notifications.ts +++ b/src/lib/AniList/notifications.ts @@ -30,7 +30,7 @@ export interface Notification { | 'THREAD_LIKE'; } -export const notifications = async (accessToken: string): Promise => { +export const notifications = async (accessToken: string): Promise => { const activityNotification = (type: string, extend = '') => `... on ${type} { id user { name avatar { large } } context createdAt type ${extend} }`; @@ -45,18 +45,16 @@ export const notifications = async (accessToken: string): Promise `${activityNotification(type, `thread { title id }`)}`; - - return ( - await ( - await fetch('https://graphql.anilist.co', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${accessToken}`, - Accept: 'application/json' - }, - body: JSON.stringify({ - query: `{ Page(page: 1, perPage: 50) { notifications { + const data = await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${accessToken}`, + Accept: 'application/json' + }, + body: JSON.stringify({ + query: `{ Page(page: 1, perPage: 50) { notifications { ${activityNotification('FollowingNotification')} ${activityNotification('ActivityMessageNotification')} ${richActivityNotification('ActivityMentionNotification')} @@ -70,8 +68,11 @@ export const notifications = async (accessToken: string): Promise { +export const GET = async ({ url, cookies }) => { const formData = new FormData(); formData.append('grant_type', 'refresh_token'); formData.append('client_id', env2.PUBLIC_ANILIST_CLIENT_ID as string); formData.append('client_secret', env.ANILIST_CLIENT_SECRET as string); - formData.append( - 'refresh_token', - JSON.parse(cookies.get('user') || '{ refresh_token: null }')['refresh_token'] - ); - cookies.set( - 'user', - JSON.stringify( - await ( - await fetch('https://anilist.co/api/v2/oauth/token', { - method: 'POST', - body: formData - }) - ).json() - ), - { - path: '/', - maxAge: 60 * 60 * 24 * 7, - httpOnly: true, - sameSite: 'lax', - secure: !dev - } - ); + formData.append('refresh_token', url.searchParams.get('token') || ''); - throw redirect(303, '/'); + const newUser = await ( + await fetch('https://anilist.co/api/v2/oauth/token', { + method: 'POST', + body: formData + }) + ).json(); + + cookies.set('user', JSON.stringify(newUser), { + path: '/', + maxAge: 60 * 60 * 24 * 7, + httpOnly: true, + sameSite: 'lax', + secure: !dev + }); + + return Response.json(newUser); }; diff --git a/src/routes/feeds/activity-notifications/+server.ts b/src/routes/feeds/activity-notifications/+server.ts index fde2027d..f0482194 100644 --- a/src/routes/feeds/activity-notifications/+server.ts +++ b/src/routes/feeds/activity-notifications/+server.ts @@ -59,9 +59,19 @@ const render = (posts: Notification[] = []) => ` { - const token = url.searchParams.get('token'); + let token = url.searchParams.get('token'); + const refresh = url.searchParams.get('refresh'); + let notification = await notifications(token || ''); - return new Response(token ? render(await notifications(token)) : render(), { + if (notification === null) { + token = ( + await (await fetch(`https://192.168.1.60:5173/api/oauth/refresh?token=${refresh}`)).json() + )['access_token']; + + notification = await notifications(token as string); + } + + return new Response(token ? render(notification || []) : render(), { headers: { 'Cache-Control': `max-age=0, s-max-age=0`, 'Content-Type': 'application/xml' diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte index a7db1390..2cb077e8 100644 --- a/src/routes/settings/+page.svelte +++ b/src/routes/settings/+page.svelte @@ -67,7 +67,11 @@ href={'#'} on:click={() => navigator.clipboard.writeText( - `https://due.moe/feeds/activity-notifications?token=${data.user.accessToken}` + `https://${ + env.PUBLIC_ANILIST_REDIRECT_URI?.includes('192.168') ? '192.168.1.60:5173' : 'due.moe' + }/feeds/activity-notifications?token=${data.user.accessToken}&refresh=${ + data.user.refreshToken + }` )} > Click to copy AniList notifications RSS feed URL and token -- cgit v1.2.3