Public Access
refactor(auth): send login token through apiFetch
All authenticated calls now use apiFetch, and only utils/auth.js touches the stored token. The cookie login can then change these two files instead of every page.
This commit is contained in:
1 parent
085fcf15a4
commit
5ff8d48c53
18 files changed
+127
-70
No files matched your search
+12
-13
@@ -25,6 +25,8 @@ import SteamAvatar from '../components/SteamAvatar.jsx';
|
||||
import useSteamProfile from '../hooks/useSteamProfile.js';
|
||||
import {cn} from '@/lib/utils';
|
||||
import {usePreferences} from '../context/PreferencesContext.jsx';
|
||||
import {apiFetch} from '../utils/api.js';
|
||||
import {getToken} from '../utils/auth.js';
|
||||
|
||||
const TOP_GAMES = 5;
|
||||
// The games endpoint accepts only the page sizes 20, 40 and 80.
|
||||
@@ -32,10 +34,7 @@ const TOP_GAMES_URL = '/api/library/games?page=0&size=20&sort=playtimeMinutes%2C
|
||||
const PLAYING_GAMES = 4;
|
||||
const PLAYING_URL = '/api/library/games?page=0&size=20&sort=playtimeMinutes%2Cdesc&status=PLAYING';
|
||||
|
||||
const authHeaders = () => ({
|
||||
'Authorization': `Bearer ${localStorage.getItem('tbp_jwt')}`,
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
const JSON_HEADERS = {'Content-Type': 'application/json'};
|
||||
|
||||
const Dashboard = () => {
|
||||
const {t} = useTranslation();
|
||||
@@ -55,7 +54,7 @@ const Dashboard = () => {
|
||||
|
||||
const fetchSteamCount = useCallback(async () => {
|
||||
try {
|
||||
const r = await fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/games-count`, {headers: authHeaders()});
|
||||
const r = await apiFetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/games-count`, {headers: JSON_HEADERS});
|
||||
if (r.status === 401) {
|
||||
expireSession();
|
||||
return;
|
||||
@@ -70,7 +69,7 @@ const Dashboard = () => {
|
||||
|
||||
const fetchTopGames = useCallback(async () => {
|
||||
try {
|
||||
const r = await fetch(`${import.meta.env.VITE_CORE_API_URL}${TOP_GAMES_URL}`, {headers: authHeaders()});
|
||||
const r = await apiFetch(`${import.meta.env.VITE_CORE_API_URL}${TOP_GAMES_URL}`, {headers: JSON_HEADERS});
|
||||
if (r.status === 401) {
|
||||
expireSession();
|
||||
return;
|
||||
@@ -86,7 +85,7 @@ const Dashboard = () => {
|
||||
|
||||
const fetchPlaying = useCallback(async () => {
|
||||
try {
|
||||
const r = await fetch(`${import.meta.env.VITE_CORE_API_URL}${PLAYING_URL}`, {headers: authHeaders()});
|
||||
const r = await apiFetch(`${import.meta.env.VITE_CORE_API_URL}${PLAYING_URL}`, {headers: JSON_HEADERS});
|
||||
if (r.status === 401) {
|
||||
expireSession();
|
||||
return;
|
||||
@@ -107,7 +106,7 @@ const Dashboard = () => {
|
||||
const fetchSummary = useCallback(async (excludeAppid) => {
|
||||
const query = excludeAppid ? `?exclude=${excludeAppid}` : '';
|
||||
try {
|
||||
const r = await fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/summary${query}`, {headers: authHeaders()});
|
||||
const r = await apiFetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/summary${query}`, {headers: JSON_HEADERS});
|
||||
if (r.status === 401) {
|
||||
expireSession();
|
||||
return true;
|
||||
@@ -126,7 +125,7 @@ const Dashboard = () => {
|
||||
|
||||
const fetchActivity = useCallback(async () => {
|
||||
try {
|
||||
const r = await fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/activity`, {headers: authHeaders()});
|
||||
const r = await apiFetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/activity`, {headers: JSON_HEADERS});
|
||||
if (r.status === 401) {
|
||||
expireSession();
|
||||
return;
|
||||
@@ -141,7 +140,7 @@ const Dashboard = () => {
|
||||
|
||||
const fetchAchievements = useCallback(async () => {
|
||||
try {
|
||||
const r = await fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/achievements/summary`, {headers: authHeaders()});
|
||||
const r = await apiFetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/achievements/summary`, {headers: JSON_HEADERS});
|
||||
if (r.status === 401) {
|
||||
expireSession();
|
||||
return;
|
||||
@@ -173,15 +172,15 @@ const Dashboard = () => {
|
||||
|
||||
useEffect(() => {
|
||||
const fetchUserData = async () => {
|
||||
if (!localStorage.getItem('tbp_jwt')) {
|
||||
if (!getToken()) {
|
||||
navigate('/login');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const profileResponse = await fetch(`${import.meta.env.VITE_API_URL}/api/users/me?v=1`, {
|
||||
const profileResponse = await apiFetch(`${import.meta.env.VITE_API_URL}/api/users/me?v=1`, {
|
||||
method: 'GET',
|
||||
headers: authHeaders(),
|
||||
headers: JSON_HEADERS,
|
||||
});
|
||||
|
||||
if (profileResponse.ok) {
|
||||
|
||||
Reference in new issue
Block a user