Server: AuthID passed from decoded token via context
This commit is contained in:
1 parent
0669921d0e
commit
12e055ee80
6 files changed
+29
-29
No files matched your search
+1
-1
@@ -34,7 +34,7 @@ const errorLink= onError(({ graphQLErrors, networkError }) => {
|
|||||||
graphQLErrors.map(({ message, locations, path }) =>
|
graphQLErrors.map(({ message, locations, path }) =>
|
||||||
console.log(
|
console.log(
|
||||||
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
|
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
|
||||||
),
|
)
|
||||||
);
|
);
|
||||||
if (networkError) console.log(`[Network error]: ${networkError}`);
|
if (networkError) console.log(`[Network error]: ${networkError}`);
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -52,12 +52,4 @@ class LoggedInUser extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default graphql(getUserByAuthIdQuery, { // What follows is the way to pass a parameter to a query.
|
export default graphql(getUserByAuthIdQuery)(LoggedInUser); // This binds the querty to the component
|
||||||
options: (props) => {
|
|
||||||
return {
|
|
||||||
variables: {
|
|
||||||
authId: props.authId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})(LoggedInUser); // This binds the querty to the component
|
|
||||||
@@ -53,7 +53,7 @@ class NavBar extends Component {
|
|||||||
const userStored = localStorage.getItem('user');
|
const userStored = localStorage.getItem('user');
|
||||||
|
|
||||||
if (userStored !== null && userStored !== undefined && userStored !== 'undefined') {
|
if (userStored !== null && userStored !== undefined && userStored !== 'undefined') {
|
||||||
const user = JSON.parse(userStored);
|
const user = userStored;
|
||||||
|
|
||||||
/* For some reason, the result of GraphQL some times varies, that is why I had to do the following hack. */
|
/* For some reason, the result of GraphQL some times varies, that is why I had to do the following hack. */
|
||||||
|
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ query($id: ID){
|
|||||||
`
|
`
|
||||||
|
|
||||||
const getUserByAuthIdQuery = gql`
|
const getUserByAuthIdQuery = gql`
|
||||||
query($authId: String){
|
query {
|
||||||
userByAuthId (authId: $authId){
|
userByAuthId{
|
||||||
id
|
id
|
||||||
alias
|
alias
|
||||||
firstName
|
firstName
|
||||||
|
|||||||
+12
-11
@@ -26,25 +26,26 @@ mongoose.connection.once('open', () => {
|
|||||||
|
|
||||||
/* Here we bind all requests to this endpoint to be procecced by the GraphQL Library. */
|
/* 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({
|
app.get('/graphql', graphqlHTTP({
|
||||||
schema,
|
schema,
|
||||||
graphiql: true
|
graphiql: true
|
||||||
}));
|
}));
|
||||||
|
|
||||||
app.post('/graphql', graphqlHTTP({
|
app.post('/graphql', graphqlHTTP(req => ({
|
||||||
schema,
|
schema,
|
||||||
graphiql: false,
|
graphiql: false,
|
||||||
context: req => ({ ...req })
|
context: { user: req.user }
|
||||||
}));
|
})
|
||||||
|
));
|
||||||
|
|
||||||
app.post('/graphql', checkJwt, (err, req, res, next) => {
|
app.post('/graphql', (err, req, res, done) => getUser(req, res, done));
|
||||||
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,() => {
|
app.listen(4000,() => {
|
||||||
console.log('Now listening for requests on port 4000');
|
console.log('Now listening for requests on port 4000');
|
||||||
|
|||||||
+12
-5
@@ -77,20 +77,28 @@ const RootQuery = new GraphQLObjectType({
|
|||||||
},
|
},
|
||||||
userByAuthId: {
|
userByAuthId: {
|
||||||
type: UserType,
|
type: UserType,
|
||||||
args: {authId: {type: GraphQLString}},
|
// args: {authId: {type: GraphQLString}}, // Get AuthId from context rather than send from client.
|
||||||
resolve(parent,args) {
|
resolve(parent, args, context) {
|
||||||
// Code to get data from data source.
|
// 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 -> 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. */
|
/* 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) => {
|
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 -> Entering function."); }
|
||||||
if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Promise -> args.authId = " + args.authId); }
|
if (INFO_LOG === true) { console.log("[INFO] " + MODULE_NAME + " -> RootQuery -> userByAuthId -> resolve -> Promise -> args.authId = " + authId); }
|
||||||
|
|
||||||
findUserByAuthId(args.authId, onUserFound);
|
findUserByAuthId(authId, onUserFound);
|
||||||
|
|
||||||
function onUserFound(err, responseToGraphQL) {
|
function onUserFound(err, responseToGraphQL) {
|
||||||
|
|
||||||
@@ -274,7 +282,6 @@ function findUserByAuthId(authId, callBackFunction) {
|
|||||||
|
|
||||||
if (global.ERROR_LOG === true) { console.log("[ERROR] " + MODULE_NAME + " -> findUserByAuthId -> err.message = " + err.message); }
|
if (global.ERROR_LOG === true) { console.log("[ERROR] " + MODULE_NAME + " -> findUserByAuthId -> err.message = " + err.message); }
|
||||||
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err });
|
callBackFunction(global.DEFAULT_FAIL_RESPONSE, { error: err });
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user