Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions backend-dummy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 17 additions & 0 deletions backend-dummy/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions src/networking/api-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const API_ROUTES = {
EXAMPLE: "/example",
LOGIN: "users/login",
SIGN_UP: "users/signUp",
ME: "users/me",
};

export { API_ROUTES };
15 changes: 13 additions & 2 deletions src/networking/controllers/users.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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<UserRaw>(API_ROUTES.ME);
const info = deserializeUser(response);
return info;
};

export { login, signUp, me };
5 changes: 5 additions & 0 deletions src/networking/serializers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ export const serializeLogin = (
email,
password,
});

export const deserializeUser = (response: UserRaw): UserInfo => ({
email: response.email,
name: response.name,
});
10 changes: 10 additions & 0 deletions src/networking/types/user.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ interface loginCredentials {
interface signUpCredentials extends loginCredentials {
name: string;
}

interface UserRaw {
email: string;
name: string;
}

interface UserInfo {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the difference?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to separate the types that comes from the backend to the type after deserialize the information

email: string;
name: string;
}