Invalid token on my API, no available fixes worked yet

prisma

#1

Hey, everyone. Yesterday, I stumbled upon an error I had never received before saying,

"Your token is invalid. It might have expired or you might be using a token from a different project.

I first scoured this forum, GitHub, and StackOverflow for solutions and found these posts to be relevant:

Thus, my biggest takeaways were that I should implement a secret in my prisma.yml file that I reference with ${env:PRISMA_SECRET}.

Now, the above fixes worked great on my Prisma Server (which I host on localhost:4466), and I am able to generate tokens from the command line and use the Playground associated with that server.

BUT…

I have many custom resolvers and queries that have led me to rely on a server API instantiated by graphql-yoga and graphql-nexus. The configuration in my index.ts looks roughly as follows.

...

const server = new GraphQLServer({
    schema,
    context: (req) => ({
      ...req,
      db: {
        ...prisma,
        secret: PRISMA_SECRET,
      }
    })
})

server.get('/_admin', (req, res) => {
  res.redirect(`http://localhost:${PRISMA_PORT}/_admin`)
})

server.start(({ port }) => console.log(`Server is running on http://localhost:${port}`))

As you can see, I am unpacking the prisma instance generated by my schema and serving it into an API. The funny thing is that the line secret: PRISMA_SECRET is useless since the Prisma constructor which created the prisma variable ALREADY looks as follows:

export const Prisma = makePrismaClientClass<ClientConstructor<Prisma>>({
  typeDefs,
  models,
  endpoint: `http://localhost:4466`,
  secret: `${process.env["PRISMA_SECRET"]}`
});
export const prisma = new Prisma();

Hence, my server KNOWS that there is a server secret and it is still throwing me a message when I attempt to authenticate using a token generated by prisma token.

I’ve also tried generating my token in a resolver function on my API layer (code below), but tokens generated this way ALSO do not solve the problem…

t.field('authorize', {
        type: 'SecretPayload',
        args: {},
        resolve: async (_, __, ctx) => {

            const token = jwt.sign(
                {
                    data: {
                        service: 'my-api@1.0.0',
                        roles: [ "admin" ]
                    },
                },
                PRISMA_SECRET,
                {
                    expiresIn: '1h',
                }
            )

            return {
                token,
            }
        }
    })

Long story short, I’m running out of options and need to solve the Your token is invalid. issue on my generated Prisma API layer instead of on my default Prisma server.

Any help would be greatly appreciated! :blush: