Skip to content

Commit

Permalink
store avatar url
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham-Lal authored Jul 7, 2024
2 parents a473cec + bbed479 commit 071b94b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
7 changes: 6 additions & 1 deletion client/src/components/modal/auth/brief-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const BriefInfo: React.FC<RegisterDataProps> = ({ registerData, setRegisterData

localStorage.removeItem('username');
navigate('/');
toast.success(response.message);
}
else {
setIsAuthenticated(AuthStatus.Failed);
Expand Down Expand Up @@ -216,7 +217,11 @@ const BriefInfo: React.FC<RegisterDataProps> = ({ registerData, setRegisterData
</div>
<p>Accept <span>Terms & Conditions</span></p>
</div>
<button type='submit' disabled={isAuthenticated === AuthStatus.Authenticating}>
<button
type='submit'
disabled={isAuthenticated === AuthStatus.Authenticating}
style={{ cursor: `${isAuthenticated === AuthStatus.Authenticating ? 'not-allowed' : ''}` }}
>
{isAuthenticated === AuthStatus.Authenticating ? <LoadingSVG size={23} /> : 'Create my Account'}
</button>
<p>Change Email or Password? <span onClick={() => setAuthTab(AuthTab.Signup)}>Go Back</span></p>
Expand Down
7 changes: 6 additions & 1 deletion client/src/components/modal/auth/login-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -131,7 +132,11 @@ const LoginTab = () => {
placeholder={isSubmitted && !validationState.isPasswordValid ? 'Required' : ''}
/>
</div>
<button type='submit' disabled={isAuthenticated === AuthStatus.Authenticating}>
<button
type='submit'
disabled={isAuthenticated === AuthStatus.Authenticating}
style={{ cursor: `${isAuthenticated === AuthStatus.Authenticating ? 'not-allowed' : ''}` }}
>
{isAuthenticated === AuthStatus.Authenticating && isSubmitted ? <LoadingSVG size={23} /> : 'Login'}
</button>
<div className='extra-btn'>
Expand Down
25 changes: 12 additions & 13 deletions server/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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
}
});
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 071b94b

Please sign in to comment.