Get session data inside a mutation

Hello. So im trying to create a profile inside a user that is already logged in. So the flow is Create User => Login => Create User Profile Inside the app. My Models:
model User {
id String @id @default(cuid())
email String @unique
username String @unique
password String
emailVerified DateTime?
profile Profile?
accounts Account[]
sessions Session[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
deletedAt DateTime @default(now())
role Role @default(USER)
}

model Profile {
id String @id @default(cuid())
bio String?
profession String?
image String?
user User @relation(fields: [userId], references: [id])
userId String @unique
first_name String?
last_name String?
gender Gender
}
model User {
id String @id @default(cuid())
email String @unique
username String @unique
password String
emailVerified DateTime?
profile Profile?
accounts Account[]
sessions Session[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
deletedAt DateTime @default(now())
role Role @default(USER)
}

model Profile {
id String @id @default(cuid())
bio String?
profession String?
image String?
user User @relation(fields: [userId], references: [id])
userId String @unique
first_name String?
last_name String?
gender Gender
}
createProfileSchema: publicProcedure
.input(createProfileSchema)
.mutation(async ({ ctx, input }) => {
try {
const session = await getSession({ ctx.session });
const { bio, profession, image, first_name, last_name, gender } = input;
return await ctx.prisma.profile.create({
data: {
bio,
profession,
image,
first_name,
last_name,
gender,
user: { connect: { email: session.data.user.email } },
},
});
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
cause: error,
});
}
}),
createProfileSchema: publicProcedure
.input(createProfileSchema)
.mutation(async ({ ctx, input }) => {
try {
const session = await getSession({ ctx.session });
const { bio, profession, image, first_name, last_name, gender } = input;
return await ctx.prisma.profile.create({
data: {
bio,
profession,
image,
first_name,
last_name,
gender,
user: { connect: { email: session.data.user.email } },
},
});
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
cause: error,
});
}
}),
Im failing to understand how do I get the session that is logged .. or do I even need it ?
8 Replies
Neto
Neto2y ago
If you need the user session Use a protected procedure And on the ctx you will have the user session
Knox
KnoxOP2y ago
Oh i completly forgot i can use that
createProfileSchema: protectedProcedure
.input(createProfileSchema)
.mutation(async ({ ctx, input }) => {
try {
const { bio, profession, image, first_name, last_name, gender } = input;
return await ctx.prisma.profile.create({
data: {
bio,
profession,
image,
first_name,
last_name,
gender,
user: { connect: { email: ctx.session.user?.email } },
},
});
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
cause: error,
});
}
}),
createProfileSchema: protectedProcedure
.input(createProfileSchema)
.mutation(async ({ ctx, input }) => {
try {
const { bio, profession, image, first_name, last_name, gender } = input;
return await ctx.prisma.profile.create({
data: {
bio,
profession,
image,
first_name,
last_name,
gender,
user: { connect: { email: ctx.session.user?.email } },
},
});
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
cause: error,
});
}
}),
Much better. Thank you! Now looking at my models, i might want to rethink whats in user and whats in profile too! Anyway, Thank you!
.hatulapro
.hatulapro2y ago
It probably doesn't apply in this case, but you can still access ctx.session?.user in public procedures, just make sure the user is not undefined. It can be useful in some situations
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Knox
KnoxOP2y ago
Im not doing that YET. On my part i have a button to go to profile page but yes the objective is to detect user sign in for the first time and if so, you complete your profile. So you may want something like this: https://stackoverflow.com/questions/70739015/next-auth-check-if-user-signs-in-for-the-first-time
Stack Overflow
Next-auth check if user signs in for the first time
How to check if the it's the first time the user signs in using Next-Auth? I have tried implementing the newUser page following the official documentation, but it doesn't really seem to work. This ...
Knox
KnoxOP2y ago
For me I use a model User and a model Profile Like this
model User {
id String @id @default(cuid())
email String @unique
name String @unique
password String
emailVerified DateTime?
mealplan Mealplan[]
exercise_plan ExercisePlan[]
userBioData UserBioData?
userSuggestedPlan UserSuggestedPlan[]
accounts Account[]
sessions Session[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
deletedAt DateTime @default(now())
role Role @default(USER)
profile Profile?
}

model Profile {
id String @id @default(cuid())
firstname String?
lastname String?
gender Gender?
about String?
address String?
image Image?
contact String?
birthday String?
age Int?
country String?
user User @relation(fields: [userId], references: [id])
userId String @unique
}
model User {
id String @id @default(cuid())
email String @unique
name String @unique
password String
emailVerified DateTime?
mealplan Mealplan[]
exercise_plan ExercisePlan[]
userBioData UserBioData?
userSuggestedPlan UserSuggestedPlan[]
accounts Account[]
sessions Session[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
deletedAt DateTime @default(now())
role Role @default(USER)
profile Profile?
}

model Profile {
id String @id @default(cuid())
firstname String?
lastname String?
gender Gender?
about String?
address String?
image Image?
contact String?
birthday String?
age Int?
country String?
user User @relation(fields: [userId], references: [id])
userId String @unique
}
So signup you create the user -> is first time sign in ?
complete user profile : navigate app and eventually update user profile
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Knox
KnoxOP2y ago
Yeah tbh I created an user the good old way, have to see that tomorrow.
Want results from more Discord servers?
Add your server