Skip to content

Commit

Permalink
server: add check username API, catch error
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham-Lal committed May 14, 2024
1 parent fe2826b commit a50fb47
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 39 deletions.
60 changes: 23 additions & 37 deletions server/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const ErrorHandler = require('../utils/ErrorHandler');
const { ErrorHandler, catchError } = require('../utils/ErrorHandler');

exports.handleGoogleAuth = async function (fastify, request, reply) {
try {
Expand Down Expand Up @@ -55,18 +55,7 @@ exports.register = async function (fastify, request, reply) {
});
} else throw new ErrorHandler(500, false, 'Failed to create user account.');
} catch (err) {
if (err instanceof ErrorHandler) {
return reply.code(err.statusCode).send({
success: err.success,
message: err.message
});
} else {
console.error(err);
return reply.code(500).send({
success: false,
message: 'Internal Server Error'
});
}
return catchError(reply, err);
}
}

Expand Down Expand Up @@ -98,18 +87,7 @@ exports.login = async function (fastify, request, reply) {
}
});
} catch (err) {
if (err instanceof ErrorHandler) {
return reply.code(err.statusCode).send({
success: err.success,
message: err.message
});
} else {
console.error(err);
return reply.code(500).send({
success: false,
message: 'Internal Server Error'
});
}
return catchError(reply, err);
}
}

Expand All @@ -132,18 +110,26 @@ exports.autoLogin = async function (fastify, request, reply) {
}
}
});
} catch (error) {
if (err instanceof ErrorHandler) {
return reply.code(err.statusCode).send({
success: err.success,
message: err.message
});
} else {
console.error(err);
return reply.code(500).send({
success: false,
message: 'Internal Server Error'
});
} catch (err) {
return catchError(reply, err);
}
}

exports.checkUsername = async function (fastify, request, reply) {
try {
const { username } = request.body;
const query = 'SELECT * FROM users WHERE username =?';

const [user] = await fastify.mysql.query(query, [username]);
if (user.length) {
throw new ErrorHandler(400, false, 'Username already taken.');
}

return reply.code(200).send({
success: true,
message: 'Username available.',
});
} catch (err) {
return catchError(reply, err);
}
}
28 changes: 27 additions & 1 deletion server/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { handleGoogleAuth, register, login, autoLogin } = require('../controllers/auth');
const { handleGoogleAuth, register, login, autoLogin, checkUsername } = require('../controllers/auth');
const verifyToken = require('../middleware/verifyToken');

module.exports = async function (fastify, opts) {
Expand Down Expand Up @@ -28,6 +28,16 @@ module.exports = async function (fastify, opts) {
}
};

const usernameSchema = {
body: {
type: 'object',
properties: {
username: { type: 'string', minLength: 1 },
},
required: ['username']
}
};

fastify.get('/google/callback', async (request, reply) => {
return handleGoogleAuth(fastify, request, reply);
});
Expand Down Expand Up @@ -78,4 +88,20 @@ module.exports = async function (fastify, opts) {
}
}
});

fastify.post('/check-username', {
schema: usernameSchema,
attachValidation: true
}, async (request, reply) => {
if (request.validationError) {
const errors = request.validationError.validation.map(error => {
return {
field: error.params.missingProperty || error.instancePath.substring(1),
message: error.message
};
});
return reply.code(400).send({ success: false, message: 'Validation failed', errors });
}
return checkUsername(fastify, request, reply);
});
}
16 changes: 15 additions & 1 deletion server/utils/ErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,18 @@ class ErrorHandler extends Error {
}
}

module.exports = ErrorHandler;
function catchError(reply, err) {
if (err instanceof ErrorHandler) {
return reply.code(err.statusCode).send({
success: err.success,
message: err.message
});
} else {
return reply.code(500).send({
success: false,
message: 'Internal Server Error'
});
}
}

module.exports = { ErrorHandler, catchError };

0 comments on commit a50fb47

Please sign in to comment.