From bbed4799cd8a009710b6e4b6a19471d08c93cb7f Mon Sep 17 00:00:00 2001 From: Shubham-Lal Date: Sun, 7 Jul 2024 13:02:27 +0530 Subject: [PATCH] store avatar url --- .../src/components/modal/auth/brief-info.tsx | 7 +++++- .../src/components/modal/auth/login-tab.tsx | 7 +++++- server/controllers/auth.js | 25 +++++++++---------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/client/src/components/modal/auth/brief-info.tsx b/client/src/components/modal/auth/brief-info.tsx index ad5a98a..df581b4 100644 --- a/client/src/components/modal/auth/brief-info.tsx +++ b/client/src/components/modal/auth/brief-info.tsx @@ -115,6 +115,7 @@ const BriefInfo: React.FC = ({ registerData, setRegisterData localStorage.removeItem('username'); navigate('/'); + toast.success(response.message); } else { setIsAuthenticated(AuthStatus.Failed); @@ -216,7 +217,11 @@ const BriefInfo: React.FC = ({ registerData, setRegisterData

Accept Terms & Conditions

-

Change Email or Password? setAuthTab(AuthTab.Signup)}>Go Back

diff --git a/client/src/components/modal/auth/login-tab.tsx b/client/src/components/modal/auth/login-tab.tsx index 43fbb00..78ef12d 100644 --- a/client/src/components/modal/auth/login-tab.tsx +++ b/client/src/components/modal/auth/login-tab.tsx @@ -71,6 +71,7 @@ const LoginTab = () => { localStorage.setItem('token', response.data.token); setAuthTab(AuthTab.Closed); navigate(route); + toast.success(response.message); } else { setIsAuthenticated(AuthStatus.Failed); @@ -131,7 +132,11 @@ const LoginTab = () => { placeholder={isSubmitted && !validationState.isPasswordValid ? 'Required' : ''} /> -
diff --git a/server/controllers/auth.js b/server/controllers/auth.js index b0ec791..b4411f6 100644 --- a/server/controllers/auth.js +++ b/server/controllers/auth.js @@ -24,14 +24,10 @@ exports.handleGoogleAuth = async function (fastify, request, reply) { exports.register = async function (fastify, request, reply) { try { const { email, password, username, first_name, last_name } = request.body; - let avatar = null; - if (request.body.avatar) { - avatar = request.body.avatar; - } - else if (request.file) { - avatar = request.file; - } + let avatar = null; + if (request.body.avatar) avatar = request.body.avatar; + else if (request.file) avatar = null; const [emailExists] = await fastify.mysql.query('SELECT * FROM users WHERE email = ?', [email]); if (emailExists.length > 0) throw new ErrorHandler(400, false, 'Account already exists.'); @@ -41,15 +37,18 @@ exports.register = async function (fastify, request, reply) { const hashedPassword = await bcrypt.hash(password, 10); - const [result] = await fastify.mysql.query('INSERT INTO users (email, password, username, first_name, last_name, avatar) VALUES (?, ?, ?, ?, ?, ?)', [email, hashedPassword, username, first_name, last_name, null]); + const [result] = await fastify.mysql.query( + 'INSERT INTO users (email, password, username, first_name, last_name, avatar) VALUES (?, ?, ?, ?, ?, ?)', + [email, hashedPassword, username, first_name, last_name, avatar] + ); if (result.affectedRows > 0) { const token = jwt.sign({ userId: username }, process.env.JWT_SECRET, { expiresIn: '12h' }); return reply.code(201).send({ success: true, - message: 'Account created successfully.', + message: 'Account created successfully', data: { - user: { email, avatar: null, username, first_name, last_name }, + user: { email, avatar, username, first_name, last_name }, token } }); @@ -78,7 +77,7 @@ exports.login = async function (fastify, request, reply) { return reply.code(200).send({ success: true, - message: 'Login successful.', + message: 'Login successful', data: { user: { ...(({ password, ...rest }) => rest)(userData) @@ -103,7 +102,7 @@ exports.autoLogin = async function (fastify, request, reply) { return reply.code(200).send({ success: true, - message: 'Login successful.', + message: 'Login successful', data: { user: { ...(({ password, ...rest }) => rest)(userData) @@ -127,7 +126,7 @@ exports.checkUsername = async function (fastify, request, reply) { return reply.code(200).send({ success: true, - message: 'Username available.', + message: 'Username available', }); } catch (err) { return catchError(reply, err);