feat: major code refactor, add login and register endpoints with swagger
All checks were successful
Update changelog / changelog (push) Successful in 27s
All checks were successful
Update changelog / changelog (push) Successful in 27s
This commit is contained in:
82
src/schemas/authSchema.ts
Normal file
82
src/schemas/authSchema.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import z from 'zod';
|
||||
|
||||
// Maybe load this from .env? 24 hours in seconds.
|
||||
export const DEFAULT_TOKEN_LIFETIME = 86_400;
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* components:
|
||||
* schemas:
|
||||
* LoginRequestDTO:
|
||||
* type: object
|
||||
* required:
|
||||
* - name
|
||||
* - password
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* default: mail@example.com or username
|
||||
* password:
|
||||
* type: string
|
||||
* default: sha512(Hunter2)
|
||||
* ttl:
|
||||
* type: number
|
||||
* default: 86400 # 24 hours * 60 minutes * 60 seconds
|
||||
*/
|
||||
const loginRequestSchemaBody = z.object({
|
||||
name: z.string({
|
||||
error: (e) => e.input === undefined ? 'Name is required' : 'Name must be a string'
|
||||
})
|
||||
.min( 3, 'Name is too short (try something longer than 3 characters)')
|
||||
.max(64, 'Name is too long (try something shorter than 64 characters)'),
|
||||
password: z.hash('sha512', {
|
||||
error: (e) => e.input === undefined ? 'Password is required' : 'Password must be a SHA512 hash'
|
||||
}),
|
||||
ttl: z.number('TTL must be a number between 120 and 2 592 000 seconds')
|
||||
.min( 120, 'TTL is too short (try something longer than 120 seconds)') // 120s
|
||||
.max(2_592_000, 'TTL is too long (try something shorter than 30 days)') // 30d
|
||||
.optional()
|
||||
});
|
||||
// export type LoginRequestBodyDTO = z.TypeOf<typeof loginRequestSchemaBody>;
|
||||
|
||||
export const loginRequestSchema = z.object({
|
||||
body: loginRequestSchemaBody
|
||||
});
|
||||
export type LoginRequestDTO = z.TypeOf<typeof loginRequestSchema>;
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* components:
|
||||
* schemas:
|
||||
* UserInfoDTO:
|
||||
* type: object
|
||||
* required:
|
||||
* - status
|
||||
* - name
|
||||
* - role
|
||||
* - token
|
||||
* properties:
|
||||
* status:
|
||||
* type: string
|
||||
* default: ok on success otherwise ErrorDTO with error
|
||||
* name:
|
||||
* type: string
|
||||
* default: username
|
||||
* role:
|
||||
* type: number
|
||||
* default: 0 # 0 - standard user, 1 - administrator
|
||||
* token:
|
||||
* type: string
|
||||
* default: JWT
|
||||
* ttl:
|
||||
* type: number
|
||||
* default: 86400 # 24 hours * 60 minutes * 60 seconds
|
||||
*/
|
||||
export type UserInfoDTO = {
|
||||
status: 'ok';
|
||||
name: string;
|
||||
role: number;
|
||||
token: string;
|
||||
ttl: number | null;
|
||||
};
|
||||
|
||||
26
src/schemas/miscSchema.ts
Normal file
26
src/schemas/miscSchema.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* components:
|
||||
* schemas:
|
||||
* ErrorDTO:
|
||||
* type: object
|
||||
* required:
|
||||
* - status
|
||||
* - error
|
||||
* properties:
|
||||
* status:
|
||||
* type: string
|
||||
* default: error
|
||||
* error:
|
||||
* type: string
|
||||
* default: error message
|
||||
* code:
|
||||
* type: string
|
||||
* default: error code (may not be returned for every request)
|
||||
*/
|
||||
export type ErrorDTO = {
|
||||
status: 'error';
|
||||
error: string;
|
||||
code?: string | undefined;
|
||||
};
|
||||
Reference in New Issue
Block a user