From cbfd72b48019e79056003d8a4931b2ad1f93e360 Mon Sep 17 00:00:00 2001
From: Noel Simbolon <84700640+noelsimbolon@users.noreply.github.com>
Date: Thu, 16 Nov 2023 20:53:13 +0700
Subject: [PATCH] feat: add username availability endpoint and new allowed
 origin

---
 src/controllers/auth-controller.ts | 17 +++++++++++++++--
 src/cores/app.ts                   |  1 +
 src/routers/auth-router.ts         | 13 ++++++++++---
 src/services/auth-service.ts       | 24 +++++++++++++++++++++---
 4 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/src/controllers/auth-controller.ts b/src/controllers/auth-controller.ts
index 20dc57d..1dc437d 100644
--- a/src/controllers/auth-controller.ts
+++ b/src/controllers/auth-controller.ts
@@ -26,7 +26,7 @@ const login = async (
   try {
     const accessTokenAndFingerPrint = await AuthService.login(req.body);
     setFingerprintCookie(res, accessTokenAndFingerPrint.fingerprint);
-    generateResponse(res, StatusCodes.OK, { accessToken : accessTokenAndFingerPrint.accessToken });
+    generateResponse(res, StatusCodes.OK, {accessToken: accessTokenAndFingerPrint.accessToken});
   } catch (err) {
     next(err);
   }
@@ -44,4 +44,17 @@ const setFingerprintCookie = (
   });
 };
 
-export { signup, login };
+const isUsernameAvailable = async (
+  req: Request,
+  res: Response,
+  next: NextFunction,
+) => {
+  try {
+    const isUsernameAvailable = await AuthService.isUsernameAvailable(req.body);
+    generateResponse(res, StatusCodes.OK, isUsernameAvailable);
+  } catch (err) {
+    next(err)
+  }
+}
+
+export { signup, login, isUsernameAvailable };
diff --git a/src/cores/app.ts b/src/cores/app.ts
index bd4a5d6..2c17231 100644
--- a/src/cores/app.ts
+++ b/src/cores/app.ts
@@ -13,6 +13,7 @@ const allowedOrigin = [
   'http://localhost:3000',
   'http://localhost:3001',
   'http://localhost:5173',
+  'http://localhost:8000',
   'http://localhost:8888',
 ]
 
diff --git a/src/routers/auth-router.ts b/src/routers/auth-router.ts
index 9cfd8f0..caead52 100644
--- a/src/routers/auth-router.ts
+++ b/src/routers/auth-router.ts
@@ -1,18 +1,25 @@
 import express, { Router } from "express";
 import * as AuthController from "../controllers/auth-controller";
 import { handleStandardError } from "../middlewares/handle-standard-error";
-import multer from "multer";
 
 const authRouter: Router = express.Router();
 
 authRouter.post(
   "/api/signup",
   AuthController.signup,
-  handleStandardError);
+  handleStandardError
+);
 
 authRouter.post(
   "/api/login",
   AuthController.login,
-  handleStandardError);
+  handleStandardError
+);
+
+authRouter.post(
+  "/api/username-availability",
+  AuthController.isUsernameAvailable,
+  handleStandardError
+);
 
 export { authRouter };
diff --git a/src/services/auth-service.ts b/src/services/auth-service.ts
index 09beb79..24c9258 100644
--- a/src/services/auth-service.ts
+++ b/src/services/auth-service.ts
@@ -4,8 +4,8 @@ import prismaClient from "../cores/db";
 import { ErrorType, StandardError } from "../errors/standard-error";
 import { hashPassword, isPasswordValid } from "../utils/password";
 import { generateAccessTokenAndFingerprint } from "../utils/token";
-import {validate} from "../validation/validation";
-import {loginSchema, signupSchema} from "../validation/auth-validation";
+import { validate } from "../validation/validation";
+import { loginSchema, signupSchema } from "../validation/auth-validation";
 
 const signup = async (
   data: Prisma.UserCreateInput,
@@ -71,4 +71,22 @@ const login = async (data: { username: string; password: string }) => {
   }
 };
 
-export { signup, login };
+const isUsernameAvailable = async (data: { username: string }) => {
+  const user = await prismaClient.user.findUnique({
+    where: {
+      username: data.username,
+    }
+  })
+
+  if (user !== null) {
+    return {
+      usernameAvailable: "false",
+    }
+  } else {
+    return {
+      usernameAvailable: "true",
+    }
+  }
+}
+
+export { signup, login, isUsernameAvailable };
-- 
GitLab