fix auth registration and access management ui
This commit is contained in:
Vendored
+4
@@ -9,6 +9,8 @@
|
||||
*/
|
||||
|
||||
import type * as aiSessions from "../aiSessions.js";
|
||||
import type * as auth from "../auth.js";
|
||||
import type * as users from "../users.js";
|
||||
|
||||
import type {
|
||||
ApiFromModules,
|
||||
@@ -18,6 +20,8 @@ import type {
|
||||
|
||||
declare const fullApi: ApiFromModules<{
|
||||
aiSessions: typeof aiSessions;
|
||||
auth: typeof auth;
|
||||
users: typeof users;
|
||||
}>;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
export default {
|
||||
providers: [
|
||||
{
|
||||
domain: process.env.CONVEX_SITE_URL,
|
||||
applicationID: "convex",
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Password } from "@convex-dev/auth/providers/Password";
|
||||
import { convexAuth } from "@convex-dev/auth/server";
|
||||
|
||||
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
|
||||
providers: [
|
||||
Password({
|
||||
validatePasswordRequirements: (password: string) => {
|
||||
if (password.length < 8) {
|
||||
throw new Error("密码至少需要 8 个字符");
|
||||
}
|
||||
},
|
||||
profile(params) {
|
||||
const email = typeof params.email === "string" ? params.email : String(params.email ?? "");
|
||||
const name = typeof params.name === "string" ? params.name.trim() : "";
|
||||
return { email, name };
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
@@ -1,7 +1,10 @@
|
||||
import { defineSchema, defineTable } from "convex/server";
|
||||
import { v } from "convex/values";
|
||||
import { authTables } from "@convex-dev/auth/server";
|
||||
|
||||
export default defineSchema({
|
||||
...authTables,
|
||||
|
||||
acp_runtime_runs: defineTable({
|
||||
id: v.string(),
|
||||
user_id: v.string(),
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import { query } from "./_generated/server";
|
||||
import { mutation } from "./_generated/server";
|
||||
import { v } from "convex/values";
|
||||
import { getAuthUserId } from "@convex-dev/auth/server";
|
||||
|
||||
function normalizeAccount(value: string) {
|
||||
return value.trim().toLowerCase();
|
||||
}
|
||||
|
||||
export const resolveLoginAccount = query({
|
||||
args: {
|
||||
account: v.string(),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const account = normalizeAccount(args.account);
|
||||
if (!account) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const users = await ctx.db.query("users").collect();
|
||||
const user = users.find((row: any) => {
|
||||
const email = normalizeAccount(String(row.email ?? ""));
|
||||
const name = normalizeAccount(String(row.name ?? ""));
|
||||
return email === account || name === account;
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
userId: user._id,
|
||||
email: String((user as any).email ?? ""),
|
||||
name: String((user as any).name ?? ""),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
function normalizeUsername(raw: string): string {
|
||||
const username = String(raw ?? "").trim();
|
||||
if (!username) throw new Error("用户名不能为空");
|
||||
if (username.length < 2 || username.length > 32) throw new Error("用户名长度需为 2-32 个字符");
|
||||
if (/\s/.test(username)) throw new Error("用户名不能包含空格");
|
||||
return username;
|
||||
}
|
||||
|
||||
export const currentUser = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
const userId = await getAuthUserId(ctx);
|
||||
if (userId === null) {
|
||||
return null;
|
||||
}
|
||||
return await ctx.db.get(userId);
|
||||
},
|
||||
});
|
||||
|
||||
export const setMyUsername = mutation({
|
||||
args: { username: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await getAuthUserId(ctx);
|
||||
if (userId === null) throw new Error("未登录");
|
||||
|
||||
const username = normalizeUsername(args.username);
|
||||
const existing = await ctx.db
|
||||
.query("users")
|
||||
.filter((q) => q.eq(q.field("name"), username))
|
||||
.first();
|
||||
|
||||
if (existing && String(existing._id) !== String(userId)) {
|
||||
throw new Error("用户名已被占用");
|
||||
}
|
||||
|
||||
await ctx.db.patch(userId, { name: username });
|
||||
return { ok: true, username };
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user