From 64101169d45324f6dfc9557f776c8e8a13ad9d24 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 9 Sep 2018 21:07:54 +0200 Subject: [PATCH] No more duplication of recors at database --- client/src/components/NavBar.js | 11 ++++++- server/schema/schema.js | 58 +++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/client/src/components/NavBar.js b/client/src/components/NavBar.js index 7ce5477a9..5b02b375d 100644 --- a/client/src/components/NavBar.js +++ b/client/src/components/NavBar.js @@ -55,7 +55,16 @@ class NavBar extends Component { if (userStored !== null && userStored !== undefined && userStored !== 'undefined') { const user = JSON.parse(userStored); - const authId = user.authId; + + /* For some reason, the result of GraphQL some times varies, that is why I had to do the following hack. */ + + let authId; + if (user.authId === undefined) { + authId = user.sub; + } else { + authId = user.authId; + } + console.log("user", user); console.log("authid", authId); this.setState({ authId: authId }); diff --git a/server/schema/schema.js b/server/schema/schema.js index 9029794df..7e06a1a6c 100644 --- a/server/schema/schema.js +++ b/server/schema/schema.js @@ -79,7 +79,7 @@ const RootQuery = new GraphQLObjectType({ type: UserType, args: {authId: {type: GraphQLString}}, resolve(parent,args) { - // Code to get data from data source. + // Code to get data from data source. if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Entering function."); } @@ -242,7 +242,12 @@ function findUserByAuthId(authId, callBackFunction) { if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> findUserByAuthId -> onUserReceived -> User not found at Database."); } if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> findUserByAuthId -> onUserReceived -> args.authId = " + authId); } - callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err }); + let customResponse = { + result: global.CUSTOM_OK_RESPONSE.result, + message: "User Not Found" + }; + + callBackFunction(customResponse, { error: customResponse.message }); return; } @@ -303,22 +308,49 @@ try { email = decodedToken.email; emailVerified = decodedToken.email_verified; - /* The authenticated user is NOT at our module database. We need to add him. */ + findUserByAuthId(authId, onUserFound); - let newUser = new User({ - alias: alias, - authId: authId, - email: email, - emailVerified: emailVerified, - roleId: "1" - }); + function onUserFound(err, user){ - if (global.INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> authenticate -> onValidated -> " + alias + " being added to the database"); } + if (global.INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> authenticate -> onValidated -> onUserFound -> Entering function."); } - newUser.save(); + if (err.result === global.DEFAULT_FAIL_RESPONSE.result) { + if (global.ERROR_LOG === true) { console.log("[ERROR] " + MODULE_NAME + " -> authenticate -> onValidated -> onUserFound -> err.message = " + err.message); } + callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err }); + return; + } - callBackFunction(global.DEFAULT_OK_RESPONSE, { authId, alias }); + if (err.result === global.DEFAULT_OK_RESPONSE.result) { + if (global.INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> authenticate -> onValidated -> onUserFound -> User already exists at database"); } + callBackFunction(global.DEFAULT_OK_RESPONSE, { authId: authId, alias: user.alias }); + return; + } + if ( + err.result === global.CUSTOM_OK_RESPONSE.result && + err.message === "User Not Found" + ) { + + if (global.INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> authenticate -> onValidated -> onUserFound -> User does not exist at database"); } + + /* The authenticated user is NOT at our module database. We need to add him. */ + + let newUser = new User({ + alias: alias, + authId: authId, + email: email, + emailVerified: emailVerified, + roleId: "1" + }); + + if (global.INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> authenticate -> onValidated -> onUserFound -> " + alias + " being added to the database"); } + + newUser.save(); + + callBackFunction(global.DEFAULT_OK_RESPONSE, { authId, alias }); + + } + } } } catch(err) {