Skip to content

Commit

Permalink
Changed user api extension
Browse files Browse the repository at this point in the history
  • Loading branch information
TIVMOF committed Apr 24, 2024
1 parent 517a5d2 commit 44957c9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 55 deletions.
55 changes: 0 additions & 55 deletions dirigible-bank-server-api/user.mjs

This file was deleted.

76 changes: 76 additions & 0 deletions dirigible-bank-server-api/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { rs } from "sdk/http";
// import { session } from "sdk/http";
import { UsersRepository } from "/dirigible-bank-server/gen/dao/users/UsersRepository.ts";

const usersRepo = new UsersRepository();

rs.service({
"login": {
"post": [
{
"serve": (_ctx, request, response) => {
const body = request.getJSON();

response.println(body);

["Username", "Password"].forEach((field) => {
if (!(field in body)) {
response.setStatus(400);
response.println("Required more parameters");
return;
}
});

const filterOptions = {
$filter: {
equals: {
Username: body.Username,
},
},
};

response.println(filterOptions);

const existingUsers = usersRepo.findAll(filterOptions);

response.println(existingUsers);

if (existingUsers.length <= 0) {
response.setStatus(404); // User not found
response.println("User not found");
return;
} else {
const user = existingUsers[0];

if (user.Password === body.Password) {
// Set session attributes
// session.setAttribute("userId", user.Id); // Save user ID in the session
// session.setAttribute("username", user.Username);

// Return session-related data in the response
// const sessionData = {
// userId: user.Id,
// sessionId: session.getId(), // You can return the session ID
// };

response.setStatus(200);
response.setContentType("application/json");
response.println(JSON.stringify(user.Id));

// response.println(JSON.stringify(sessionData)); // Include session data in the response
} else {
response.setStatus(401);
response.println("Invalid password");
return;
}
}
},

"catch": (_ctx, err, _request, response) => {
response.println("Pesho");
response.println(err);
}
},
],
}
}).execute()

0 comments on commit 44957c9

Please sign in to comment.