mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: handle login from deeplink
This commit is contained in:
parent
55c214eae6
commit
32566e5dfc
17 changed files with 243 additions and 62 deletions
|
@ -1,5 +1,6 @@
|
|||
import { userAuthRepository } from "@main/repository";
|
||||
import axios, { AxiosError, AxiosInstance } from "axios";
|
||||
import { WindowManager } from "./window-manager";
|
||||
|
||||
export class HydraApi {
|
||||
private static instance: AxiosInstance;
|
||||
|
@ -16,6 +17,38 @@ export class HydraApi {
|
|||
return this.userAuth.authToken !== "";
|
||||
}
|
||||
|
||||
static async handleExternalAuth(auth: string) {
|
||||
const decodedBase64 = atob(auth);
|
||||
const jsonData = JSON.parse(decodedBase64);
|
||||
|
||||
const { accessToken, expiresIn, refreshToken } = jsonData;
|
||||
|
||||
const now = new Date();
|
||||
|
||||
const tokenExpirationTimestamp =
|
||||
now.getTime() + expiresIn - this.EXPIRATION_OFFSET_IN_MS;
|
||||
|
||||
this.userAuth = {
|
||||
authToken: accessToken,
|
||||
refreshToken: refreshToken,
|
||||
expirationTimestamp: tokenExpirationTimestamp,
|
||||
};
|
||||
|
||||
await userAuthRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
accessToken,
|
||||
tokenExpirationTimestamp,
|
||||
refreshToken,
|
||||
},
|
||||
["id"]
|
||||
);
|
||||
|
||||
if (WindowManager.mainWindow) {
|
||||
WindowManager.mainWindow.webContents.send("on-signin");
|
||||
}
|
||||
}
|
||||
|
||||
static async setupApi() {
|
||||
this.instance = axios.create({
|
||||
baseURL: import.meta.env.MAIN_VITE_API_URL,
|
||||
|
@ -33,6 +66,8 @@ export class HydraApi {
|
|||
}
|
||||
|
||||
private static async revalidateAccessTokenIfExpired() {
|
||||
if (!this.userAuth.authToken) throw new Error("user is not logged in");
|
||||
|
||||
const now = new Date();
|
||||
if (this.userAuth.expirationTimestamp < now.getTime()) {
|
||||
try {
|
||||
|
@ -64,15 +99,11 @@ export class HydraApi {
|
|||
this.userAuth.authToken = "";
|
||||
this.userAuth.expirationTimestamp = 0;
|
||||
|
||||
userAuthRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
accessToken: "",
|
||||
refreshToken: "",
|
||||
tokenExpirationTimestamp: 0,
|
||||
},
|
||||
["id"]
|
||||
);
|
||||
if (WindowManager.mainWindow) {
|
||||
WindowManager.mainWindow.webContents.send("on-signout");
|
||||
}
|
||||
|
||||
userAuthRepository.delete({ id: 1 });
|
||||
}
|
||||
|
||||
throw err;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue