User Profile and other relations to Identity

Once thing I am running into is how to add models like UserProfile or Team/Account

UserProfile would have a 1:1 relationship with a User identity
Team or Account model would have a relationship with a User identity where it’s the owner of a team. But then could be extended so other User identities could be given access to that Team.

I am having trouble with modeling this out and working with it in the context of a real application.

UserProfile

Based on what I read, there are a couple of ways to do this

  1. Use UserProfile model, and link that to a User model (but this way identity is not handled via auth library)
  2. Use the user_metadata on the Auth implementation.

Which of these methods is recommended?

In terms of how to actually set the data, in implementation there seems to be two ways:

  1. Use a identity trigger serverless function - With this method I worry about the user logging in before some required metadata is set. May cause a race-condition related error on the application?
  2. Use a success callback after the signup method, to then update the user metadata. - Similar for this method, the second call for update may fail, but the user gets created. Thus leaving that user with blank metadata which may be required for the application.

If we consider traditional MVC stacks, there is the concepts of transactions (where all the operations need to succeeds or it fails), how do we handle this with these auth providers and redwood?

Team or Account

If we consider the following models

model Feedback {

  id        String       @id @default(cuid())

  text      String

  createdAt DateTime     @default(now())

  type      FeedbackType

  Team      Team?        @relation(fields: [teamId], references: [id])

  teamId    String?

  metadata  Json?

}

model Team {

  id        String     @id @default(cuid())

  name      String

  feedbacks Feedback[]

}

When fetching the feedbacks, we may want to pass in the current logged’s user’s context. Ideally their current Team (teamId). This would be useful in Cells, when doing GraphQL queries, or in services, where we need to create a feedback for a specific team.

Is there a way to get a userContext of the current logged in user?

1 Like

@viperfx Have a look at what @Chris has implemented here:

Might give a direction.

2 Likes