File tree Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Expand file tree Collapse file tree 2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 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+
112const 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
541export { findUserFromDb } ;
Original file line number Diff line number Diff 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 ( ) } ) ;
You can’t perform that action at this time.
0 commit comments