Skip to content

Commit fbaf9de

Browse files
committed
login feature improved for existing users
1 parent d0d55fd commit fbaf9de

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/dbRelated/logindDbOps.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
1+
// local dependencies
2+
import {
3+
createConnectionToDB,
4+
closeConnectionToDB,
5+
selectDB,
6+
} from "../helpers/dbHelpers.js";
7+
import { sendResponse } from "../helpers/sendResponse.js";
8+
9+
// all the db related process variables
10+
const { DB_NAME, COLLECTION_USER_STICKER } = process.env;
11+
112
const findUserFromDb = async (userInfo) => {
2-
return true;
13+
const client = await createConnectionToDB();
14+
15+
try {
16+
// select the db, Collections are selected based on needs
17+
const db = selectDB(client, DB_NAME);
18+
19+
const options = {
20+
projection: {
21+
_id: 0,
22+
username: 1,
23+
name: 1,
24+
},
25+
};
26+
27+
// query the collection for the specific users
28+
const data = await db
29+
.collection(COLLECTION_USER_STICKER)
30+
.findOne(userInfo, options);
31+
32+
// send the user info if present in DB
33+
return data;
34+
} catch (error) {
35+
return { error: error.toString() };
36+
} finally {
37+
closeConnectionToDB(client);
38+
}
339
};
440

541
export { findUserFromDb };

src/services/loginService.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ export async function loginHandler(event) {
1616
// if payload contains a valid username
1717
const userInfo = await findUserFromDb(payload);
1818

19+
if (!userInfo || !userInfo.username) {
20+
return sendResponse(process.env.ERROR_CODE, {
21+
message: userInfo?.error || "User is not present",
22+
});
23+
}
24+
1925
// create token if a valid user found
2026
const token = createToken(payload);
2127

2228
// send the response
23-
const res = { msg: "login successful" };
29+
const res = { msg: "login successful", userInfo };
2430
return sendResponse(process.env.SUCCESS_CODE, res, token);
2531
} catch (error) {
2632
return sendResponse(process.env.ERROR_CODE, { error: error.toString() });

0 commit comments

Comments
 (0)