-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support finding and promoting roles by username
- Loading branch information
1 parent
075a342
commit 086ad28
Showing
4 changed files
with
75 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,49 @@ | ||
import { createDb } from "@/lib/db" | ||
import { userRoles, users } from "@/lib/schema" | ||
import { users } from "@/lib/schema" | ||
import { eq } from "drizzle-orm" | ||
|
||
export const runtime = "edge" | ||
|
||
export async function GET(request: Request) { | ||
const url = new URL(request.url) | ||
const email = url.searchParams.get('email') | ||
export async function POST(request: Request) { | ||
try { | ||
const json = await request.json() | ||
const { searchText } = json as { searchText: string } | ||
|
||
if (!email) { | ||
return Response.json( | ||
{ error: "邮箱地址不能为空" }, | ||
{ status: 400 } | ||
) | ||
} | ||
if (!searchText) { | ||
return Response.json({ error: "请提供用户名或邮箱地址" }, { status: 400 }) | ||
} | ||
|
||
const db = createDb() | ||
|
||
const user = await db.query.users.findFirst({ | ||
where: eq(users.email, email), | ||
}) | ||
const db = createDb() | ||
|
||
if (!user) { | ||
return Response.json({ user: null }) | ||
} | ||
const user = await db.query.users.findFirst({ | ||
where: searchText.includes('@') ? eq(users.email, searchText) : eq(users.username, searchText), | ||
with: { | ||
userRoles: { | ||
with: { | ||
role: true | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const userRole = await db.query.userRoles.findFirst({ | ||
where: eq(userRoles.userId, user.id), | ||
with: { | ||
role: true | ||
if (!user) { | ||
return Response.json({ error: "未找到用户" }, { status: 404 }) | ||
} | ||
}) | ||
|
||
return Response.json({ | ||
user: { | ||
id: user.id, | ||
name: user.name, | ||
email: user.email, | ||
role: userRole?.role.name | ||
} | ||
}) | ||
return Response.json({ | ||
user: { | ||
id: user.id, | ||
name: user.name, | ||
username: user.username, | ||
email: user.email, | ||
role: user.userRoles[0]?.role.name | ||
} | ||
}) | ||
} catch (error) { | ||
console.error("Failed to find user:", error) | ||
return Response.json( | ||
{ error: "查询用户失败" }, | ||
{ status: 500 } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters