Sistemato home e keycloak
This commit is contained in:
@@ -34,7 +34,7 @@ function parseBody(body: PostGruppoBody): { nome: string; regione?: string; ruol
|
||||
export async function postGruppo(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||||
try {
|
||||
const input = parseBody(req.body ?? {});
|
||||
const result = await createGruppo({ ...input, userId: req.auth!.userId });
|
||||
const result = await createGruppo(input);
|
||||
res.status(201).json(result);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { getProfiloUtente, aggiornaProfiloUtente, cambiaPasswordUtente } from '../services/utente.service';
|
||||
import { HttpError } from '../errors';
|
||||
|
||||
interface PutProfiloBody {
|
||||
email?: unknown;
|
||||
nome?: unknown;
|
||||
cognome?: unknown;
|
||||
}
|
||||
|
||||
function parseProfiloBody(body: PutProfiloBody): { email: string; nome: string; cognome: string } {
|
||||
if (typeof body.email !== 'string' || body.email.trim().length === 0) {
|
||||
throw new HttpError(400, "Il campo 'email' è obbligatorio ed è una stringa non vuota");
|
||||
}
|
||||
if (typeof body.nome !== 'string' || body.nome.trim().length === 0) {
|
||||
throw new HttpError(400, "Il campo 'nome' è obbligatorio ed è una stringa non vuota");
|
||||
}
|
||||
if (typeof body.cognome !== 'string' || body.cognome.trim().length === 0) {
|
||||
throw new HttpError(400, "Il campo 'cognome' è obbligatorio ed è una stringa non vuota");
|
||||
}
|
||||
return { email: body.email.trim(), nome: body.nome.trim(), cognome: body.cognome.trim() };
|
||||
}
|
||||
|
||||
export async function getMe(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||||
try {
|
||||
const profilo = await getProfiloUtente(req.auth!.userId);
|
||||
res.json(profilo);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
|
||||
export async function putMe(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||||
try {
|
||||
const input = parseProfiloBody(req.body ?? {});
|
||||
await aggiornaProfiloUtente(req.auth!.userId, input);
|
||||
res.status(200).json(input);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
|
||||
export async function putMePassword(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||||
try {
|
||||
const { password } = req.body ?? {};
|
||||
if (typeof password !== 'string' || password.length < 8) {
|
||||
throw new HttpError(400, "Il campo 'password' è obbligatorio e deve avere almeno 8 caratteri");
|
||||
}
|
||||
|
||||
await cambiaPasswordUtente(req.auth!.userId, password);
|
||||
res.status(204).send();
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user