feat(auth): 新增微信小程序登录(绑定策略:首次用学号密码绑定)
- 新增 POST /api/auth/wechat/login:用 wx.login 的 code 换 openid;已绑定直接签发 JWT 返回,未绑定返回 bindToken - bind.post.ts:支持从请求体读取 bindToken(小程序不便维护 cookie),绑定成功后返回 token,供 Authorization: Bearer 使用 - auth-route-policy.ts:把新接口登记进免鉴权白名单,否则会被鉴权中间件拦截 - .env.example:新增 WECHAT_MINI_APPID / WECHAT_MINI_SECRET
This commit is contained in:
@@ -13,7 +13,8 @@ import { getClientIP } from '~~/server/utils/ip-utils'
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
const { username, password } = body
|
||||
const bindingToken = getCookie(event, 'binding-token')
|
||||
// 网页端走 cookie;小程序等非浏览器客户端不便维护 cookie,允许从请求体传入
|
||||
const bindingToken = getCookie(event, 'binding-token') || body.bindToken
|
||||
|
||||
if (!bindingToken) {
|
||||
throw createError({ statusCode: 400, message: '绑定会话已过期,请重新通过第三方登录发起' })
|
||||
@@ -105,5 +106,17 @@ export default defineEventHandler(async (event) => {
|
||||
// 清除绑定令牌
|
||||
deleteCookie(event, 'binding-token')
|
||||
|
||||
return { success: true }
|
||||
return {
|
||||
success: true,
|
||||
// 供小程序等非浏览器客户端使用:后续请求带 Authorization: Bearer <token>
|
||||
token,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
name: user.name,
|
||||
grade: user.grade,
|
||||
class: user.class,
|
||||
role: user.role
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user