Updating existing T3 with Supabase Auth from Next Auth

Background: - Launched a T3 app with Next Auth. - Currently got 3000 users. - Launching an app version (expo), so migrating T3 app to T3-turbo. - Upgrading Next-Auth to Supabase Auth to work with React Native, want to keep existing users and ease migration. - Supabase generates an auth.users table. I'm looking to guidance that I'm on the right track with my implementation. 1. I've added a new field to my existing Users table called SupabaseId. This will be the reference for Supabase Auth and my Users table. (Usually both id's would be the same.). 2. Created a webhook from Supabase when a new user is created in Supabase Auth (Signs up,in). The webhook hits an API to create a new user in the Users table of my DB and added the Supabase ID. 3. The new TRPC context. This bit feels abit hairy. I'm having to extend the supabase user and lookup the user from my DB on each request to add in the users ID and role.
export const createTRPCContext = async (opts: CreateNextContextOptions) => {

const supabase = createServerSupabaseClient(opts)

const token = opts.req.headers.authorization

const user = token
? await supabase.auth.getUser(token)
: await supabase.auth.getUser()

if (!user?.data?.user) {
return createInnerTRPCContext({
user: null,
})
}

//Also get the user from the DB if there is a user

const prismaUser = await prisma.user.findUnique({
where: {
email: user?.data?.user?.email,
},
})

let extendedUser: ExtendedUser | null = null
if (user.data.user) {
extendedUser = {
...user.data?.user,
supabaseId: user?.data?.user?.id || null,
id: prismaUser ? prismaUser.id : "",
DBUserRole: prismaUser ? prismaUser.role : null,
}
}

return createInnerTRPCContext({
user: extendedUser,
})
}
export const createTRPCContext = async (opts: CreateNextContextOptions) => {

const supabase = createServerSupabaseClient(opts)

const token = opts.req.headers.authorization

const user = token
? await supabase.auth.getUser(token)
: await supabase.auth.getUser()

if (!user?.data?.user) {
return createInnerTRPCContext({
user: null,
})
}

//Also get the user from the DB if there is a user

const prismaUser = await prisma.user.findUnique({
where: {
email: user?.data?.user?.email,
},
})

let extendedUser: ExtendedUser | null = null
if (user.data.user) {
extendedUser = {
...user.data?.user,
supabaseId: user?.data?.user?.id || null,
id: prismaUser ? prismaUser.id : "",
DBUserRole: prismaUser ? prismaUser.role : null,
}
}

return createInnerTRPCContext({
user: extendedUser,
})
}
Overall wondering if this approach is the correct one? My db id's are all cuid's and kind of wish I just started the uuid's and they seem much more used in industry. Thanks
2 Replies
davidnaak
davidnaak2y ago
Hi, I'm also building a web app and will need to build a react-native app later on. Would you recommend starting out with supabase auth straight away as it integrates into both? Also, have you looked at solito.dev?
Complexlity
Complexlity2y ago
Hello! Seeing the current context, he’s looking for answers himself. It feels burdensome to ask a question to him while he’s looking for help himself . I suggest you create a new post. This way you can get answers from anyone who’s looking
Want results from more Discord servers?
Add your server