完成修改密码功能

This commit is contained in:
2026-03-12 20:20:01 +08:00
parent a768597ca2
commit 103c6fe0b6
3 changed files with 279 additions and 83 deletions

View File

@@ -658,6 +658,73 @@ export class AkSupa {
}
}
/**
* 模拟 supabase-js 的 auth 属性,提供认证相关方法
*/
get auth() : AkSupa {
return this;
}
/**
* 校验密码或登录(别名,兼容 supabase-js 命名)
*/
async signInWithPassword(credentials : UTSJSONObject) : Promise<AkSupaSignInResult> {
const email = credentials.getString('email');
const password = credentials.getString('password');
if (email == null || password == null) {
throw new Error('Email and password are required');
}
return await this.signIn(email, password);
}
/**
* 更新用户信息(如修改密码、修改元数据等)
* 对应 Supabase Auth API: PUT /auth/v1/user (部分版本或Kong配置可能只支持PUT)
*/
async updateUser(attributes : UTSJSONObject) : Promise<AkReqResponse<any>> {
const url = this.baseUrl + '/auth/v1/user';
const token = AkReq.getToken();
if (token == null || token == '') {
throw new Error('未登录,无法更新用户信息');
}
const headers = {
apikey: this.apikey,
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
} as UTSJSONObject;
// 尝试先用 PUT 方法,因为部分环境 PATCH 报 405 Method Not Allowed
const reqOptions : AkReqOptions = {
url,
method: 'PUT',
headers,
data: attributes,
contentType: 'application/json'
};
// updateUser 后Supabase 会返回更新后的用户对象
let res = await AkReq.request(reqOptions, false);
// 如果 PUT 也是 405则尝试 PATCH
if (res.status == 405) {
reqOptions.method = 'PATCH';
res = await AkReq.request(reqOptions, false);
}
if (res.status >= 200 && res.status < 300 && res.data != null) {
// 如果返回了新的 user 对象,更新本地缓存
try {
const newUser = new UTSJSONObject(res.data);
this.user = newUser;
if (this.session != null) {
this.session!!.user = newUser;
}
} catch (e) {}
}
return res;
}
// [CHANGE][2026-01-30] hydrate user from /auth/v1/user when token exists in storage
async hydrateSessionFromStorage() : Promise<boolean> {
try {
@@ -1052,7 +1119,7 @@ async delete(table : string, filter : string | null) : Promise<AkReqResponse<any
} as UTSJSONObject,
data: { refresh_token: this.session?.refresh_token } as UTSJSONObject,
contentType: 'application/json'
}, false);
}, true);
if (res.status == 200 && (res.data != null)) {
const data = res.data as UTSJSONObject;
const access_token = data.getString('access_token') ?? '';
@@ -1187,4 +1254,15 @@ export function createClient(url : string, key : string) : AkSupa {
return new AkSupa(url, key);
}
/**
* 创建一个临时 Supabase 客户端实例,用于校验密码等操作,不会污染全局 Token
*/
export function createTempClient(url : string, key : string) : AkSupa {
const supa = new AkSupa(url, key);
// 临时客户端不执行持久化逻辑,直接清空可能已加载的 session
supa.session = null;
supa.user = null;
return supa;
}
export default AkSupa;