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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 02:08:12 +08:00
|
|
|
/**
|
|
|
|
|
* 认证文件路径(与配置一致):
|
|
|
|
|
* - 打包后 → 系统用户数据目录(userData)
|
|
|
|
|
* - 开发模式 → 项目根目录
|
|
|
|
|
*/
|
|
|
|
|
export function authPath(): string {
|
2026-06-28 03:37:07 +08:00
|
|
|
if (app.isPackaged) {
|
2026-08-28 02:08:12 +08:00
|
|
|
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-08-28 02:08:12 +08:00
|
|
|
}
|
2026-06-28 03:37:07 +08:00
|
|
|
|
|
|
|
|
export function readAuth(): AuthData {
|
2026-08-28 02:08:12 +08:00
|
|
|
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 {
|
2026-08-28 02:08:12 +08:00
|
|
|
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 {
|
2026-08-28 02:08:12 +08:00
|
|
|
const filePath = authPath();
|
2026-06-28 03:37:07 +08:00
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
|
|
fs.unlinkSync(filePath);
|
|
|
|
|
}
|
|
|
|
|
}
|