u
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import loginRoute from './login';
|
||||
import { OpenAPIHono } from '@hono/zod-openapi';
|
||||
import loginRoute from './login/password';
|
||||
import logoutRoute from './logout';
|
||||
import meRoute from './me';
|
||||
import phoneCode from './phone-code';
|
||||
import registerRoute from './register';
|
||||
import meRoute from './me/get';
|
||||
import registerRoute from './register/create';
|
||||
import ssoVerify from './sso-verify';
|
||||
|
||||
export default {
|
||||
loginRoute,
|
||||
logoutRoute,
|
||||
meRoute,
|
||||
phoneCode,
|
||||
registerRoute,
|
||||
ssoVerify
|
||||
}
|
||||
const app = new OpenAPIHono()
|
||||
.route('/', loginRoute)
|
||||
.route('/', logoutRoute)
|
||||
.route('/', meRoute)
|
||||
.route('/', registerRoute)
|
||||
.route('/', ssoVerify);
|
||||
|
||||
export default app;
|
||||
@@ -1,13 +0,0 @@
|
||||
import { OpenAPIHono } from '@hono/zod-openapi';
|
||||
import { AuthContext } from '@/server/types/context';
|
||||
import passwordRoute from './password';
|
||||
import smsRoute from './sms';
|
||||
|
||||
// const api = new OpenAPIHono<AuthContext>()
|
||||
// .route('/', passwordRoute)
|
||||
// .route('/', smsRoute);
|
||||
// export default api;
|
||||
export default {
|
||||
passwordRoute,
|
||||
smsRoute,
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
import { createRoute, OpenAPIHono } from '@hono/zod-openapi'
|
||||
import { setCookie } from 'hono/cookie'
|
||||
import { AuthService } from '../../../modules/auth/auth.service'
|
||||
import { UserService } from '../../../modules/users/user.service'
|
||||
import { z } from 'zod'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { ErrorSchema } from '../../../utils/errorHandler'
|
||||
import { AppDataSource } from '../../../data-source'
|
||||
import { AuthContext } from '../../../types/context'
|
||||
import { UserResponseSchema } from '../schemas'
|
||||
import debug from 'debug'
|
||||
import process from 'node:process'
|
||||
|
||||
const log = {
|
||||
auth: debug('auth')
|
||||
}
|
||||
|
||||
const userService = new UserService(AppDataSource)
|
||||
const authService = new AuthService(userService)
|
||||
|
||||
const SmsLoginSchema = z.object({
|
||||
phone: z.string().regex(/^1[3-9]\d{9}$/).openapi({
|
||||
example: '13800138000',
|
||||
description: '手机号'
|
||||
}),
|
||||
code: z.string().length(6).openapi({
|
||||
example: '123456',
|
||||
description: '6位验证码'
|
||||
})
|
||||
})
|
||||
|
||||
const TokenResponseSchema = z.object({
|
||||
token: z.string().openapi({
|
||||
example: 'jwt.token.here',
|
||||
description: 'JWT Token'
|
||||
}),
|
||||
user: UserResponseSchema
|
||||
})
|
||||
|
||||
const smsLoginRoute = createRoute({
|
||||
method: 'post',
|
||||
path: '/login/sms',
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: SmsLoginSchema
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: '登录成功',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: TokenResponseSchema
|
||||
}
|
||||
}
|
||||
},
|
||||
400: {
|
||||
description: '验证码错误或已过期',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: ErrorSchema
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const app = new OpenAPIHono<AuthContext>().openapi(smsLoginRoute, async (c) => {
|
||||
const { phone, code } = c.req.valid('json')
|
||||
|
||||
// 验证验证码
|
||||
const isValid = authService.verifyCode(phone, code)
|
||||
if (!isValid) {
|
||||
throw new HTTPException(400, { message: '验证码错误或已过期' })
|
||||
}
|
||||
|
||||
// 查找或创建用户
|
||||
let user = await userService.getUserByPhone(phone)
|
||||
if (!user) {
|
||||
user = await userService.createUser({ mobile: phone, username: phone })
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const token = authService.generateToken(user)
|
||||
|
||||
|
||||
const response = {
|
||||
token,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
mobile: user.mobile,
|
||||
status: user.status,
|
||||
createdAt: user.createdAt.toISOString(),
|
||||
updatedAt: user.updatedAt.toISOString()
|
||||
}
|
||||
}
|
||||
return c.json(response, 200)
|
||||
})
|
||||
|
||||
export default app
|
||||
@@ -1,10 +0,0 @@
|
||||
import { OpenAPIHono } from '@hono/zod-openapi';
|
||||
import { AuthContext } from '@/server/types/context';
|
||||
import meRoute from './get';
|
||||
|
||||
// const api = new OpenAPIHono<AuthContext>()
|
||||
// .route('/', meRoute)
|
||||
// export default api;
|
||||
export default {
|
||||
meRoute,
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
import { createRoute, OpenAPIHono } from '@hono/zod-openapi'
|
||||
import { AuthService } from '@/server/modules/auth/auth.service'
|
||||
import { UserService } from '@/server/modules/users/user.service'
|
||||
import { z } from 'zod'
|
||||
import { ErrorSchema } from '@/server/utils/errorHandler'
|
||||
import { AppDataSource } from '@/server/data-source'
|
||||
import { AuthContext } from '@/server/types/context'
|
||||
|
||||
const GenerateFixedCodeSchema = z.object({
|
||||
phone: z.string().regex(/^1[3-9]\d{9}$/).openapi({
|
||||
example: '13800138000',
|
||||
description: '手机号'
|
||||
})
|
||||
})
|
||||
|
||||
const FixedCodeResponseSchema = z.object({
|
||||
code: z.string().length(6).openapi({
|
||||
example: '123456',
|
||||
description: '6位固定验证码'
|
||||
})
|
||||
})
|
||||
|
||||
const userService = new UserService(AppDataSource)
|
||||
const authService = new AuthService(userService)
|
||||
|
||||
const generateFixedCodeRoute = createRoute({
|
||||
method: 'get',
|
||||
path: '/phone-code/fixed/{phone}',
|
||||
request: {
|
||||
params: GenerateFixedCodeSchema
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: '生成成功',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: FixedCodeResponseSchema
|
||||
}
|
||||
}
|
||||
},
|
||||
403: {
|
||||
description: '生产环境禁止访问',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: ErrorSchema
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const app = new OpenAPIHono<AuthContext>().openapi(generateFixedCodeRoute, async (c) => {
|
||||
const { phone } = c.req.valid('param')
|
||||
const code = authService.generateFixedCode(phone)
|
||||
return c.json({ code }, 200)
|
||||
})
|
||||
|
||||
export default app
|
||||
@@ -1,8 +0,0 @@
|
||||
|
||||
import fixed from './fixed';
|
||||
import sms from './sms';
|
||||
|
||||
export default {
|
||||
fixed,
|
||||
sms
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import { createRoute, OpenAPIHono } from '@hono/zod-openapi'
|
||||
import { AuthService } from '@/server/modules/auth/auth.service'
|
||||
import { UserService } from '@/server/modules/users/user.service'
|
||||
import { z } from 'zod'
|
||||
import { ErrorSchema } from '@/server/utils/errorHandler'
|
||||
import { AppDataSource } from '@/server/data-source'
|
||||
import { AuthContext } from '@/server/types/context'
|
||||
import { SMS } from '@/server/utils/sms.aliyun'
|
||||
|
||||
const GenerateSMSRndCodeSchema = z.object({
|
||||
phone: z.string().regex(/^1[3-9]\d{9}$/).openapi({
|
||||
example: '13800138000',
|
||||
description: '手机号'
|
||||
})
|
||||
})
|
||||
|
||||
const SMSRndCodeResponseSchema = z.object({
|
||||
success: z.boolean().openapi({
|
||||
example: true,
|
||||
description: '是否成功'
|
||||
})
|
||||
})
|
||||
|
||||
const userService = new UserService(AppDataSource)
|
||||
const authService = new AuthService(userService)
|
||||
|
||||
const generateFixedCodeRoute = createRoute({
|
||||
method: 'get',
|
||||
path: '/phone-code/sms/{phone}',
|
||||
request: {
|
||||
params: GenerateSMSRndCodeSchema
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: '发送成功',
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: SMSRndCodeResponseSchema
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const app = new OpenAPIHono<AuthContext>().openapi(generateFixedCodeRoute, async (c) => {
|
||||
const { phone } = c.req.valid('param')
|
||||
const code = await authService.generateRandCode(phone)
|
||||
// TODO: 发送短信
|
||||
const result = await SMS.sendVerificationSMS(phone, code)
|
||||
return c.json({ success: result }, 200)
|
||||
})
|
||||
|
||||
export default app
|
||||
@@ -1,10 +0,0 @@
|
||||
import { OpenAPIHono } from '@hono/zod-openapi';
|
||||
import { AuthContext } from '@/server/types/context';
|
||||
import registerRoute from './create';
|
||||
|
||||
// const api = new OpenAPIHono<AuthContext>()
|
||||
// .route('/', registerRoute)
|
||||
// export default api;
|
||||
export default {
|
||||
registerRoute,
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
export const UserResponseSchema = z.object({
|
||||
id: z.number(),
|
||||
username: z.string(),
|
||||
mobile: z.string(),
|
||||
status: z.number(),
|
||||
createdAt: z.string(),
|
||||
updatedAt: z.string()
|
||||
}).openapi('User')
|
||||
Reference in New Issue
Block a user