Merge pull request #718 from hydralauncher/feat/better-api-logs-and-handle-401

feat: better api logs and handle 401
This commit is contained in:
Zamitto 2024-06-28 16:42:42 -03:00 committed by GitHub
commit c12f16f59e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 31 deletions

View file

@ -24,7 +24,7 @@ const getMe = async (
return me; return me;
}) })
.catch((err) => { .catch((err) => {
logger.error("getMe", err); logger.error("getMe", err.message);
return userAuthRepository.findOne({ where: { id: 1 } }); return userAuthRepository.findOne({ where: { id: 1 } });
}); });
}; };

View file

@ -11,20 +11,22 @@ const autoLaunch = async (
) => { ) => {
if (!app.isPackaged) return; if (!app.isPackaged) return;
const appLauncher = new AutoLaunch({
name: app.getName(),
});
if (process.platform == "win32") { if (process.platform == "win32") {
const destination = path.join(windowsStartupPath, "hydralauncher.vbs"); const destination = path.join(windowsStartupPath, "Hydra.vbs");
if (enabled) { if (enabled) {
const scriptPath = path.join(process.resourcesPath, "hydralauncher.vbs"); const scriptPath = path.join(process.resourcesPath, "hydralauncher.vbs");
fs.copyFileSync(scriptPath, destination); fs.copyFileSync(scriptPath, destination);
} else { } else {
appLauncher.disable().catch();
fs.rmSync(destination); fs.rmSync(destination);
} }
} else { } else {
const appLauncher = new AutoLaunch({
name: app.getName(),
});
if (enabled) { if (enabled) {
appLauncher.enable().catch(); appLauncher.enable().catch();
} else { } else {

View file

@ -90,7 +90,21 @@ export class HydraApi {
return response; return response;
}, },
(error) => { (error) => {
logger.error("response error", error); logger.error(" ---- RESPONSE ERROR -----");
const { config } = error;
logger.error(config.method, config.baseURL, config.url, config.headers);
if (error.response) {
logger.error(error.response.status, error.response.data);
} else if (error.request) {
logger.error(error.request);
} else {
logger.error("Error", error.message);
}
logger.error(" ----- END RESPONSE ERROR -------");
return Promise.reject(error); return Promise.reject(error);
} }
); );
@ -146,24 +160,7 @@ export class HydraApi {
["id"] ["id"]
); );
} catch (err) { } catch (err) {
if ( this.handleUnauthorizedError(err);
err instanceof AxiosError &&
(err?.response?.status === 401 || err?.response?.status === 403)
) {
this.userAuth = {
authToken: "",
expirationTimestamp: 0,
refreshToken: "",
};
userAuthRepository.delete({ id: 1 });
this.sendSignOutEvent();
logger.log("user refresh token expired");
}
throw err;
} }
} }
} }
@ -176,28 +173,54 @@ export class HydraApi {
}; };
} }
private static handleUnauthorizedError = (err) => {
if (err instanceof AxiosError && err.response?.status === 401) {
this.userAuth = {
authToken: "",
expirationTimestamp: 0,
refreshToken: "",
};
userAuthRepository.delete({ id: 1 });
this.sendSignOutEvent();
}
throw err;
};
static async get(url: string) { static async get(url: string) {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance.get(url, this.getAxiosConfig()); return this.instance
.get(url, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
} }
static async post(url: string, data?: any) { static async post(url: string, data?: any) {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance.post(url, data, this.getAxiosConfig()); return this.instance
.post(url, data, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
} }
static async put(url: string, data?: any) { static async put(url: string, data?: any) {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance.put(url, data, this.getAxiosConfig()); return this.instance
.put(url, data, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
} }
static async patch(url: string, data?: any) { static async patch(url: string, data?: any) {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance.patch(url, data, this.getAxiosConfig()); return this.instance
.patch(url, data, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
} }
static async delete(url: string) { static async delete(url: string) {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance.delete(url, this.getAxiosConfig()); return this.instance
.delete(url, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
} }
} }

View file

@ -64,7 +64,7 @@ export const mergeWithRemoteGames = async () => {
} }
} catch (err) { } catch (err) {
if (err instanceof AxiosError) { if (err instanceof AxiosError) {
logger.error("getRemoteGames", err.response, err.message); logger.error("getRemoteGames", err.message);
} else { } else {
logger.error("getRemoteGames", err); logger.error("getRemoteGames", err);
} }

View file

@ -1,6 +1,6 @@
import { UserProfile } from "@types"; import { UserProfile } from "@types";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import { useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { setHeaderTitle } from "@renderer/features"; import { setHeaderTitle } from "@renderer/features";
import { useAppDispatch } from "@renderer/hooks"; import { useAppDispatch } from "@renderer/hooks";
import { UserSkeleton } from "./user-skeleton"; import { UserSkeleton } from "./user-skeleton";
@ -12,6 +12,7 @@ import * as styles from "./user.css";
export const User = () => { export const User = () => {
const { userId } = useParams(); const { userId } = useParams();
const [userProfile, setUserProfile] = useState<UserProfile>(); const [userProfile, setUserProfile] = useState<UserProfile>();
const navigate = useNavigate();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
@ -20,6 +21,8 @@ export const User = () => {
if (userProfile) { if (userProfile) {
dispatch(setHeaderTitle(userProfile.displayName)); dispatch(setHeaderTitle(userProfile.displayName));
setUserProfile(userProfile); setUserProfile(userProfile);
} else {
navigate(-1);
} }
}); });
}, [dispatch, userId]); }, [dispatch, userId]);