@@ -4,7 +4,7 @@ import { signInWithEmailAndPassword } from "firebase/auth";
4
4
import { Auth , File , UpdateFile , Upload , createAuthSchema , createFileSchema , createFileUpdateSchema } from "../interfaces/upload.interface" ;
5
5
import { validarUploadArquivo } from "../utils/validator" ;
6
6
import { z } from "zod" ;
7
- import { auth } from "../utils/constants" ;
7
+ import { auth , uploadReturnMessage } from "../utils/constants" ;
8
8
import { FirebaseError } from "firebase/app" ;
9
9
const dotenv = require ( "dotenv" ) ;
10
10
dotenv . config ( ) ;
@@ -28,13 +28,18 @@ export const uploadUserImageController = async (req: Request, res: Response, nex
28
28
email : authInfo . email ,
29
29
senha : process . env . FIREBASE_SENHA ,
30
30
} ;
31
+
32
+ if ( authFields . email != process . env . FIREBASE_EMAIL ) {
33
+ throw Error ( "Dados de autenticação inválidos!" ) ;
34
+ }
35
+
31
36
await signInWithEmailAndPassword ( auth , authFields . email , authFields . senha ! ) ;
32
37
33
38
const storageRef = ref ( storage , `users/${ upload . fileName } ` ) ;
34
39
await uploadBytesResumable ( storageRef , upload . buffer ! , upload . metadata ) ;
35
40
const image : string = await getDownloadURL ( storageRef ) ;
36
41
37
- res . status ( 201 ) . json ( { image : image } ) ;
42
+ res . status ( 201 ) . json ( { image : image , message : uploadReturnMessage . upload } ) ;
38
43
} catch ( err ) {
39
44
const errMessage : string = ( err as Error ) . message ?? "Ocorreu um erro ao tentar fazer o upload da imagem! Por favor, tente novamente mais tarde!" ;
40
45
@@ -57,18 +62,23 @@ export const uploadUserImageController = async (req: Request, res: Response, nex
57
62
58
63
export const removeUserImageController = async ( req : Request , res : Response , next : NextFunction ) => {
59
64
try {
65
+ const authInfo : Auth = createAuthSchema . parse ( req . body ) ;
60
66
const file : File = createFileSchema . parse ( req . body ) ;
61
67
62
68
const storage = getStorage ( ) ;
63
69
const storageRef = ref ( storage , `users/${ file . fileName } ` ) ;
64
70
const authFields = {
65
- email : process . env . FIREBASE_EMAIL ,
71
+ email : authInfo . email ,
66
72
senha : process . env . FIREBASE_SENHA ,
67
73
} ;
68
74
75
+ if ( authFields . email != process . env . FIREBASE_EMAIL ) {
76
+ throw Error ( "Dados de autenticação inválidos!" ) ;
77
+ }
78
+
69
79
await signInWithEmailAndPassword ( auth , authFields . email ! , authFields . senha ! ) ;
70
80
await deleteObject ( storageRef ) ;
71
- res . status ( 200 ) . json ( { message : "Imagem excluída com sucesso!" } ) ;
81
+ res . status ( 200 ) . json ( { message : uploadReturnMessage . delete } ) ;
72
82
} catch ( err ) {
73
83
const errMessage : string = ( err as Error ) . message ?? "Ocorreu um erro ao tentar remover a imagem! Por favor, tente novamente mais tarde!" ;
74
84
@@ -101,6 +111,11 @@ export const updateUserImageController = async (req: Request, res: Response, nex
101
111
email : authInfo . email ,
102
112
senha : process . env . FIREBASE_SENHA ,
103
113
} ;
114
+
115
+ if ( authFields . email != process . env . FIREBASE_EMAIL ) {
116
+ throw Error ( "Dados de autenticação inválidos!" ) ;
117
+ }
118
+
104
119
await signInWithEmailAndPassword ( auth , authFields . email ! , authFields . senha ! ) ;
105
120
await deleteObject ( storageRef ) ;
106
121
@@ -118,7 +133,7 @@ export const updateUserImageController = async (req: Request, res: Response, nex
118
133
await uploadBytesResumable ( newStorageRef , upload . buffer ! , upload . metadata ) ;
119
134
const image : string = await getDownloadURL ( newStorageRef ) ;
120
135
121
- res . status ( 200 ) . json ( { message : "Imagem atualizada com sucesso!" , image : image } ) ;
136
+ res . status ( 200 ) . json ( { message : uploadReturnMessage . update , image : image } ) ;
122
137
} catch ( err ) {
123
138
const errMessage : string = ( err as Error ) . message ?? "Ocorreu um erro ao tentar atualizar a imagem! Por favor, tente novamente mais tarde!" ;
124
139
0 commit comments