fix: authorization
This commit is contained in:
1 parent
a3fb95eca1
commit
fbbd7b52e3
7 files changed
+54
-36
No files matched your search
@@ -16,7 +16,7 @@ export async function GET({ nextUrl: { searchParams } }: NextRequest, { params:
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${MOVIE_DB_TOKEN}`
|
Authorization: `Bearer ${MOVIE_DB_TOKEN}`
|
||||||
},
|
},
|
||||||
next: searchParams.get('session_id') ? undefined : { revalidate: 3600 }
|
next: searchParams.get('preventCache') || searchParams.get('session_id') ? undefined : { revalidate: 3600 }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
import { useMutation } from '@tanstack/react-query';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import sessionMutation from '@/data/mutations/session';
|
||||||
|
import tokenMutation from '@/data/mutations/token';
|
||||||
import { useSetSession } from '@/hooks/useSession';
|
import { useSetSession } from '@/hooks/useSession';
|
||||||
|
import { useToast } from '@/hooks/useToast';
|
||||||
import { createRequestToken, createSession } from '../../../../data';
|
|
||||||
|
|
||||||
const useAuth = () => {
|
const useAuth = () => {
|
||||||
const setSession = useSetSession();
|
const setSession = useSetSession();
|
||||||
const tabRef = useRef<Window | null>(null);
|
const tabRef = useRef<Window | null>(null);
|
||||||
const intervalRef = useRef<NodeJS.Timeout>();
|
const intervalRef = useRef<NodeJS.Timeout>();
|
||||||
|
const retryRef = useRef(10);
|
||||||
|
|
||||||
|
const { toast } = useToast();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const interval = intervalRef.current;
|
const interval = intervalRef.current;
|
||||||
@@ -16,17 +20,28 @@ const useAuth = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const session = useMutation({
|
const session = useMutation({
|
||||||
mutationFn: createSession,
|
...sessionMutation(),
|
||||||
onSuccess({ session_id }) {
|
onSuccess({ session_id }) {
|
||||||
setSession(session_id);
|
setSession(session_id);
|
||||||
clearInterval(intervalRef.current);
|
clearInterval(intervalRef.current);
|
||||||
|
|
||||||
tabRef.current?.close();
|
tabRef.current?.close();
|
||||||
|
},
|
||||||
|
onError() {
|
||||||
|
if (!retryRef.current) {
|
||||||
|
toast({ title: 'Authorization failed', description: 'Try to authorize later' });
|
||||||
|
|
||||||
|
tabRef.current?.close();
|
||||||
|
|
||||||
|
return clearInterval(intervalRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
retryRef.current--;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const requestToken = useMutation({
|
const requestToken = useMutation({
|
||||||
mutationFn: createRequestToken,
|
...tokenMutation(),
|
||||||
onMutate() {
|
onMutate() {
|
||||||
tabRef.current = window.open('', '_blank');
|
tabRef.current = window.open('', '_blank');
|
||||||
tabRef.current?.focus();
|
tabRef.current?.focus();
|
||||||
|
|||||||
@@ -25,17 +25,23 @@ class Http {
|
|||||||
body?: B;
|
body?: B;
|
||||||
preventCache?: boolean;
|
preventCache?: boolean;
|
||||||
}): Promise<T> {
|
}): Promise<T> {
|
||||||
const serializedParams = new URLSearchParams(
|
const searchParams = new URLSearchParams(
|
||||||
Object.entries(search ?? {}).reduce((params, [key, value]) => {
|
Object.entries(search ?? {}).reduce((params, [key, value]) => {
|
||||||
if (value != null && value !== '') {
|
if (value != null && value !== '') {
|
||||||
params.set(key, String(value));
|
params.set(key, String(value));
|
||||||
}
|
}
|
||||||
return params;
|
return params;
|
||||||
}, new URLSearchParams())
|
}, new URLSearchParams())
|
||||||
).toString();
|
);
|
||||||
|
|
||||||
|
if (preventCache) {
|
||||||
|
searchParams.append('preventCache', '1');
|
||||||
|
}
|
||||||
|
|
||||||
|
const searchParamsString = searchParams.toString();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${base}${url}${serializedParams ? `?${serializedParams}` : ''}`, {
|
const response = await fetch(`${base}${url}${searchParamsString ? `?${searchParamsString}` : ''}`, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: this.authorization || '',
|
Authorization: this.authorization || '',
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
import { MOVIE_DB_API_URL, MOVIE_DB_TOKEN } from '@/env';
|
|
||||||
|
|
||||||
import Http from './Http';
|
|
||||||
|
|
||||||
const movieDB = new Http(MOVIE_DB_API_URL, `Bearer ${MOVIE_DB_TOKEN}`);
|
|
||||||
|
|
||||||
export default movieDB;
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
'use server';
|
|
||||||
|
|
||||||
import { cookies } from 'next/headers';
|
|
||||||
|
|
||||||
import movieDB from './clients/movieDB';
|
|
||||||
|
|
||||||
export const session = async () => cookies().get('session')?.value;
|
|
||||||
|
|
||||||
export const createRequestToken = async () => {
|
|
||||||
return movieDB.get<RequestToken>('/authentication/token/new', { preventCache: true }).then(({ request_token }) => ({
|
|
||||||
redirectUrl: `https://www.themoviedb.org/authenticate/${request_token}`,
|
|
||||||
requestToken: request_token
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createSession = async ({ requestToken }: { requestToken: string }) => {
|
|
||||||
return movieDB.post<Session>('/authentication/session/new', {
|
|
||||||
params: { request_token: requestToken },
|
|
||||||
preventCache: true
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import movieAdvisor from '../clients/movieAdvisor';
|
||||||
|
|
||||||
|
const sessionMutation = () => ({
|
||||||
|
mutationFn: ({ requestToken }: { requestToken: string }) =>
|
||||||
|
movieAdvisor.post<Session>(
|
||||||
|
'/authentication/session/new',
|
||||||
|
{
|
||||||
|
preventCache: true
|
||||||
|
},
|
||||||
|
{ request_token: requestToken }
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
export default sessionMutation;
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import movieAdvisor from '../clients/movieAdvisor';
|
||||||
|
|
||||||
|
const tokenMutation = () => ({
|
||||||
|
mutationFn: () =>
|
||||||
|
movieAdvisor.get<RequestToken>('/authentication/token/new', { preventCache: true }).then(({ request_token }) => ({
|
||||||
|
redirectUrl: `https://www.themoviedb.org/authenticate/${request_token}`,
|
||||||
|
requestToken: request_token
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
|
||||||
|
export default tokenMutation;
|
||||||
Reference in new issue
Block a user