Skip to content

Commit 6b52512

Browse files
committed
user name valid and email validity check
1 parent a428854 commit 6b52512

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

backend/user/src/controllers/auth/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { COOKIE_NAME } from '@/lib/cookies';
44
import {
55
loginService,
66
registerService,
7+
checkEmailValidService,
8+
checkUsernameValidService,
9+
type IEmailValidPayload,
10+
type IUsernameValidPayload,
711
type ILoginPayload,
812
type IRegisterPayload,
913
} from '@/services/auth';
@@ -69,3 +73,29 @@ export const register: IRouteHandler = async (req, res) => {
6973
user: data.user, // Return user data if needed
7074
});
7175
};
76+
77+
export const checkUsernameValid: IRouteHandler = async (req, res) => {
78+
const { username }: IUsernameValidPayload = req.body;
79+
if (!username) {
80+
return res.status(StatusCodes.UNPROCESSABLE_ENTITY).json('Malformed Request');
81+
}
82+
const { code, data, error } = await checkUsernameValidService({ username });
83+
if (error || code !== StatusCodes.OK || !data) {
84+
const sanitizedErr = error?.message ?? 'An error occurred.';
85+
return res.status(code).json(sanitizedErr);
86+
}
87+
return res.status(StatusCodes.OK).json(data);
88+
};
89+
90+
export const checkEmailValid: IRouteHandler = async (req, res) => {
91+
const { email }: IEmailValidPayload = req.body;
92+
if (!email) {
93+
return res.status(StatusCodes.UNPROCESSABLE_ENTITY).json('Malformed Request');
94+
}
95+
const { code, data, error } = await checkEmailValidService({ email });
96+
if (error || code !== StatusCodes.OK || !data) {
97+
const sanitizedErr = error?.message ?? 'An error occurred.';
98+
return res.status(code).json(sanitizedErr);
99+
}
100+
return res.status(StatusCodes.OK).json(data);
101+
};

backend/user/src/routes/auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import express from 'express';
22

3-
import { login, logout, register } from '@/controllers/auth';
3+
import { checkEmailValid, checkUsernameValid, login, logout, register } from '@/controllers/auth';
44
import { limiter } from '@/lib/ratelimit';
55

66
const router = express.Router();
77

88
router.post('/login', login);
99
router.post('/logout', logout);
1010
router.post('/register', register);
11-
11+
router.post('/username-valid', checkUsernameValid);
12+
router.post('/email-valid', checkEmailValid);
1213
router.use(limiter);
1314

1415
export default router;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './login';
22
export * from './register';
33
export * from './types';
4+
export * from './validity_check';
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { eq, sql } from 'drizzle-orm';
2+
import { StatusCodes } from 'http-status-codes';
3+
import { db, users } from '@/lib/db';
4+
import {
5+
IUsernameValidResponse,
6+
IUsernameValidPayload,
7+
IEmailValidPayload,
8+
IEmailValidResponse,
9+
} from './types';
10+
11+
export const checkUsernameValidService = async (
12+
payload: IUsernameValidPayload
13+
): Promise<IUsernameValidResponse> => {
14+
const { username } = payload;
15+
16+
try {
17+
const result = await db
18+
.select({ count: sql<number>`count(*)` })
19+
.from(users)
20+
.where(eq(users.username, username));
21+
22+
const isValid = result[0].count === 0;
23+
24+
return {
25+
code: StatusCodes.OK,
26+
data: {
27+
valid: isValid,
28+
},
29+
};
30+
} catch (error) {
31+
console.error('Error checking username availability:', error);
32+
return {
33+
code: StatusCodes.INTERNAL_SERVER_ERROR,
34+
error: {
35+
message: 'An error occurred while checking username availability',
36+
},
37+
};
38+
}
39+
};
40+
41+
export const checkEmailValidService = async (
42+
payload: IEmailValidPayload
43+
): Promise<IEmailValidResponse> => {
44+
const { email } = payload;
45+
46+
try {
47+
const result = await db
48+
.select({ count: sql<number>`count(*)` })
49+
.from(users)
50+
.where(eq(users.email, email));
51+
52+
const isValid = result[0].count === 0;
53+
54+
return {
55+
code: StatusCodes.OK,
56+
data: {
57+
valid: isValid,
58+
},
59+
};
60+
} catch (error) {
61+
console.error('Error checking email availability:', error);
62+
return {
63+
code: StatusCodes.INTERNAL_SERVER_ERROR,
64+
error: {
65+
message: 'An error occurred while checking email availability',
66+
},
67+
};
68+
}
69+
};

0 commit comments

Comments
 (0)