Skip to content

Commit

Permalink
fix: default token
Browse files Browse the repository at this point in the history
  • Loading branch information
nnivxix committed Oct 26, 2024
1 parent 9b1a358 commit de1b72d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions app/discover/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Metadata } from "next";
type Status = "idle" | "pending" | "success" | "error"


const { apiUrl } = config;
const { apiUrl, token } = config;


export const metadata: Metadata = {
Expand All @@ -34,7 +34,7 @@ export default async function Page() {


async function getDiscover(): Promise<{ data: Response<MovieTv[]> | null, status: Status, error: string | null }> {
const apiToken = cookies().get("API_TOKEN");
const apiToken = cookies().get("API_TOKEN")?.value ?? token;
let status: Status = "idle";
let data: Response<MovieTv[]> | null = null;
let error: string | null = null;
Expand All @@ -47,7 +47,7 @@ async function getDiscover(): Promise<{ data: Response<MovieTv[]> | null, status
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
Authorization: `Bearer ${apiToken?.value}`,
Authorization: `Bearer ${apiToken}`,
},
});

Expand Down
6 changes: 3 additions & 3 deletions app/show/movie/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface Authentication {
type Status = "idle" | "pending" | "success" | "error"


const { apiUrl } = config;
const { apiUrl, token } = config;

export async function generateMetadata(
{ params }: Params
Expand Down Expand Up @@ -197,7 +197,7 @@ export default async function Page({ params }: Params) {
}

async function getMovie(movieId: string): Promise<{ movie: MovieResponse | null, status: Status, error: string | null }> {
const apiToken = cookies().get("API_TOKEN");
const apiToken = cookies().get("API_TOKEN")?.value ?? token;
let status: Status = "idle";
let movie: MovieResponse | null = null;
let error: string | null = null;
Expand All @@ -210,7 +210,7 @@ async function getMovie(movieId: string): Promise<{ movie: MovieResponse | null,
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
Authorization: `Bearer ${apiToken?.value}`,
Authorization: `Bearer ${apiToken}`,
},
});

Expand Down
6 changes: 3 additions & 3 deletions app/show/tv/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface Authentication {
type Status = "idle" | "pending" | "success" | "error"


const { apiUrl } = config
const { apiUrl, token } = config

export async function generateMetadata(
{ params }: Params
Expand Down Expand Up @@ -213,7 +213,7 @@ export default async function Page({ params }: Params) {


async function getTvShow(movieId: string): Promise<{ tv: TvResponse | null, status: Status, error: string | null }> {
const apiToken = cookies().get("API_TOKEN");
const apiToken = cookies().get("API_TOKEN")?.value ?? token;
let status: Status = "idle";
let tv: TvResponse | null = null;
let error: string | null = null;
Expand All @@ -226,7 +226,7 @@ async function getTvShow(movieId: string): Promise<{ tv: TvResponse | null, stat
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
Authorization: `Bearer ${apiToken?.value}`,
Authorization: `Bearer ${apiToken}`,
},
});

Expand Down
14 changes: 8 additions & 6 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// import { cookies } from 'next/headers'
// import { get } from 'next-client-cookies';

const tokenStorage =
typeof window !== "undefined" && window.localStorage
? localStorage.getItem("token")
: "";
import { getCookie } from "cookies-next";

// const apiToken = useCookies().get("API_TOKEN")
// const tokenStorage =
// typeof window !== "undefined" && window.localStorage
// ? localStorage.getItem("token")
// : "";

const apiToken = getCookie("API_TOKEN");

const config = {
apiUrl: "https://api.themoviedb.org/3",
token: tokenStorage || process.env.NEXT_PUBLIC_TMDB_API_TOKEN,
token: apiToken || process.env.NEXT_PUBLIC_TMDB_API_TOKEN,
};

export default config;

0 comments on commit de1b72d

Please sign in to comment.