连接数据库并修改页面
This commit is contained in:
@@ -1,54 +1,63 @@
|
||||
// /components/supadb/aksupainstance.uts
|
||||
import { createClient } from './aksupa.uts'
|
||||
|
||||
// 创建并导出 Supabase 客户端实例
|
||||
const supabaseUrl = 'https://your-project.supabase.co' // 替换为你的 Supabase URL
|
||||
const supabaseAnonKey = 'your-anon-key' // 替换为你的匿名密钥
|
||||
|
||||
export const supabase = createClient(supabaseUrl, supabaseAnonKey)
|
||||
|
||||
// 导出 Supabase 实例就绪状态
|
||||
export const isSupabaseReady = true
|
||||
|
||||
// 如果有其他需要导出的函数,可以这样导出:
|
||||
export function initializeSupabase(url: string, key: string) {
|
||||
return createClient(url, key)
|
||||
}
|
||||
|
||||
// 检查连接状态的函数
|
||||
export function checkConnection() {
|
||||
return new Promise((resolve) => {
|
||||
// 模拟连接检查
|
||||
setTimeout(() => {
|
||||
resolve(true)
|
||||
}, 500)
|
||||
})
|
||||
}
|
||||
|
||||
// 不再使用 supaready 变量,而是提供函数
|
||||
export async function ensureSupabaseReady() {
|
||||
return await checkConnection()
|
||||
}
|
||||
// components/supadb/aksupainstance.uts
|
||||
import AkSupa from './aksupa.uts'
|
||||
import { SUPA_URL, SUPA_KEY } from '@/ak/config.uts'
|
||||
import { AkReq } from '@/uni_modules/ak-req/index.uts'
|
||||
|
||||
// 创建 AkSupa 实例(在全局复用)
|
||||
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.
|
||||
/**
|
||||
* supaReady: 初始化时尝试从持久化 token 恢复会话。
|
||||
* - 若内存中已有 session 则直接返回 true
|
||||
* - 否则尝试读取 AkReq 中持久化的 refresh token 并调用 `refreshSession()` 恢复
|
||||
*/
|
||||
const supaReady: Promise<boolean> = (async () => {
|
||||
try {
|
||||
const sess = supa.getSession();
|
||||
if (sess != null && sess.session != null) {
|
||||
return true;
|
||||
const cur = supa.getSession()
|
||||
if (cur && cur.session != null && cur.session.access_token) {
|
||||
return true
|
||||
}
|
||||
// No session found — do not auto sign-in with hard-coded credentials.
|
||||
return true;
|
||||
|
||||
// 从持久化 storage 读取 token
|
||||
const access = AkReq.getToken()
|
||||
const refresh = AkReq.getRefreshToken()
|
||||
const expiresAt = AkReq.getExpiresAt() ?? 0
|
||||
|
||||
if (refresh && refresh !== '') {
|
||||
// 临时注入 session(以便 refreshSession 使用 refresh_token)
|
||||
try {
|
||||
supa.session = {
|
||||
access_token: access ?? '',
|
||||
refresh_token: refresh,
|
||||
expires_at: expiresAt,
|
||||
user: null,
|
||||
token_type: '',
|
||||
expires_in: 0,
|
||||
raw: new UTSJSONObject({})
|
||||
}
|
||||
} catch (e) {
|
||||
// 在某些环境 UTSJSONObject 构造可能不可用,忽略并继续
|
||||
try { (supa as any).session = { access_token: access ?? '', refresh_token: refresh, expires_at: expiresAt } } catch (_) {}
|
||||
}
|
||||
|
||||
const ok = await supa.refreshSession()
|
||||
if (ok) return true
|
||||
|
||||
// 刷新失败,清空内存 session
|
||||
try { supa.session = null; supa.user = null } catch (_) {}
|
||||
}
|
||||
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error('Supabase instance init failed', err)
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
})()
|
||||
|
||||
// 向后兼容:保留 ensureSupabaseReady 接口
|
||||
export async function ensureSupabaseReady() {
|
||||
return await supaReady
|
||||
}
|
||||
|
||||
export { supaReady }
|
||||
export default supa
|
||||
Reference in New Issue
Block a user