NQ-58 & NQ-57
This commit is contained in:
parent
cf24ab1af9
commit
ccf14fcd6b
@ -3,16 +3,22 @@
|
||||
* https://stackoverflow.com/questions/76131891/what-is-the-best-method-for-socket-io-authentication
|
||||
*
|
||||
*/
|
||||
|
||||
import {Application, Request, Response} from 'express';
|
||||
import UserService from '../services/UserService';
|
||||
import jwt from "jsonwebtoken";
|
||||
import config from "./Config";
|
||||
import {loginAccountSchema, registerAccountSchema} from "./ZodTypes";
|
||||
|
||||
async function addAuthRoutes(app: Application) {
|
||||
app.post('/login', async (req: Request, res: Response) => {
|
||||
const { username, password } = req.body;
|
||||
|
||||
try {
|
||||
loginAccountSchema.parse({ username, password });
|
||||
} catch (error: any) {
|
||||
return res.status(400).json({ message: error.errors[0].message });
|
||||
}
|
||||
|
||||
const userService = new UserService();
|
||||
const user = await userService.login(username, password);
|
||||
|
||||
@ -20,12 +26,19 @@ async function addAuthRoutes(app: Application) {
|
||||
const token = jwt.sign({ id: user.id }, config.JWT_SECRET, { expiresIn: '1h' });
|
||||
return res.status(200).json({ token });
|
||||
}
|
||||
return res.status(401).json({ message: 'Invalid credentials' });
|
||||
|
||||
return res.status(400).json({ message: 'Failed to login' });
|
||||
});
|
||||
|
||||
app.post('/register', async (req: Request, res: Response) => {
|
||||
const { username, password } = req.body;
|
||||
|
||||
try {
|
||||
registerAccountSchema.parse({ username, password });
|
||||
} catch (error: any) {
|
||||
return res.status(400).json({ message: error.errors[0].message });
|
||||
}
|
||||
|
||||
const userService = new UserService();
|
||||
const user = await userService.register(username, password);
|
||||
|
||||
@ -33,6 +46,7 @@ async function addAuthRoutes(app: Application) {
|
||||
const token = jwt.sign({ id: user.id }, config.JWT_SECRET, { expiresIn: '1h' });
|
||||
return res.status(200).json({ token });
|
||||
}
|
||||
|
||||
return res.status(400).json({ message: 'Failed to register user' });
|
||||
});
|
||||
|
||||
|
@ -1,7 +1,27 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const loginAccountSchema = z.object({
|
||||
username: z.string().min(3, {
|
||||
message: 'Username must be at least 3 characters long'
|
||||
}).max(255),
|
||||
password: z.string().min(8, {
|
||||
message: 'Password must be at least 8 characters long'
|
||||
}).max(255)
|
||||
});
|
||||
|
||||
export const registerAccountSchema = z.object({
|
||||
username: z.string().min(3, {
|
||||
message: 'Username must be at least 3 characters long'
|
||||
}).max(255),
|
||||
password: z.string().min(8, {
|
||||
message: 'Password must be at least 8 characters long'
|
||||
}).max(255)
|
||||
});
|
||||
|
||||
export const ZCharacterCreate = z.object({
|
||||
name: z.string().min(3).max(255)
|
||||
name: z.string().min(3, {
|
||||
message: 'Name must be at least 3 characters long'
|
||||
}).max(255)
|
||||
});
|
||||
|
||||
export const ZCharacterDelete = z.object({
|
||||
|
@ -1,11 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig to read more about this file */
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
},
|
||||
// include logo.txt from ./src to dist
|
||||
"outDir": "./dist",
|
||||
"target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||
"module": "commonjs", /* Specify what module code is generated. */
|
||||
@ -15,4 +10,4 @@
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
"skipLibCheck": true, /* Skip type checking of declaration files. */
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user