Files

50 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2026-06-28 03:37:07 +08:00
import * as fs from 'fs';
import * as path from 'path';
import electron from 'electron';
const { app } = electron;
export interface AuthData {
username: string;
uuid: string;
accessToken: string;
refreshToken: string;
xboxProfile: string;
}
/**
* 认证文件路径(与配置一致):
* - 打包后 → 系统用户数据目录(userData)
* - 开发模式 → 项目根目录
*/
export function authPath(): string {
2026-06-28 03:37:07 +08:00
if (app.isPackaged) {
return path.join(app.getPath('userData'), 'koring-auth.json');
2026-06-28 03:37:07 +08:00
}
return path.join(__dirname, '..', 'koring-auth.json');
}
2026-06-28 03:37:07 +08:00
export function readAuth(): AuthData {
const filePath = authPath();
2026-06-28 03:37:07 +08:00
if (!fs.existsSync(filePath)) {
return { username: '', uuid: '', accessToken: '', refreshToken: '', xboxProfile: '' };
}
try {
const raw = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(raw) as AuthData;
} catch {
return { username: '', uuid: '', accessToken: '', refreshToken: '', xboxProfile: '' };
}
}
export function writeAuth(auth: AuthData): void {
const filePath = authPath();
2026-06-28 03:37:07 +08:00
fs.writeFileSync(filePath, JSON.stringify(auth, null, 2), 'utf-8');
}
export function deleteAuth(): void {
const filePath = authPath();
2026-06-28 03:37:07 +08:00
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
}