11import UserModel from "../models/user.js" ;
22import bcrypt from "bcrypt" ;
3- import jwt from "jsonwebtoken" ;
4- import { SECRET_KEY , CLOUDINARY_CONFIG } from "../config.js" ;
3+ import { CLOUDINARY_CONFIG } from "../config.js" ;
54import { v2 as cloudinary } from "cloudinary" ;
6- import { handleAvatarUpload } from "../utils/avatarHandlers.js" ;
5+ import { handleFileUpload } from "../utils/upload.js" ;
6+ import { generateToken } from "../utils/token.js" ;
77
88cloudinary . config ( CLOUDINARY_CONFIG ) ;
99
@@ -34,8 +34,9 @@ const UserControllers = {
3434 } ;
3535 // Only add avatar if the user upload avatar file
3636 if ( avatar ) {
37- const response = await handleAvatarUpload ( avatar , userPayload ) ;
37+ const response = await handleFileUpload ( avatar ) ;
3838 if ( ! response . success ) throw new Error ( response . message ) ;
39+ userPayload . avatar = response . data ;
3940 }
4041
4142 // Create a new user
@@ -80,17 +81,35 @@ const UserControllers = {
8081 const user = {
8182 _id : crrUser . _id ,
8283 email : crrUser . email ,
84+ userName : crrUser . userName ,
8385 } ;
8486
85- const token = jwt . sign ( user , SECRET_KEY , { expiresIn : 60 * 60 } ) ;
87+ const accessToken = generateToken (
88+ {
89+ ...user ,
90+ typeToken : "AT" ,
91+ } ,
92+ "AT"
93+ ) ;
94+
95+ const refreshToken = generateToken (
96+ {
97+ ...user ,
98+ typeToken : "RT" ,
99+ } ,
100+ "RT"
101+ ) ;
86102
87103 res . status ( 200 ) . send ( {
88104 message : "User signs in successfully" ,
89105 success : true ,
90- data : token ,
106+ data : {
107+ accessToken,
108+ refreshToken,
109+ } ,
91110 } ) ;
92111 } catch ( error ) {
93- res . status ( 409 ) . send ( {
112+ res . status ( 400 ) . send ( {
94113 message : error . message ,
95114 success : false ,
96115 data : null ,
@@ -103,11 +122,6 @@ const UserControllers = {
103122 const { userName, email, bio } = req . body ;
104123 const avatar = req . file ;
105124
106- // If the user doesn't update anything
107- if ( ! ( userName || email || avatar || bio ) ) {
108- throw new Error ( "Please enter an updated field!" ) ;
109- }
110-
111125 // Get the crrUser
112126 const crrUser = await UserModel . findById ( user . _id ) ;
113127
@@ -134,8 +148,9 @@ const UserControllers = {
134148 // If the names are different, replace with the new file
135149 if ( newFileName !== currentFileName ) {
136150 // Proceed with upload
137- const response = await handleAvatarUpload ( avatar , updatedFields ) ;
151+ const response = await handleFileUpload ( avatar ) ;
138152 if ( ! response . success ) throw new Error ( response . message ) ;
153+ updatedFields . avatar = response . data ;
139154 // Delete the old avatar from Cloudinary
140155 if ( currentFileName ) {
141156 await cloudinary . uploader . destroy ( currentFileName ) ;
@@ -155,7 +170,7 @@ const UserControllers = {
155170 data : updatedProfile ,
156171 } ) ;
157172 } catch ( error ) {
158- res . status ( 400 ) . send ( {
173+ res . status ( 500 ) . send ( {
159174 message : error . message ,
160175 success : false ,
161176 data : null ,
0 commit comments