Merge huangzhenbao-admin into main and fix config

This commit is contained in:
comlibmb
2026-01-27 21:43:26 +08:00
65 changed files with 13313 additions and 1547 deletions

View File

@@ -646,6 +646,10 @@ export class AkSupa {
this.user = null
}
async signIn(email : string, password : string) : Promise<AkSupaSignInResult> {
// 提前检查 apikey 配置是否为占位符,避免发送无效请求导致 401
if (this.apikey == null || this.apikey.trim() === '' || this.apikey === 'your-anon-key') {
throw new Error('Supabase 配置错误:请在 ak/config.uts 中设置 SUPA_KEY当前为占位符');
}
const res = await AkReq.request({
url: this.baseUrl + '/auth/v1/token?grant_type=password',
method: 'POST',
@@ -656,13 +660,31 @@ export class AkSupa {
data: { email, password } as UTSJSONObject,
contentType: 'application/json'
}, false);
//console.log(res)
const data = new UTSJSONObject(res.data); // 修正确保data为UTSJSONObject
// 如果响应不是 2xx例如 401提取后端错误信息并抛出便于上层显示具体原因
const status = res.status ?? 0;
if (!(status >= 200 && status < 400)) {
let msg = 'user.login.login_failed';
try {
if (res.data != null) {
const obj = new UTSJSONObject(res.data);
msg = obj.getString('message') ?? obj.getString('error') ?? obj.getString('msg') ?? obj.getString('description') ?? obj.getString('error_description') ?? msg;
}
} catch (e) {
// ignore
}
throw new Error(msg);
}
// 解析成功的返回体
let data: UTSJSONObject;
try {
data = new UTSJSONObject(res.data);
} catch (e) {
data = new UTSJSONObject({});
}
const access_token = data.getString('access_token') ?? '';
const refresh_token = data.getString('refresh_token') ?? '';
const expires_at = data.getNumber('expires_at') ?? 0;
const user = data.getJSON('user');
//console.log(user, data)
AkReq.setToken(access_token, refresh_token, expires_at);
const session : AkSupaSignInResult = {
access_token: access_token,
@@ -675,7 +697,6 @@ export class AkSupa {
};
this.session = session;
this.user = user;
//console.log(this.user)
return session;
}

View File

@@ -3,14 +3,19 @@ import { SUPA_URL, SUPA_KEY } from '@/ak/config.uts'
const supa = new AkSupa(SUPA_URL, SUPA_KEY)
// Do not perform hard-coded auto sign-in during page preload (development mode may preload pages).
// Instead, mark supa as ready if an existing session is present; otherwise defer sign-in to explicit user action.
const supaReady: Promise<boolean> = (async () => {
try {
// await supa.signIn('akoo@163.com', 'Hf2152111')
await supa.signIn('am@163.com', 'kookoo')
return true
const sess = supa.getSession();
if (sess != null && sess.session != null) {
return true;
}
// No session found — do not auto sign-in with hard-coded credentials.
return true;
} catch (err) {
console.error('Supabase auto sign-in failed', err)
return false
console.error('Supabase instance init failed', err)
return false;
}
})()