11import { Request , Response } from "express" ;
2- import { z } from "zod" ;
3- import { LoginSchema , RegisterSchema } from "../types/credentials.types" ;
2+ import { AppError } from "../../lib/errors" ;
43import { CredentialsService } from "./credentials.service" ;
54
65export class CredentialsController {
76 constructor ( private readonly service : CredentialsService ) { }
87
98 async register ( req : Request , res : Response ) {
10- try {
11- const input = RegisterSchema . parse ( req . body ) ;
12- const { user , session : userSession } = await this . service . register ( input ) ;
9+ const { user , session : userSession } = await this . service . register (
10+ req . body ,
11+ ) ;
1312
14- req . session . userId = userSession . userId ;
15- req . session . role = userSession . role ;
16- await req . session . save ( ) ;
13+ req . session . userId = userSession . userId ;
14+ req . session . role = userSession . role ;
15+ await req . session . save ( ) ;
1716
18- return res . status ( 201 ) . json ( { user, session : userSession } ) ;
19- } catch ( error ) {
20- if ( error instanceof z . ZodError ) {
21- return res . status ( 400 ) . json ( { error : error . format ( ) } ) ;
22- }
23- const message = error instanceof Error ? error . message : "Erro interno" ;
24- const status = message === "Email já cadastrado" ? 409 : 500 ;
25- return res . status ( status ) . json ( { error : message } ) ;
26- }
17+ return res . status ( 201 ) . json ( { user, session : userSession } ) ;
2718 }
2819
2920 async login ( req : Request , res : Response ) {
30- try {
31- const input = LoginSchema . parse ( req . body ) ;
32- const { user, session : userSession } = await this . service . login ( input ) ;
21+ const { user, session : userSession } = await this . service . login ( req . body ) ;
3322
34- req . session . userId = userSession . userId ;
35- req . session . role = userSession . role ;
36- await req . session . save ( ) ;
23+ req . session . userId = userSession . userId ;
24+ req . session . role = userSession . role ;
25+ await req . session . save ( ) ;
3726
38- return res . json ( { user, session : userSession } ) ;
39- } catch ( error ) {
40- if ( error instanceof z . ZodError ) {
41- return res . status ( 400 ) . json ( { error : error . format ( ) } ) ;
42- }
43- const message = error instanceof Error ? error . message : "Erro interno" ;
44- const status = message === "Credenciais inválidas" ? 401 : 500 ;
45- return res . status ( status ) . json ( { error : message } ) ;
46- }
27+ return res . json ( { user, session : userSession } ) ;
4728 }
4829
4930 async logout ( req : Request , res : Response ) {
@@ -53,13 +34,13 @@ export class CredentialsController {
5334
5435 async me ( req : Request , res : Response ) {
5536 if ( ! req . session . userId ) {
56- return res . status ( 401 ) . json ( { error : "Não autenticado" } ) ;
37+ throw AppError . unauthorized ( ) ;
5738 }
5839
5940 const user = await this . service . findById ( req . session . userId ) ;
6041 if ( ! user ) {
6142 await req . session . destroy ( ) ;
62- return res . status ( 401 ) . json ( { error : "Não autenticado" } ) ;
43+ throw AppError . unauthorized ( ) ;
6344 }
6445
6546 return res . json ( { user } ) ;
0 commit comments