vibbin
vibbin
Explore posts from servers
TTCTheo's Typesafe Cult
Created by rafaelsilva81 on 1/5/2024 in #questions
Syncing data from clerk sign up to database
export async function syncUser() {

const clerkUser = await currentUser();

if (!clerkUser?.id) {
return null;
}

try {
let user = await db.user.findUnique({
where: {
clerkId: clerkUser.id as string,
},
});

if (!user) {
user = await db.user.create({
data: {
clerkId: clerkUser.id,
name: clerkUser.firstName,
email: clerkUser.emailAddresses?.[0]?.emailAddress,
image: clerkUser.imageUrl,
levels: {
create: { level: 1, xp: 0 },
},
},
});
}

return user;
} catch (error) {
console.error('Error syncing user:', error);
return null;
}
}
export async function syncUser() {

const clerkUser = await currentUser();

if (!clerkUser?.id) {
return null;
}

try {
let user = await db.user.findUnique({
where: {
clerkId: clerkUser.id as string,
},
});

if (!user) {
user = await db.user.create({
data: {
clerkId: clerkUser.id,
name: clerkUser.firstName,
email: clerkUser.emailAddresses?.[0]?.emailAddress,
image: clerkUser.imageUrl,
levels: {
create: { level: 1, xp: 0 },
},
},
});
}

return user;
} catch (error) {
console.error('Error syncing user:', error);
return null;
}
}
7 replies
TTCTheo's Typesafe Cult
Created by rafaelsilva81 on 1/5/2024 in #questions
Syncing data from clerk sign up to database
I've created a function in my utils folder that I call in my /onboarding page (server component)
7 replies
TTCTheo's Typesafe Cult
Created by Circus on 11/27/2023 in #questions
Clerk Webhooks with Prisma (Sync database, user model)
I'm currently doing this without webhooks but feel like I should be using webhooks. Currently after signin/signup I am redirecting to /new-user
import { db } from '@/server/db/client'
import { currentUser } from '@clerk/nextjs'
import { redirect } from 'next/navigation'

const createNewUser = async (): Promise<void> => {
try {
const clerkUser = await currentUser()

if (!clerkUser?.id) {
throw new Error('Clerk user ID is undefined.')
}

const match = await db.user.findUnique({
where: {
clerkId: clerkUser.id,
},
})

if (match && match.onboarded) {
redirect('/')
} else if (!match) {
await db.user.create({
data: {
clerkId: clerkUser.id,
name: clerkUser.firstName,
email: clerkUser.emailAddresses[0]?.emailAddress,
createdAt: new Date(),

},
})
redirect('/onboarding')
}

redirect('/onboarding')
} catch (error) {
console.error('Error creating new user:', error)
}
}

const NewUser = async () => {
await createNewUser()
return <div>...loading</div>
}

export default NewUser
import { db } from '@/server/db/client'
import { currentUser } from '@clerk/nextjs'
import { redirect } from 'next/navigation'

const createNewUser = async (): Promise<void> => {
try {
const clerkUser = await currentUser()

if (!clerkUser?.id) {
throw new Error('Clerk user ID is undefined.')
}

const match = await db.user.findUnique({
where: {
clerkId: clerkUser.id,
},
})

if (match && match.onboarded) {
redirect('/')
} else if (!match) {
await db.user.create({
data: {
clerkId: clerkUser.id,
name: clerkUser.firstName,
email: clerkUser.emailAddresses[0]?.emailAddress,
createdAt: new Date(),

},
})
redirect('/onboarding')
}

redirect('/onboarding')
} catch (error) {
console.error('Error creating new user:', error)
}
}

const NewUser = async () => {
await createNewUser()
return <div>...loading</div>
}

export default NewUser
I have my own User model that has an onboarding field of boolean. If I integrated webhooks how fast is this? After signup and redirect to /onboarding will it create a new user in my db in time for the user? How do people handle this in Clerk when you need to onboard users with your own data?
30 replies
TTCTheo's Typesafe Cult
Created by DraganovicDr on 11/27/2023 in #questions
Seeking Advice for a T3 Stack Application: Separating tRPC from NextJS
Sounds like you're looking for something like t3-turbo? https://github.com/t3-oss/create-t3-turbo
14 replies
TTCTheo's Typesafe Cult
Created by Pavstermeister on 11/5/2023 in #questions
Configuring Clerk on the latest release of t3 with tRPC
Would be interested in listening to the stream/discussion - what platform was that on?
123 replies
TTCTheo's Typesafe Cult
Created by Pavstermeister on 11/5/2023 in #questions
Configuring Clerk on the latest release of t3 with tRPC
And idea how to get Clerk auth to flow through into trpc context? I'm getting null values when trying to access my Clerk user details
import { initTRPC, TRPCError } from "@trpc/server";
import { type NextRequest } from "next/server";
import superjson from "superjson";
import { ZodError } from "zod";
import type {
SignedInAuthObject,
SignedOutAuthObject,
} from "@clerk/nextjs/server";
import { getAuth } from "@clerk/nextjs/server";

import { db } from "~/server/db";


interface CreateContextOptions {
req: NextRequest;
auth?: SignedInAuthObject | SignedOutAuthObject;
}

export const createInnerTRPCContext = (opts: CreateContextOptions) => {
const auth = getAuth(opts.req);
console.log(auth);

return {
...opts,
auth,
db,
};
};

export const createTRPCContext = (opts: { req: NextRequest }) => {
// Fetch stuff that depends on the request

return createInnerTRPCContext({
req: opts.req,
});
};
import { initTRPC, TRPCError } from "@trpc/server";
import { type NextRequest } from "next/server";
import superjson from "superjson";
import { ZodError } from "zod";
import type {
SignedInAuthObject,
SignedOutAuthObject,
} from "@clerk/nextjs/server";
import { getAuth } from "@clerk/nextjs/server";

import { db } from "~/server/db";


interface CreateContextOptions {
req: NextRequest;
auth?: SignedInAuthObject | SignedOutAuthObject;
}

export const createInnerTRPCContext = (opts: CreateContextOptions) => {
const auth = getAuth(opts.req);
console.log(auth);

return {
...opts,
auth,
db,
};
};

export const createTRPCContext = (opts: { req: NextRequest }) => {
// Fetch stuff that depends on the request

return createInnerTRPCContext({
req: opts.req,
});
};
123 replies
TTCTheo's Typesafe Cult
Created by gwilliamnn on 10/17/2023 in #questions
New App Router is not working!
Are you pushing the db by running: npm run db:push
10 replies
TTCTheo's Typesafe Cult
Created by pilatos on 6/26/2023 in #questions
MacBook Air (15inch) vs MacBook Pro (14inch vs 16inch)
I had this exact same contemplation 2 weeks ago. Ended up getting the MBP 16inch refurbished (saving £400). V happy with my choice, the 16inch weight doesn't bother me at all.
38 replies
TTCTheo's Typesafe Cult
Created by not a robot on 5/31/2023 in #questions
Migrating to supabase from create-t3-turbo
I Think you want to create a supabase webhook that calls an api route in your next app. From there you can create the User from the supabase data.
3 replies
TTCTheo's Typesafe Cult
Created by Brenner on 1/9/2023 in #questions
Tailwind Default Colors not showing automatically
Only add extra colours inside the extend object. You don't need the theme one I believe otherwise you override all others
7 replies
TTCTheo's Typesafe Cult
Created by CuriouslyCory on 12/20/2022 in #questions
Video hosting solutions
Cloudinary, Mux
10 replies
TTCTheo's Typesafe Cult
Created by joshborseth on 12/1/2022 in #questions
Tech Stack Question
I've literally this week started building a marketing site for a business using Sanity + Next 13. There's a 'landing page' template (built in js) and a newer blog template (built in ts) from sanity & next as good starting points. I'm building out a set of components that the business will be able to then use as they see fit. Enjoying it so far.
15 replies
TTCTheo's Typesafe Cult
Created by theFern on 11/30/2022 in #questions
Any recommendations for a date library?
date-fns is good. There's also luxon https://moment.github.io/luxon/#/
6 replies
TTCTheo's Typesafe Cult
Created by nexxel on 11/21/2022 in #questions
need help with a complex db query
Just do them as two separate queries and then combine both results into an array pre sorted?
46 replies
TTCTheo's Typesafe Cult
Created by vibbin on 11/9/2022 in #questions
Serializing date with superjson
Error code I'm getting is for the verifiedemail object. Error: Error serializing .user.emailVerified returned from getServerSideProps in "/onboarding". Reason: object ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.
8 replies