diff --git a/client/src/App.js b/client/src/App.js index cb2cf9407..03c3cd676 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -34,7 +34,7 @@ const errorLink= onError(({ graphQLErrors, networkError }) => { graphQLErrors.map(({ message, locations, path }) => console.log( `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`, - ) + ), ); if (networkError) console.log(`[Network error]: ${networkError}`); }) diff --git a/client/src/components/LoggedInUser.js b/client/src/components/LoggedInUser.js index fedf2724f..7b6137d6d 100644 --- a/client/src/components/LoggedInUser.js +++ b/client/src/components/LoggedInUser.js @@ -52,4 +52,12 @@ class LoggedInUser extends Component { } } -export default graphql(getUserByAuthIdQuery)(LoggedInUser); // This binds the querty to the component +export default graphql(getUserByAuthIdQuery, { // What follows is the way to pass a parameter to a query. + options: (props) => { + return { + variables: { + authId: props.authId + } + } + } +})(LoggedInUser); // This binds the querty to the component diff --git a/client/src/components/NavBar.js b/client/src/components/NavBar.js index 9ca38de84..8d210e23b 100644 --- a/client/src/components/NavBar.js +++ b/client/src/components/NavBar.js @@ -53,7 +53,7 @@ class NavBar extends Component { const userStored = localStorage.getItem('user'); if (userStored !== null && userStored !== undefined && userStored !== 'undefined') { - const user = userStored; + const user = JSON.parse(userStored); /* For some reason, the result of GraphQL some times varies, that is why I had to do the following hack. */ diff --git a/client/src/queries/queries.js b/client/src/queries/queries.js index 67a82c958..45b2c38ce 100644 --- a/client/src/queries/queries.js +++ b/client/src/queries/queries.js @@ -35,8 +35,8 @@ query($id: ID){ ` const getUserByAuthIdQuery = gql` -query { - userByAuthId{ +query($authId: String){ + userByAuthId (authId: $authId){ id alias firstName diff --git a/server/app.js b/server/app.js index 627010212..aa7b6c3e3 100644 --- a/server/app.js +++ b/server/app.js @@ -26,26 +26,25 @@ mongoose.connection.once('open', () => { /* Here we bind all requests to this endpoint to be procecced by the GraphQL Library. */ -app.post('/graphql', checkJwt, (err, req, res, next) => { - console.log('checkJwt middleware req.user: ', req.user); - if (err) return res.status(401).send({error: `[Authenticate Token Error] ${err.message}`}); - next(); - } -); - app.get('/graphql', graphqlHTTP({ schema, graphiql: true })); -app.post('/graphql', graphqlHTTP(req => ({ +app.post('/graphql', graphqlHTTP({ schema, graphiql: false, - context: { user: req.user } - }) -)); + context: req => ({ ...req }) +})); -app.post('/graphql', (err, req, res, done) => getUser(req, res, done)); +app.post('/graphql', checkJwt, (err, req, res, next) => { + console.log(req); + if (err) return res.status(401).send(`[Authenticate Token Error] ${err.message}`); + next(); + } +); + +app.post('/graphql', (req, res, done) => getUser(req, res, done)); app.listen(4000,() => { console.log('Now listening for requests on port 4000'); diff --git a/server/schema/schema.js b/server/schema/schema.js index 584e64a78..75a35857c 100644 --- a/server/schema/schema.js +++ b/server/schema/schema.js @@ -77,28 +77,20 @@ const RootQuery = new GraphQLObjectType({ }, userByAuthId: { type: UserType, - // args: {authId: {type: GraphQLString}}, // Get AuthId from context rather than send from client. - resolve(parent, args, context) { + args: {authId: {type: GraphQLString}}, + resolve(parent,args) { // Code to get data from data source. - console.log('userByAuthId: ', parent, args, context); if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Entering function."); } - if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> context.user = " + context.user); } - if (context.user === undefined || context.user.sub === undefined || context.user.sub === null) { - if (global.ERROR_LOG === true) { console.log("[ERROR] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> noContextUser -> err.message = Not Logged In"); } - return null; - } - - const authId = context.user.sub; /* In order to be able to wait for asyc calls to the database, we need to return a promise to GraphQL. */ const promiseToGraphQL = new Promise((resolve, reject) => { if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Promise -> Entering function."); } - if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Promise -> args.authId = " + authId); } + if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Promise -> args.authId = " + args.authId); } - findUserByAuthId(authId, onUserFound); + findUserByAuthId(args.authId, onUserFound); function onUserFound(err, responseToGraphQL) { @@ -282,6 +274,7 @@ function findUserByAuthId(authId, callBackFunction) { if (global.ERROR_LOG === true) { console.log("[ERROR] " + MODULE_NAME + " -> findUserByAuthId -> err.message = " + err.message); } callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err }); + } }