Files
web/server/scripts/create-superuser.ts
T
xuyueandClaude Opus 5.5 26827e31d5
Deploy / deploy (build, debian, 22) (push) Canceled after 0s
feat: 登录改用 JWT(access token + refresh token),不再用 cookie
- 登录返回 15 分钟的 access token(HS256,密钥 JWT_SECRET,未配置则每次启动随机)
  和 14 天的 refresh token;请求带 Authorization: Bearer,WebSocket 用 ?token=
- refresh token 只存 sha256,放在新表 auth_refresh_token(删除旧的 auth_session);
  POST /account/refresh 换新的 access token,退出、改密码、封号时作废
- 带了无效或过期的 token 一律 401,前端据此刷新:快过期时提前刷新、并发请求只刷新一次,
  401 时刷新后重试一次,refresh token 也失效才退出登录
- 不再用 cookie,去掉 CORS credentials 和写请求的 Origin 校验

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 19:10:55 +08:00

34 lines
1.3 KiB
TypeScript

/**
* 新建超级管理员,或把已有用户设为超级管理员并重设密码。
*
* bun scripts/create-superuser.ts <用户名> <密码>
*/
import { get, run, now } from "../src/db"
import { makePassword } from "../src/auth"
const [username, password] = process.argv.slice(2)
if (!username || !password) {
console.error("用法:bun scripts/create-superuser.ts <用户名> <密码>")
process.exit(1)
}
const hash = await makePassword(password)
const user = get<{ id: number }>(`SELECT id FROM account_user WHERE username = $username`, { username })
if (user) {
run(
`UPDATE account_user SET password = $hash, raw_password = $raw, role = 'super',
is_superuser = 1, is_staff = 1, is_active = 1 WHERE id = $id`,
{ hash, raw: password.slice(0, 20), id: user.id },
)
run(`DELETE FROM auth_refresh_token WHERE user_id = $id`, { id: user.id })
console.log(`已将 ${username} 设为超级管理员并重设密码`)
} else {
run(
`INSERT INTO account_user
(password, last_login, is_superuser, username, email, is_staff, is_active, date_joined, role, raw_password, classname)
VALUES ($hash, NULL, 1, $username, '', 1, 1, $now, 'super', $raw, '')`,
{ hash, username, now: now(), raw: password.slice(0, 20) },
)
console.log(`已创建超级管理员 ${username}`)
}