diff --git a/backend-dummy/README.md b/backend-dummy/README.md index 945ead5..eb1fa59 100644 --- a/backend-dummy/README.md +++ b/backend-dummy/README.md @@ -33,3 +33,15 @@ This backend does not use a real database. However, there is a file named users. - **200 OK:** Login successful. The user's session cookie is set. - **400 Bad Request:** The request body is missing required fields (email or password). - **401 Unauthorized:** The provided email or password does not match any existing user. + +* [GET] /users/me + + **Description** + + Retrieves information about the authenticated user based on their session cookie. + + **Responses** + + - **200 OK:** Returns the authenticated user's information. + - **400 Bad request:** The user is not authenticated. + - **401 Unauthorized:** The user with that cookie is not in the database. diff --git a/backend-dummy/routes/users.js b/backend-dummy/routes/users.js index 192dcf8..c6cde3b 100644 --- a/backend-dummy/routes/users.js +++ b/backend-dummy/routes/users.js @@ -69,4 +69,21 @@ router.post("/signUp", function (req, res, next) { return res.json({ status: "success", message: "User created successfully" }); }); +router.get("/me", function (req, res, next) { + const id = req.cookies["cookie-id"]; + if (!id) { + return res + .status(400) + .json({ status: "error", message: "Invalid form submission", code: 400 }); + } + const user = users.find((user) => user.id === id); + if (!user) { + return res + .status(401) + .json({ status: "error", message: "Unauthorized", code: 401 }); + } + const { name, email } = user; + return res.json({ status: "success", name: name, email: email }); +}); + module.exports = router; diff --git a/src/networking/api-routes.ts b/src/networking/api-routes.ts index cb574de..457e774 100644 --- a/src/networking/api-routes.ts +++ b/src/networking/api-routes.ts @@ -6,6 +6,7 @@ const API_ROUTES = { EXAMPLE: "/example", LOGIN: "users/login", SIGN_UP: "users/signUp", + ME: "users/me", }; export { API_ROUTES }; diff --git a/src/networking/controllers/users.ts b/src/networking/controllers/users.ts index d4a852e..f26c39a 100644 --- a/src/networking/controllers/users.ts +++ b/src/networking/controllers/users.ts @@ -1,6 +1,10 @@ import { ApiService } from "networking/api-service"; import { API_ROUTES } from "networking/api-routes"; -import { serializeLogin, serializeSignUp } from "networking/serializers/users"; +import { + deserializeUser, + serializeLogin, + serializeSignUp, +} from "networking/serializers/users"; const login = async (email: string, password: string) => { const serializeCredentials = serializeLogin(email, password); @@ -17,4 +21,11 @@ const signUp = async (email: string, password: string, name: string) => { }); return response; }; -export { login, signUp }; + +const me = async () => { + const response = await ApiService.get(API_ROUTES.ME); + const info = deserializeUser(response); + return info; +}; + +export { login, signUp, me }; diff --git a/src/networking/serializers/users.ts b/src/networking/serializers/users.ts index b740985..72018cf 100644 --- a/src/networking/serializers/users.ts +++ b/src/networking/serializers/users.ts @@ -15,3 +15,8 @@ export const serializeLogin = ( email, password, }); + +export const deserializeUser = (response: UserRaw): UserInfo => ({ + email: response.email, + name: response.name, +}); diff --git a/src/networking/types/user.d.ts b/src/networking/types/user.d.ts index 63f5457..84774c4 100644 --- a/src/networking/types/user.d.ts +++ b/src/networking/types/user.d.ts @@ -6,3 +6,13 @@ interface loginCredentials { interface signUpCredentials extends loginCredentials { name: string; } + +interface UserRaw { + email: string; + name: string; +} + +interface UserInfo { + email: string; + name: string; +}