mirror of
https://github.com/ivabus/gui
synced 2025-06-07 15:50:27 +03:00
#78 store session data locally: ~/.tea/tea.xyz/gui/tmp.dat
This commit is contained in:
parent
b0e0b90334
commit
742f5064c1
4 changed files with 76 additions and 34 deletions
|
@ -4,10 +4,10 @@
|
||||||
import type { User } from '@tea/ui/types';
|
import type { User } from '@tea/ui/types';
|
||||||
|
|
||||||
let user: User | null = null;
|
let user: User | null = null;
|
||||||
const authPage = `http://localhost:3000/v1/auth/user?device_id=${authStore.deviceId}`; // https://api.tea.xyz/v1/auth/user?device_id=device_id
|
const deviceId = authStore.deviceIdStore;
|
||||||
|
|
||||||
const openGithub = () => {
|
const openGithub = () => {
|
||||||
open(authPage);
|
open(`http://localhost:3000/v1/auth/user?device_id=${$deviceId}`);
|
||||||
try {
|
try {
|
||||||
authStore.pollSession();
|
authStore.pollSession();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -323,8 +323,7 @@ export async function getCategorizedPackages(): Promise<Category[]> {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getDeviceAuth(): Promise<any> {
|
export async function getDeviceAuth(deviceId: string): Promise<any> {
|
||||||
const deviceId = 'xyz123';
|
|
||||||
// const data = await get<any>(`/auth/device/${deviceId}`);
|
// const data = await get<any>(`/auth/device/${deviceId}`);
|
||||||
return {
|
return {
|
||||||
status: 'SUCCESS',
|
status: 'SUCCESS',
|
||||||
|
@ -354,3 +353,7 @@ export async function getPackageBottles(name: string): Promise<Bottle[]> {
|
||||||
{ name, platform: 'linux', arch: 'x86-64', version: '3.40.0' }
|
{ name, platform: 'linux', arch: 'x86-64', version: '3.40.0' }
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function registerDevice(): Promise<string> {
|
||||||
|
return 'uuid1234';
|
||||||
|
}
|
||||||
|
|
|
@ -175,9 +175,9 @@ type DeviceAuth = {
|
||||||
key: string;
|
key: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function getDeviceAuth(): Promise<DeviceAuth> {
|
export async function getDeviceAuth(deviceId: string): Promise<DeviceAuth> {
|
||||||
const deviceId = 'xyxz123';
|
|
||||||
const data = await get<DeviceAuth>(`/auth/device/${deviceId}`);
|
const data = await get<DeviceAuth>(`/auth/device/${deviceId}`);
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getPackageBottles(packageName: string): Promise<Bottle[]> {
|
export async function getPackageBottles(packageName: string): Promise<Bottle[]> {
|
||||||
|
@ -188,3 +188,8 @@ export async function getPackageBottles(packageName: string): Promise<Bottle[]>
|
||||||
console.log('got bottles', data);
|
console.log('got bottles', data);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function registerDevice(): Promise<string> {
|
||||||
|
const { deviceId } = await get<{ deviceId: string }>('/auth/registerDevice');
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
|
|
@ -1,42 +1,58 @@
|
||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import { BaseDirectory, createDir, readTextFile, writeTextFile } from '@tauri-apps/api/fs';
|
import { BaseDirectory, createDir, readTextFile, writeTextFile } from '@tauri-apps/api/fs';
|
||||||
import { join } from '@tauri-apps/api/path';
|
import { join } from '@tauri-apps/api/path';
|
||||||
import { getDeviceAuth } from '@api';
|
import { getDeviceAuth, registerDevice } from '@api';
|
||||||
import type { User } from '@tea/ui/types';
|
import type { User } from '@tea/ui/types';
|
||||||
|
|
||||||
const basePath = '.tea/tea.xyz/gui';
|
const basePath = '.tea/tea.xyz/gui';
|
||||||
interface Session {
|
interface Session {
|
||||||
key: string;
|
device_id?: string;
|
||||||
user: any;
|
key?: string;
|
||||||
|
user?: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function initAuthStore() {
|
export default function initAuthStore() {
|
||||||
const deviceId = 'abcdevf'; // ideally randomly generated on install
|
const session = writable<Session>({});
|
||||||
const session = writable<Session>();
|
|
||||||
let pollLoop = 0;
|
let pollLoop = 0;
|
||||||
initSession();
|
|
||||||
|
const deviceIdStore = writable<string>('');
|
||||||
|
let deviceId = '';
|
||||||
|
|
||||||
|
initSession().then((sess) => {
|
||||||
|
if (sess) {
|
||||||
|
session.set(sess);
|
||||||
|
deviceIdStore.set(sess.device_id!);
|
||||||
|
deviceId = sess.device_id!;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let timer: NodeJS.Timer | null;
|
let timer: NodeJS.Timer | null;
|
||||||
// TODO:
|
|
||||||
// fetch session data from local
|
async function updateSession(data: Session) {
|
||||||
// fetch session data remotely
|
const localSession = {
|
||||||
// update local session data
|
device_id: deviceId,
|
||||||
|
key: data.key,
|
||||||
|
user: data.user
|
||||||
|
};
|
||||||
|
saveLocallySessionData(localSession);
|
||||||
|
session.set(localSession);
|
||||||
|
}
|
||||||
|
|
||||||
async function pollSession() {
|
async function pollSession() {
|
||||||
if (!timer) {
|
if (!timer) {
|
||||||
timer = setInterval(async () => {
|
timer = setInterval(async () => {
|
||||||
pollLoop++;
|
pollLoop++;
|
||||||
try {
|
try {
|
||||||
const data = await getDeviceAuth();
|
const data = await getDeviceAuth(deviceId);
|
||||||
|
console.log('dd', deviceId, data);
|
||||||
if (data.status === 'SUCCESS') {
|
if (data.status === 'SUCCESS') {
|
||||||
session.set({
|
updateSession({
|
||||||
key: data.key,
|
key: data.key,
|
||||||
user: data.user
|
user: data.user
|
||||||
});
|
});
|
||||||
timer && clearInterval(timer);
|
timer && clearInterval(timer);
|
||||||
timer = null;
|
timer = null;
|
||||||
}
|
}
|
||||||
console.log(data);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
|
@ -52,6 +68,7 @@ export default function initAuthStore() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
deviceId,
|
deviceId,
|
||||||
|
deviceIdStore,
|
||||||
subscribe: (cb: (u: User) => void) => {
|
subscribe: (cb: (u: User) => void) => {
|
||||||
return session.subscribe((v) => v && cb(v.user));
|
return session.subscribe((v) => v && cb(v.user));
|
||||||
},
|
},
|
||||||
|
@ -60,36 +77,53 @@ export default function initAuthStore() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const initSession = async (): Promise<Session | void> => {
|
const initSession = async (): Promise<Session | void> => {
|
||||||
await createGuiDataFolder();
|
|
||||||
const session = await getSessionData();
|
|
||||||
console.log(session);
|
|
||||||
};
|
|
||||||
|
|
||||||
const createGuiDataFolder = async () => {
|
|
||||||
await createDir(basePath, {
|
await createDir(basePath, {
|
||||||
dir: BaseDirectory.Home,
|
dir: BaseDirectory.Home,
|
||||||
recursive: true
|
recursive: true
|
||||||
});
|
});
|
||||||
|
const session = await getLocalSessionData();
|
||||||
|
return session;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getSessionData = async (): Promise<Session | void> => {
|
const getLocalSessionData = async (): Promise<Session | void> => {
|
||||||
const sessionFilePath = await join(basePath, 'tmp.dat');
|
const sessionFilePath = await join(basePath, 'tmp.dat');
|
||||||
|
let data: Session;
|
||||||
try {
|
try {
|
||||||
const data = await readTextFile(sessionFilePath, {
|
const encryptedData = await readTextFile(sessionFilePath, {
|
||||||
dir: BaseDirectory.Home
|
dir: BaseDirectory.Home
|
||||||
});
|
});
|
||||||
// TODO: decrypt then return
|
// TODO: decrypt then return
|
||||||
console.log('data:', data);
|
data = JSON.parse(encryptedData || '{}');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
await writeTextFile(sessionFilePath, '', {
|
const deviceId = await getDeviceId();
|
||||||
dir: BaseDirectory.Home
|
data = {
|
||||||
});
|
device_id: deviceId
|
||||||
|
};
|
||||||
|
await saveLocallySessionData(data);
|
||||||
}
|
}
|
||||||
console.log(sessionFilePath);
|
|
||||||
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveSessionData = async (data: { [key: string]: string | number | Date }) => {
|
const saveLocallySessionData = async (data: Session) => {
|
||||||
const sessionFilePath = await join(basePath, 'tmp.dat');
|
const sessionFilePath = await join(basePath, 'tmp.dat');
|
||||||
// TODO: encrypt and write
|
// TODO: encrypt first
|
||||||
|
await writeTextFile(sessionFilePath, JSON.stringify(data), {
|
||||||
|
dir: BaseDirectory.Home
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDeviceId = async (): Promise<string> => {
|
||||||
|
const hasLocal = false;
|
||||||
|
// get from local data
|
||||||
|
// else get from server
|
||||||
|
// GET /v1/auth/registerDevice
|
||||||
|
let deviceId = '';
|
||||||
|
if (hasLocal) {
|
||||||
|
} else {
|
||||||
|
deviceId = await registerDevice();
|
||||||
|
}
|
||||||
|
console.log('deviceId:', deviceId);
|
||||||
|
return deviceId;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue