'init'
This commit is contained in:
91
lib/sdk/pocketbase/auth.ts
Normal file
91
lib/sdk/pocketbase/auth.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* PocketBase 认证 - 登录、注册、登出(客户端可用)
|
||||
*/
|
||||
|
||||
import { getPocketBaseConfig } from "../config";
|
||||
import { PB_STORAGE_KEYS } from "./constants";
|
||||
|
||||
export interface AuthUser {
|
||||
id: string;
|
||||
email: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface AuthResult {
|
||||
token: string;
|
||||
record: AuthUser;
|
||||
}
|
||||
|
||||
function getPBUrl(): string {
|
||||
return getPocketBaseConfig().url;
|
||||
}
|
||||
|
||||
/** 登录 */
|
||||
export async function pbLogin(identity: string, password: string): Promise<AuthResult> {
|
||||
const url = getPBUrl();
|
||||
const res = await fetch(`${url}/api/collections/users/auth-with-password`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ identity, password }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json();
|
||||
throw new Error(err?.message || "登录失败");
|
||||
}
|
||||
const data = await res.json();
|
||||
if (!data?.token) throw new Error("登录响应异常");
|
||||
return { token: data.token, record: data.record };
|
||||
}
|
||||
|
||||
/** 注册 */
|
||||
export async function pbRegister(
|
||||
email: string,
|
||||
password: string,
|
||||
passwordConfirm: string
|
||||
): Promise<AuthResult> {
|
||||
const url = getPBUrl();
|
||||
const config = getPocketBaseConfig();
|
||||
const res = await fetch(`${url}/api/collections/${config.usersCollection}/records`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ email, password, passwordConfirm }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json();
|
||||
throw new Error(err?.message || "注册失败");
|
||||
}
|
||||
return pbLogin(email, password);
|
||||
}
|
||||
|
||||
/** 保存认证到 localStorage */
|
||||
export function pbSaveAuth(token: string, record: AuthUser): void {
|
||||
if (typeof window === "undefined") return;
|
||||
localStorage.setItem(PB_STORAGE_KEYS.token, token);
|
||||
localStorage.setItem(PB_STORAGE_KEYS.user, JSON.stringify(record));
|
||||
}
|
||||
|
||||
/** 登出 */
|
||||
export function pbLogout(): void {
|
||||
if (typeof window === "undefined") return;
|
||||
localStorage.removeItem(PB_STORAGE_KEYS.token);
|
||||
localStorage.removeItem(PB_STORAGE_KEYS.user);
|
||||
}
|
||||
|
||||
/** 获取存储的用户邮箱 */
|
||||
export function getStoredUserEmail(): string | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
try {
|
||||
const raw = localStorage.getItem(PB_STORAGE_KEYS.user);
|
||||
if (!raw) return null;
|
||||
const user = JSON.parse(raw);
|
||||
return user?.email ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取存储的 token */
|
||||
export function getStoredToken(): string | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
return localStorage.getItem(PB_STORAGE_KEYS.token);
|
||||
}
|
||||
5
lib/sdk/pocketbase/constants.ts
Normal file
5
lib/sdk/pocketbase/constants.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
/** localStorage 存储 key */
|
||||
export const PB_STORAGE_KEYS = {
|
||||
token: "pb_token",
|
||||
user: "pb_user",
|
||||
} as const;
|
||||
12
lib/sdk/pocketbase/index.ts
Normal file
12
lib/sdk/pocketbase/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* PocketBase SDK - 认证与数据操作,随时可调用
|
||||
* 复制自 digital 项目
|
||||
*/
|
||||
|
||||
import { getPocketBaseConfig } from "../config";
|
||||
import { PB_STORAGE_KEYS } from "./constants";
|
||||
|
||||
export { PB_STORAGE_KEYS } from "./constants";
|
||||
export type { AuthUser, AuthResult } from "./auth";
|
||||
export { pbLogin, pbRegister, pbSaveAuth, pbLogout, getStoredUserEmail, getStoredToken } from "./auth";
|
||||
export { createRecord, getAdminToken } from "./records";
|
||||
52
lib/sdk/pocketbase/records.ts
Normal file
52
lib/sdk/pocketbase/records.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* PocketBase 记录操作 - 创建、查询(服务端可用)
|
||||
*/
|
||||
|
||||
import { getPocketBaseConfig } from "../config";
|
||||
|
||||
/** 获取 Admin Token(需配置 POCKETBASE_EMAIL + POCKETBASE_PASSWORD) */
|
||||
export async function getAdminToken(): Promise<string | null> {
|
||||
const email = process.env.POCKETBASE_EMAIL;
|
||||
const password = process.env.POCKETBASE_PASSWORD;
|
||||
if (!email || !password) return null;
|
||||
|
||||
const pb = getPocketBaseConfig();
|
||||
const res = await fetch(`${pb.url}/api/admins/auth-with-password`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ identity: email, password }),
|
||||
});
|
||||
if (!res.ok) return null;
|
||||
const data = await res.json();
|
||||
return data?.token ?? null;
|
||||
}
|
||||
|
||||
/** 创建记录 */
|
||||
export async function createRecord(
|
||||
collection: string,
|
||||
data: Record<string, unknown>,
|
||||
authToken?: string
|
||||
): Promise<{ id: string; [key: string]: unknown }> {
|
||||
const pb = getPocketBaseConfig();
|
||||
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
||||
if (authToken) headers["Authorization"] = authToken;
|
||||
|
||||
const res = await fetch(`${pb.url}/api/collections/${collection}/records`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errText = await res.text();
|
||||
let errMsg = "创建失败";
|
||||
try {
|
||||
const errJson = JSON.parse(errText);
|
||||
if (errJson?.message) errMsg = errJson.message;
|
||||
} catch {
|
||||
if (errText.length < 200) errMsg = errText;
|
||||
}
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
Reference in New Issue
Block a user