In the auth controller I have the logic for authenticating, example validating credentials, if credentials match, return a token, get the auth user (decode jwt token) etc. How I set the user context is as follows
import { getMe } from "../controllers/authController"; const server = new ApolloServer({ typeDefs, resolvers, schemaDirectives, context: async ({ req }) => { const me = await getMe(req); return { me }; } });
and the getMe logic in auth controller is as following:
export const getMe = async req => { const token = req.headers["x-access-token"]; if (token) { try { return await jwt.verify(token, JWT_SECRET); } catch (error) { throw new AuthenticationError("Session expired, please login!", error); } } };
jwt.verify() returns the payload if the jwt token is valid and not expired.
Amazing, thanks so much for clarifying that
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In the auth controller I have the logic for authenticating, example validating credentials, if credentials match, return a token, get the auth user (decode jwt token) etc.
How I set the user context is as follows
and the getMe logic in auth controller is as following:
jwt.verify() returns the payload if the jwt token is valid and not expired.
Amazing, thanks so much for clarifying that