Hey are there any good resources around Prisma 2 a...
# orm-help
m
Hey are there any good resources around Prisma 2 and getting user based on tokens?
b
Are you using apollo-server or graphql-yoga?
I think finding resources specifically for those will be the better approach. Prisma doesn’t necessarily have anything to do with authentication
m
Copy code
t.list.field("user", {
  type: "user",
  resolve: (_: any, args: any, ctx: any) => {
    const userID = getUser(ctx.req);

    return ctx.prisma.query.user({ where: { id: userID } });
  }
});
I found this is the right start. Put this in as a query. Then pass req.request.headers.authorization to get user and use JWT.verify to get the correct user ID
r
Here's a really great "code through" by Ben Awad:

https://www.youtube.com/watch?v=25GS0MLT8JU

I found it extremely useful and it's a step by step process into getting going with JWT based auth with refresh tokens. He uses graphql and apollo server.
❤️ 1
m
Ben is a beast. I have it figured out mostly, last thing I need is to pass req inside @nexus/schema
The answer is
Copy code
export function createContext(req: any): Context {
  return { req, prisma };
}
Here is the new query.
Copy code
t.field("user", {
  type: "user",
  resolve: (_: any, args: any, ctx: any) => {
    const userID = getUser(ctx.req.req.headers);

    return [
      ctx.prisma.user.findOne({
        where: { id: userID }
      })
    ];
  }
});