You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What am I trying to do?:
Well, I'm trying to do exactly what NextAuth does for Account and Session, but for a created resource via a GraphQL Nexus Mutation. I want to add the userId that creates that resource to the resource itself. Such as Property containing userId or creatorId. Maybe kind of like how in other terms Post may have an Author, upon creation.
At the moment I can login fine with email links, or the Google login which is what I'm after, trying to avoid credentials. Anyway, I have some basic mutations setup and my schema.prisma file configured to add the Account and Session to my PostgreSQL database (hosted on Heroku, with a shadow db also hosted there).
On the client, I can get the session (inc. user ID) from useSession which is great. But my aim currently is to somehow pass the current session (and subsequent findUnique() if needed) where I can attach the userId to a resource they can create.
The main question
Is there a way via NextAuth to add the current session to every resolver/mutation? I have included a comment below on the line where I reference a "wishful thinking" function which could get the user data given a getCurrentUser(userId), where userId is gained from NextAuth getSession? Is this possible/the correct way to do so?
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
@@map("account")
}
model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@map("session")
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
properties Property[]
@@map("user")
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
@@map("token")
}
model Property {
id String @id @default(cuid())
userId String? // I know this shouldn't be optional, but it should be here if I _can_ add userId to the create mutation
address_one String
address_two String?
city String
post_code String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User? @relation(fields: [userId], references: [id], onDelete: Cascade) // same here
@@map("property")
}
Property.ts - within graphql/types which are added to types: [] via Nexus makeSchema
Apologies for all the code, but I really want to use this stack in this way, I had this working with a Nest.js backend but want the simplicity of being within Next.js. Any help appreciated. - MS
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey folks, having some difficulty at the moment with an Email/Google NextAuth setup.
Stack:
TypeScript, Next.js, NextAuth, Apollo, GraphQL, Nexus
What am I trying to do?:
Well, I'm trying to do exactly what NextAuth does for
Account
andSession
, but for a created resource via a GraphQL Nexus Mutation. I want to add theuserId
that creates that resource to the resource itself. Such asProperty
containinguserId
orcreatorId
. Maybe kind of like how in other termsPost
may have anAuthor
, upon creation.At the moment I can login fine with email links, or the Google login which is what I'm after, trying to avoid credentials. Anyway, I have some basic mutations setup and my
schema.prisma
file configured to add the Account and Session to my PostgreSQL database (hosted on Heroku, with a shadow db also hosted there).On the client, I can get the session (inc. user ID) from
useSession
which is great. But my aim currently is to somehow pass the current session (and subsequentfindUnique()
if needed) where I can attach theuserId
to a resource they can create.The main question
Is there a way via NextAuth to add the current session to every resolver/mutation? I have included a comment below on the line where I reference a "wishful thinking" function which could get the user data given a
getCurrentUser(userId)
, whereuserId
is gained from NextAuthgetSession
? Is this possible/the correct way to do so?The code:
[...nextauth].ts
schema.prisma
Property.ts
- withingraphql/types
which are added totypes: []
via NexusmakeSchema
User.ts
Apologies for all the code, but I really want to use this stack in this way, I had this working with a Nest.js backend but want the simplicity of being within Next.js. Any help appreciated. - MS
Beta Was this translation helpful? Give feedback.
All reactions