Custom session token

Hey, I somehow need to issue a new token instead of using google/disc/email auth, is there a way to do that somehow? I couldnt find anything in the docs for some reason
export const getTokenByClerkId = async (req: any, res: any, context: any) => {
try {
const clerkId = req?.query?.clerkId || '';
const user = await context.entities.User.findUnique({
where: { clerkUserId: clerkId },
});

if (!user) {
throw new Error('User not found');
}
// Sent session token for get /me info
res.status(200).json({ message: user });
} catch (error) {
res.status(401).json({ message: error });
}
}
export const getTokenByClerkId = async (req: any, res: any, context: any) => {
try {
const clerkId = req?.query?.clerkId || '';
const user = await context.entities.User.findUnique({
where: { clerkUserId: clerkId },
});

if (!user) {
throw new Error('User not found');
}
// Sent session token for get /me info
res.status(200).json({ message: user });
} catch (error) {
res.status(401).json({ message: error });
}
}
14 Replies
kapa.ai
kapa.ai3mo ago
Hi @fustuk.. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the provided knowledge sources, Wasp currently supports email/password, Google, and GitHub authentication methods. There isn't any direct information available about issuing a new token instead of using these authentication methods. However, Wasp does use a Session entity to store the user's session information, which is used to keep the user logged in between page refreshes. This entity includes a unique identifier (id), an expiration date (expiresAt), and a foreign key to the Auth entity (userId). This suggests that it might be possible to create a custom session, but the specifics of how to do this are not provided in the knowledge sources. For more detailed information or assistance, you might want to check out the Wasp Discord community (https://discord.com/channels/686873244791210014/1216112386335969372) or the Wasp GitHub issues (https://github.com/wasp-lang/wasp/issues/387). Here is the Session entity for reference:
entity Session {=psl
id String @id @unique
expiresAt DateTime
userId String
auth Auth @relation(references: [id], fields: [userId], onDelete: Cascade)

@@index([userId])
psl=}
entity Session {=psl
id String @id @unique
expiresAt DateTime
userId String
auth Auth @relation(references: [id], fields: [userId], onDelete: Cascade)

@@index([userId])
psl=}
Please note that the code you provided is not part of the knowledge sources, so I can't provide specific guidance on it. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @fustuk., I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
fustuk.
fustuk.OP3mo ago
cries in spanish @Wasp Team help lads 😭
Milica
Milica3mo ago
I'm not a lad :laughboi: But I'm sure others will jump in asap. :be:
fustuk.
fustuk.OP3mo ago
sorry I summoned you here, was intended for the tech lads only 😄 @miho Can you please guide me on where to read in the docs? I saw that the cookie/bearer whatever it is, comes from Session but we dont have a model for it in prisma
miho
miho3mo ago
You can access the Session entity directly if you need to via imports from @prisma/client Some background on the Session entity: https://wasp-lang.dev/docs/auth/entities#entities-explained
fustuk.
fustuk.OP3mo ago
[ Server!] SyntaxError: The requested module '@prisma/client' does not provide an export named 'Session' I get an error when trying to import it tho
miho
miho3mo ago
- Are you using Wasp Auth? - Did you run wasp db migrate-dev?
MEE6
MEE63mo ago
Wohooo @miho, you just became a Waspeteer level 37!
miho
miho3mo ago
My bad, that's a type 🤦‍♂️ Try this: import { prisma } from 'wasp/server' and then prisma.session.find ...
fustuk.
fustuk.OP3mo ago
thanks but I still get some weird errors :/
const authId = await prisma.auth.findFirst({
where: {
userId: user.id
}
})
console.log(authId)
const y = await prisma.session.create({
data: {
userId: user.id,
id: sessionId,
expiresAt: expirationDate
}
})
const authId = await prisma.auth.findFirst({
where: {
userId: user.id
}
})
console.log(authId)
const y = await prisma.session.create({
data: {
userId: user.id,
id: sessionId,
expiresAt: expirationDate
}
})
2024-09-27 10:25:18.953 UTC [3718] ERROR: insert or update on table "Session" violates foreign key constraint "Session_userId_fkey"
2024-09-27 10:25:18.953 UTC [3718] DETAIL: Key (userId)=(0787f58e-8090-46ee-9c40-399cd6c61546) is not present in table "Auth".
2024-09-27 10:25:18.953 UTC [3718] ERROR: insert or update on table "Session" violates foreign key constraint "Session_userId_fkey"
2024-09-27 10:25:18.953 UTC [3718] DETAIL: Key (userId)=(0787f58e-8090-46ee-9c40-399cd6c61546) is not present in table "Auth".
Foreign key constraint failed on the field: `Session_userId_fkey (index)`
Foreign key constraint failed on the field: `Session_userId_fkey (index)`
But my user is in the auth table
miho
miho3mo ago
You are now hitting into Wasp internals being set up a certain way. The userId in the Session table is actually Auth.id and not a User.id 🙂 This is what we inject into the Prisma file:
model Auth {
id String @id @default(uuid())
userId Int? @unique
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
identities AuthIdentity[]
sessions Session[]
}

model AuthIdentity {
providerName String
providerUserId String
providerData String @default("{}")
authId String
auth Auth @relation(fields: [authId], references: [id], onDelete: Cascade)

@@id([providerName, providerUserId])
}

model Session {
id String @id @unique
expiresAt DateTime
userId String
auth Auth @relation(references: [id], fields: [userId], onDelete: Cascade)

@@index([userId])
}
model Auth {
id String @id @default(uuid())
userId Int? @unique
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
identities AuthIdentity[]
sessions Session[]
}

model AuthIdentity {
providerName String
providerUserId String
providerData String @default("{}")
authId String
auth Auth @relation(fields: [authId], references: [id], onDelete: Cascade)

@@id([providerName, providerUserId])
}

model Session {
id String @id @unique
expiresAt DateTime
userId String
auth Auth @relation(references: [id], fields: [userId], onDelete: Cascade)

@@index([userId])
}
fustuk.
fustuk.OP3mo ago
Thank you! I guess you guys missnamed it or something ? regardless its working now :be:
miho
miho3mo ago
Thank you! I guess you guys missnamed it or something ?
Lucia required the field to be named userId, as I said you hit into Wasp internals which we don't really document 🙂
fustuk.
fustuk.OP3mo ago
gotcha
Want results from more Discord servers?
Add your server