I’ve built a Nestjs app with Prisma and GQL. I just changed computer (from a Mac to PC) and all of a sudden my login
mutation seems to have stopped working, literally the only one… Here is some code:
auth.resolver.ts
:
@Mutation((returns) => AuthenticatedUser)
async login(
@Context() { res }: Auth.GqlContext,
@Args('payload', { type: () => UserLoginDto }) payload: UserLoginDto
): Promise<AuthenticatedUser> {
const authenticatedUser = await this.authService.login(payload)
res.cookie('jwt', authenticatedUser.jwt, cookieConfig)
return authenticatedUser
}
auth.service.ts
:
public async login(payload: UserLoginDto): Promise<AuthenticatedUser> {
const { password, email } = payload
Logger.log({ password, email })
const user = await this.usersService.getUser(email) // returns null since the change
Logger.log(JSON.stringify(user))
const isValidPassword = await this.verifyPassword(user.password, password) // error is thrown here
if (!isValidPassword) {
throw new AuthenticationError('Password does not match')
}
const { id } = user
const jwt = await this.jwtService.signAsync(
{ id, email },
{
secret: process.env.JWT_SECRET
}
)
return {
jwt,
user
}
}
users.service.ts
:
public async getUser(email: string): Promise<User | null> {
const { user } = this.prismaService
Logger.log(email) // returns '[email protected]'
const temp = user.findUnique({ where: { email } }) // returns null here
Logger.log(`PRISMA USER: ${JSON.stringify(temp)}`) // null
return temp
}
What’s weird is that I have the same queries set up for posts
and categories
, but those work fine. Nothing to do with JWT either.
My user is in the DB too:
Full GQL mutation:
mutation {
login(payload: { email: "[email protected]", password: "123" }) {
jwt
user {
name
id
email
}
}
}
GQL error:
{
"errors": [
{
"message": "Cannot read property 'password' of null",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"login"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"TypeError: Cannot read property 'password' of null",
" at AuthService.login (H:\work\blog-api\src\auth\auth.service.ts:25:60)",
" at AuthResolver.login (H:\work\blog-api\src\auth\auth.resolver.ts:43:31)",
" at target (H:\work\blog-api\node_modules\@nestjs\core\helpers\external-context-creator.js:77:28)",
" at H:\work\blog-api\node_modules\@nestjs\core\helpers\external-proxy.js:9:24"
]
}
}
}
],
"data": null
}
Prisma schema:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Post {
id String @id @default(uuid())
title String
slug String @unique
description String
relativeImage String @map("relative_image")
rawMdx String @map("raw_mdx")
published Boolean @default(true)
views Int @default(0)
likes Int @default(0)
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
categories Category[]
}
model Category {
id String @id @default(uuid())
name String @unique
color String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
posts Post[]
}
model User {
id String @id @default(uuid())
name String @unique
email String @unique
password String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
}
I’ve also run npx prisma db pull && npx prisma generate
since switching computers…
Update
After about an hour this issue disappeared. If anyone knows why I had this issue, I’ll love to know why.
3
looks like, … error is about arguments, are they logged properly?
Logger.log( JSON.stringify(payload) )
in resolver or service?Nov 7, 2021 at 17:02
Are you sure you're connected to the same database in the new machine? All the code checks out, it's just that
findUnique
can't seem to find the right user. Could you try running a script with justlet data = await prisma.user.findUnique({where: {email: "[email protected]"}})
and see what the results are? Additionally, the second log statement inusers.service.ts
won't give what you're hoping for because you're trying to print a promise.Nov 8, 2021 at 6:52
weirdly, after getting some food, this issue just disappeared. I have absolutely no idea what it was caused by though…
Nov 8, 2021 at 16:47