aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/oauth/refresh/+server.ts
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-12-15 23:54:53 -0800
committerFuwn <[email protected]>2023-12-15 23:54:53 -0800
commitf056a1e19470967198b1dab61cf6133047f07afd (patch)
treeea3ac4647037c257d1866aff7ff23cf64d9f9f3b /src/routes/api/oauth/refresh/+server.ts
parentfeat(media): multiple dueincludes (diff)
downloaddue.moe-f056a1e19470967198b1dab61cf6133047f07afd.tar.xz
due.moe-f056a1e19470967198b1dab61cf6133047f07afd.zip
feat(notifications): refresh token
Diffstat (limited to 'src/routes/api/oauth/refresh/+server.ts')
-rw-r--r--src/routes/api/oauth/refresh/+server.ts43
1 files changed, 18 insertions, 25 deletions
diff --git a/src/routes/api/oauth/refresh/+server.ts b/src/routes/api/oauth/refresh/+server.ts
index 0e6eef8c..ceb2566d 100644
--- a/src/routes/api/oauth/refresh/+server.ts
+++ b/src/routes/api/oauth/refresh/+server.ts
@@ -1,36 +1,29 @@
import { dev } from '$app/environment';
import { env } from '$env/dynamic/private';
import { env as env2 } from '$env/dynamic/public';
-import { redirect } from '@sveltejs/kit';
-export const GET = async ({ cookies }) => {
+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);
};