generated from azures04/Base-REST-API
base
This commit is contained in:
@@ -1,15 +0,0 @@
|
|||||||
const express = require("express")
|
|
||||||
const router = express.Router()
|
|
||||||
const registerService = require("../services/register")
|
|
||||||
|
|
||||||
router.post("/", async (req, res) => {
|
|
||||||
const { email, username, password } = req.body
|
|
||||||
const registerResult = registerService.register({ email, username, password })
|
|
||||||
return res.status(200).json({
|
|
||||||
code: 200,
|
|
||||||
message: "User successfully registered",
|
|
||||||
data: registerResult
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
module.exports = router
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
const express = require("express")
|
|
||||||
const DefaultError = require("../../errors/DefaultError")
|
|
||||||
const router = express.Router()
|
|
||||||
|
|
||||||
router.get("", async (req, res) => {
|
|
||||||
const bearer = req.headers.authorization
|
|
||||||
if (bearer == "Bearer token") {
|
|
||||||
return res.status(200).json({
|
|
||||||
id: req.params.id,
|
|
||||||
username: "johndoe"
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
throw new DefaultError(403, "Invalid token", "", "InvalidTokenException")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
module.exports = router
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
const z = require("zod")
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
POST: {
|
|
||||||
headers: z.object({
|
|
||||||
"content-type": z.string().regex(/application\/json/i).optional()
|
|
||||||
}),
|
|
||||||
body: z.object({
|
|
||||||
email: z.string()
|
|
||||||
.email({ message: "Invalid E-Mail format." })
|
|
||||||
.toLowerCase(),
|
|
||||||
username: z.string()
|
|
||||||
.min(3, { message: "The username must be at least 3 characters long." })
|
|
||||||
.max(16, { message: "The username must be no longer than 16 characters." }),
|
|
||||||
password: z.string()
|
|
||||||
.min(8, { message: "The password must be at least 8 characters long." })
|
|
||||||
.regex(/[A-Z]/, { message: "The password must contain a capital letter." })
|
|
||||||
.regex(/[0-9]/, { message: "The password must contain a number." }),
|
|
||||||
}),
|
|
||||||
error: {
|
|
||||||
code: 422,
|
|
||||||
message: "Invalid request data"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
const z = require("zod")
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
GET: {
|
|
||||||
headers: z.object({
|
|
||||||
authorization: z.string()
|
|
||||||
.startsWith("Bearer ", { message: "Token d'authentification manquant ou invalide." }),
|
|
||||||
"content-type": z.string().regex(/application\/json/i).optional()
|
|
||||||
}),
|
|
||||||
error: {
|
|
||||||
code: 422,
|
|
||||||
message: "Invalid request data"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
const crypto = require("node:crypto")
|
|
||||||
const DefaultError = require("../errors/DefaultError")
|
|
||||||
|
|
||||||
function register({ email, username }) {
|
|
||||||
const canRegister = true
|
|
||||||
if (canRegister === true) {
|
|
||||||
return {
|
|
||||||
id: crypto.randomUUID(),
|
|
||||||
username: username,
|
|
||||||
email: email
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new DefaultError(418, "I'm a teapot", "", "TeaPotExeception")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
register
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
POST http://localhost:3000/register HTTP/1.1
|
|
||||||
Content-Type: application/json
|
|
||||||
|
|
||||||
{
|
|
||||||
"email": "johndoe@exemple.com",
|
|
||||||
"username": "johndoe",
|
|
||||||
"password": "Password123"
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
GET http://localhost:3000/users/johndoe HTTP/1.1
|
|
||||||
authorization: Bearer token
|
|
||||||
Reference in New Issue
Block a user