-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from KNU-AEYE/27-token-expired-message
27-token-expired-message
- Loading branch information
Showing
4 changed files
with
70 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,16 @@ | ||
"use client"; | ||
|
||
import { useEffect } from "react"; | ||
import fetchWithInterception from "@/app/fetchWrapper"; | ||
|
||
export default function Cams() { | ||
const fetchVids = async () => { | ||
try { | ||
const res = await fetch("https://api.a-eye.live/video", { | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${localStorage.getItem("accessToken")}`, | ||
}, | ||
}); | ||
if (res.ok) { | ||
const jsonData = await res.json(); | ||
console.log(jsonData.data); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchVids(); | ||
fetchWithInterception("https://api.a-eye.live/video", { | ||
method: "GET", | ||
}) | ||
.then((response) => response.json()) | ||
.then((jsonData) => console.log(jsonData.data)) | ||
.catch((error) => console.error(error)); | ||
}, []); | ||
return <div></div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Example usage: | ||
// fetchWithInterception(apiUrl, { method: 'GET' }) | ||
// .then(response => response.json()) | ||
// .then(data => console.log(data)) | ||
// .catch(error => console.error(error)); | ||
|
||
const fetchWithInterception = async ( | ||
url: string, | ||
options?: RequestInit | ||
): Promise<Response> => { | ||
const modifiedOptions = { ...options }; | ||
modifiedOptions.headers = { | ||
...modifiedOptions.headers, | ||
Authorization: `Bearer ${localStorage.getItem("accessToken")}`, | ||
}; | ||
return fetch(url, modifiedOptions) | ||
.then(async (response: Response) => { | ||
if (!response.ok) { | ||
if (response.status === 401) { | ||
alert("토근이 만료되었습니다. 다시 로그인해주세요."); | ||
window.location.href = "/"; | ||
} | ||
throw new Error("Request failed"); | ||
} | ||
return response; | ||
}) | ||
.catch((error: Error) => { | ||
console.error("Fetch error:", error); | ||
throw error; | ||
}); | ||
}; | ||
|
||
export default fetchWithInterception; |