diff --git a/src/controllers/auth-controller.ts b/src/controllers/auth-controller.ts index 20dc57d3e045a2ae6db2bd50049ce99c03e93542..1dc437d3f0cb7a72bf312dc3482ddfc36ee47349 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 bd4a5d62bfafcf79badd349487adfb70e9021d82..2c172313dcdb46c78f43f0e0148f6af5f65bcfca 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 9cfd8f07dc2eb0d7ac6239a993d46e2b7f40982d..caead52e784b2ac7fc4e11c6809e5061fd486e0a 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 09beb79a95de3660a41bcf0a425a91ffd2931ef8..24c92585b2e2b6308af56a94fa5335f0872d1494 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 };