Get unique userID via wasp or auth0?

Hello i am wondering if there is a way to get a unique ID of some sort for every user. I managed to print out the user reg ID, which starts from 1 (the first member) etc. But im thinkin more of a "random code" simular to the "sub id" which you can get from regular auth0 app.
2 Replies
kapa.ai
kapa.ai6mo ago
Hi @Nurium. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Wasp, you can use the getFirstProviderUserId helper function to get the first user ID (e.g. username or email) that it finds for the user or null if it doesn't find any. This function can be useful if you support multiple authentication methods and you need any ID that identifies the user in your app. Here is how you can use it in your code:
import { getFirstProviderUserId } from 'wasp/auth'

const MainPage = ({ user }) => {
const userId = getFirstProviderUserId(user)
// ...
}
import { getFirstProviderUserId } from 'wasp/auth'

const MainPage = ({ user }) => {
const userId = getFirstProviderUserId(user)
// ...
}
This function returns the first user ID it finds, which could be a username or email, depending on the authentication method used. If no user ID is found, it returns null. However, if you are looking for a unique identifier similar to the "sub id" from Auth0, you might want to consider using the id field from the Auth entity in Wasp. This id is a unique identifier of the Auth entity and could serve as a unique identifier for each user. Here is how the Auth entity is defined in Wasp:
entity Auth {=psl
id String @id @default(uuid())
userId Int? @unique
// Wasp injects this relation on the User entity as well
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
identities AuthIdentity[]
sessions Session[]
psl=}
entity Auth {=psl
id String @id @default(uuid())
userId Int? @unique
// Wasp injects this relation on the User entity as well
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
identities AuthIdentity[]
sessions Session[]
psl=}
In this case, the id field is a unique identifier for each Auth entity, which could serve as a unique identifier for each user. Please note that the id field is a UUID, which means it is a random and unique string, similar to the "sub id" from Auth0. Sources: - getFirstProviderUserId (https://wasp-lang.dev/docs/auth/entities#getfirstprovideruserid) - Auth Entity (https://wasp-lang.dev/docs/auth/entities#auth-entity-) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Nurium, 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?
martinsos
martinsos6mo ago
@Nurium , I guess yuou want to show these in URL or sometihng but don't liek the idea it is just enumerated 1 by one, right? One way you could do this is by modifying your User entity in main.wasp, so its id field doesn't have autoincrement, but uuid or cuid. Those ids will be random then.
Want results from more Discord servers?
Add your server