mirror of
https://github.com/ivabus/gui
synced 2025-04-23 14:07:14 +03:00
prevent no device_id (#461)
* #460 device_id might be empty in tmp.dat repetitively prevent it from being empty * more fallbacks --------- Co-authored-by: neil molina <neil@neils-MacBook-Pro.local>
This commit is contained in:
parent
81982b9ac9
commit
6e6c697582
5 changed files with 105 additions and 51 deletions
|
@ -29,29 +29,57 @@ const initialized: Promise<Session> = new Promise((resolve, reject) => {
|
|||
}
|
||||
});
|
||||
|
||||
async function createInitialSessionFile(): Promise<Session> {
|
||||
async function addEmptySessionFile(): Promise<Session> {
|
||||
const locale = app.getLocale();
|
||||
await mkdirp(sessionFolder);
|
||||
const data = {
|
||||
device_id: await getDeviceId(),
|
||||
locale
|
||||
};
|
||||
await writeSessionData(data);
|
||||
log.info("new session file created");
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function createInitialSessionFile(): Promise<Session> {
|
||||
// TODO: this looks nasty, refactor this
|
||||
// the app is too dependent that this function succeeds
|
||||
let session = {
|
||||
...sessionMemory
|
||||
};
|
||||
const locale = app.getLocale();
|
||||
|
||||
try {
|
||||
const locale = app.getLocale();
|
||||
if (fs.existsSync(sessionFilePath)) {
|
||||
log.info("session file exists!");
|
||||
const sessionBuffer = await fs.readFileSync(sessionFilePath);
|
||||
session = JSON.parse(sessionBuffer.toString()) as Session;
|
||||
session.locale = locale;
|
||||
} else {
|
||||
log.info("session file does not exists!");
|
||||
await mkdirp(sessionFolder);
|
||||
const data = {
|
||||
device_id: await getDeviceId(),
|
||||
locale
|
||||
};
|
||||
await writeSessionData(data);
|
||||
const sessionData = JSON.parse(sessionBuffer.toString()) as Session;
|
||||
|
||||
if (!sessionData?.device_id) {
|
||||
throw new Error("device_id is empty!");
|
||||
} else {
|
||||
session = sessionData;
|
||||
session.locale = locale;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
|
||||
if (!session?.device_id) {
|
||||
try {
|
||||
const newSession = await addEmptySessionFile();
|
||||
if (newSession) {
|
||||
session = newSession;
|
||||
session.locale = locale;
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
sessionMemory = session;
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
|
@ -83,19 +111,22 @@ export async function readSessionData(): Promise<Session> {
|
|||
data?.user?.developer_id
|
||||
);
|
||||
if (sessionMemory?.device_id) {
|
||||
log.info("use session cache");
|
||||
log.debug("use session cache");
|
||||
return sessionMemory;
|
||||
}
|
||||
|
||||
try {
|
||||
log.info("reading session data");
|
||||
log.info("re-reading session data");
|
||||
const locale = app.getLocale();
|
||||
const sessionBuffer = await fs.readFileSync(sessionFilePath);
|
||||
const session = JSON.parse(sessionBuffer.toString()) as Session;
|
||||
if (!session?.device_id) throw new Error("device_id is empty!");
|
||||
|
||||
session.locale = locale;
|
||||
sessionMemory = session;
|
||||
log.info("read session data");
|
||||
log.info("re-read session data done");
|
||||
} catch (error) {
|
||||
sessionMemory = await createInitialSessionFile();
|
||||
log.error(error);
|
||||
}
|
||||
return sessionMemory;
|
||||
|
@ -107,6 +138,9 @@ export async function writeSessionData(data: Session) {
|
|||
...sessionMemory,
|
||||
...data
|
||||
};
|
||||
|
||||
if (!sessionMemory.device_id) throw new Error("writing without device_id is not allowed!");
|
||||
|
||||
log.info("creating:", sessionFolder);
|
||||
await mkdirp(sessionFolder);
|
||||
log.info("writing session data:", data); // rm this
|
||||
|
|
|
@ -3,6 +3,7 @@ import { getGuiPath } from "./tea-dir";
|
|||
import * as log from "electron-log";
|
||||
import semver from "semver";
|
||||
import { cliBinPath, asyncExec } from "./cli";
|
||||
import { createInitialSessionFile } from "./auth";
|
||||
|
||||
const destinationDirectory = getGuiPath();
|
||||
|
||||
|
@ -11,31 +12,40 @@ const destinationDirectory = getGuiPath();
|
|||
// Get the binary path from the current app directory
|
||||
const binaryUrl = "https://tea.xyz/$(uname)/$(uname -m)";
|
||||
|
||||
export default async function initializeTeaCli(): Promise<string> {
|
||||
let version = "";
|
||||
let binCheck = "";
|
||||
// Create the destination directory if it doesn't exist
|
||||
if (!fs.existsSync(destinationDirectory)) {
|
||||
fs.mkdirSync(destinationDirectory, { recursive: true });
|
||||
}
|
||||
|
||||
const curlCommand = `curl -L -o "${cliBinPath}" "${binaryUrl}"`;
|
||||
|
||||
if (fs.existsSync(cliBinPath)) {
|
||||
log.info("binary tea already exists at", cliBinPath);
|
||||
binCheck = await asyncExec(`cd ${destinationDirectory} && ./tea --version`);
|
||||
} else {
|
||||
try {
|
||||
await asyncExec(curlCommand);
|
||||
log.info("Binary downloaded and saved to", cliBinPath);
|
||||
await asyncExec("chmod u+x " + cliBinPath);
|
||||
log.info("Binary is now ready for use at", cliBinPath);
|
||||
binCheck = await asyncExec(`cd ${destinationDirectory} && ./tea --version`);
|
||||
} catch (error) {
|
||||
log.error("Error setting-up tea binary:", error);
|
||||
export async function initializeTeaCli(): Promise<string> {
|
||||
try {
|
||||
let version = "";
|
||||
let binCheck = "";
|
||||
// Create the destination directory if it doesn't exist
|
||||
if (!fs.existsSync(destinationDirectory)) {
|
||||
fs.mkdirSync(destinationDirectory, { recursive: true });
|
||||
}
|
||||
|
||||
const curlCommand = `curl -L -o "${cliBinPath}" "${binaryUrl}"`;
|
||||
|
||||
if (fs.existsSync(cliBinPath)) {
|
||||
log.info("binary tea already exists at", cliBinPath);
|
||||
binCheck = await asyncExec(`cd ${destinationDirectory} && ./tea --version`);
|
||||
} else {
|
||||
try {
|
||||
await asyncExec(curlCommand);
|
||||
log.info("Binary downloaded and saved to", cliBinPath);
|
||||
await asyncExec("chmod u+x " + cliBinPath);
|
||||
log.info("Binary is now ready for use at", cliBinPath);
|
||||
binCheck = await asyncExec(`cd ${destinationDirectory} && ./tea --version`);
|
||||
} catch (error) {
|
||||
log.error("Error setting-up tea binary:", error);
|
||||
}
|
||||
}
|
||||
version = binCheck.toString().split(" ")[1];
|
||||
log.info("binary tea version:", version);
|
||||
return semver.valid(version.trim()) ? version : "";
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
return "";
|
||||
}
|
||||
version = binCheck.toString().split(" ")[1];
|
||||
log.info("binary tea version:", version);
|
||||
return semver.valid(version.trim()) ? version : "";
|
||||
}
|
||||
|
||||
export default async function initialize() {
|
||||
await Promise.all([initializeTeaCli(), createInitialSessionFile()]);
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ export default function initializeHandlers() {
|
|||
try {
|
||||
log.info("getting session");
|
||||
const session = await readSessionData();
|
||||
log.info(session ? "found session data" : "no session data found");
|
||||
log.debug(session ? "found session data" : "no session data found");
|
||||
return session;
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
|
|
|
@ -4,19 +4,30 @@
|
|||
import { baseUrl } from '$libs/v1-client';
|
||||
import { shellOpenExternal, submitLogs } from '@native';
|
||||
import mouseLeaveDelay from "@tea/ui/lib/mouse-leave-delay";
|
||||
import { getSession } from '@native';
|
||||
|
||||
const { user, deviceIdStore } = authStore;
|
||||
const { user } = authStore;
|
||||
|
||||
let authenticating = false;
|
||||
|
||||
const openGithub = () => {
|
||||
const openGithub = async () => {
|
||||
authenticating = true;
|
||||
shellOpenExternal(`${baseUrl}/auth/user?device_id=${$deviceIdStore}`)
|
||||
try {
|
||||
authStore.pollSession();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
if (!authenticating) {
|
||||
try {
|
||||
const session = await getSession();
|
||||
if (session && session.device_id) {
|
||||
shellOpenExternal(`${baseUrl}/auth/user?device_id=${session.device_id}`);
|
||||
authStore.pollSession();
|
||||
} else {
|
||||
throw new Error("possible no internet connection");
|
||||
}
|
||||
} catch (error) {
|
||||
submittedMessage = (error as Error).message;
|
||||
console.error(error);
|
||||
} finally {
|
||||
authenticating = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let submittedMessage = "";
|
||||
|
|
|
@ -157,9 +157,8 @@ export async function getPackage(packageName: string): Promise<Partial<Package>>
|
|||
|
||||
export const getSession = async (): Promise<Session | null> => {
|
||||
try {
|
||||
log.info("getting local session data");
|
||||
const session = await ipcRenderer.invoke("get-session");
|
||||
log.info("local session data ", session ? "found" : "not found");
|
||||
if (!session) throw new Error("no session found");
|
||||
return session;
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
|
|
Loading…
Reference in a new issue