fix: authorization
This commit is contained in:
1 parent
a3fb95eca1
commit
fbbd7b52e3
7 files changed
+54
-36
No files matched your search
@@ -25,17 +25,23 @@ class Http {
|
||||
body?: B;
|
||||
preventCache?: boolean;
|
||||
}): Promise<T> {
|
||||
const serializedParams = new URLSearchParams(
|
||||
const searchParams = new URLSearchParams(
|
||||
Object.entries(search ?? {}).reduce((params, [key, value]) => {
|
||||
if (value != null && value !== '') {
|
||||
params.set(key, String(value));
|
||||
}
|
||||
return params;
|
||||
}, new URLSearchParams())
|
||||
).toString();
|
||||
);
|
||||
|
||||
if (preventCache) {
|
||||
searchParams.append('preventCache', '1');
|
||||
}
|
||||
|
||||
const searchParamsString = searchParams.toString();
|
||||
|
||||
try {
|
||||
const response = await fetch(`${base}${url}${serializedParams ? `?${serializedParams}` : ''}`, {
|
||||
const response = await fetch(`${base}${url}${searchParamsString ? `?${searchParamsString}` : ''}`, {
|
||||
method,
|
||||
headers: {
|
||||
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