From 12c4943f90d517fb64020a05c55d42259568d6d8 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 29 Apr 2024 19:30:43 +0100 Subject: [PATCH] feat: create user on initial signup --- src/components/UserMenu.tsx | 6 +++++- src/components/dashboard.tsx | 5 ++++- src/pages/api/auth/confirm.ts | 22 ++++++++++++++++++++-- src/server/api/routers/auth.ts | 18 +++++++++++++++++- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/components/UserMenu.tsx b/src/components/UserMenu.tsx index c9d6f9cf..4616e0a6 100644 --- a/src/components/UserMenu.tsx +++ b/src/components/UserMenu.tsx @@ -1,3 +1,4 @@ +import { useRouter } from "next/navigation"; import { Fragment } from "react"; import Image from "next/image"; import { Menu, Transition } from "@headlessui/react"; @@ -13,12 +14,15 @@ function classNames(...classes: string[]): string { } export default function UserMenu({ imageUrl, email }: UserMenuProps) { + const router = useRouter(); const { theme, switchTheme } = useTheme(); const handleLogout = async () => { const db = createClient(); await db.auth.signOut(); + + router.push("/login"); }; return ( @@ -44,7 +48,7 @@ export default function UserMenu({ imageUrl, email }: UserMenuProps) { )} - {email} + {email} diff --git a/src/components/dashboard.tsx b/src/components/dashboard.tsx index 37769521..5590ee9a 100644 --- a/src/components/dashboard.tsx +++ b/src/components/dashboard.tsx @@ -1,7 +1,10 @@ +import { api } from "~/utils/api"; import SideNavigation from "./SideNavigation"; import FeedbackButton from "./FeedbackButton"; export default function Dashboard(props: { children: React.ReactNode }) { + const { data } = api.auth.getUser.useQuery(); + return (
@@ -14,7 +17,7 @@ export default function Dashboard(props: { children: React.ReactNode }) {
- +
{props.children}
diff --git a/src/pages/api/auth/confirm.ts b/src/pages/api/auth/confirm.ts index 3ec7f62a..c913d1e8 100644 --- a/src/pages/api/auth/confirm.ts +++ b/src/pages/api/auth/confirm.ts @@ -23,11 +23,29 @@ export default async function handler( let next = "/error"; if (token_hash && type) { - const supabase = createNextClient(req, res); - const { error } = await supabase.auth.verifyOtp({ + const db = createNextClient(req, res); + const { error, data } = await db.auth.verifyOtp({ type: type as EmailOtpType, token_hash, }); + + console.log({ error }); + + if (data.user?.id) { + const user = await db + .from("user") + .select() + .eq("id", data.user.id) + .limit(1) + .maybeSingle(); + + if (!user.data) { + await db + .from("user") + .insert({ id: data.user.id, email: data.user.email ?? "" }); + } + } + if (error) { console.error(error); } else { diff --git a/src/server/api/routers/auth.ts b/src/server/api/routers/auth.ts index e1e2d0b5..0e540a0a 100644 --- a/src/server/api/routers/auth.ts +++ b/src/server/api/routers/auth.ts @@ -1,10 +1,26 @@ import { z } from "zod"; -import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; +import { + createTRPCRouter, + publicProcedure, + protectedProcedure, +} from "~/server/api/trpc"; import { env } from "~/env.mjs"; export const authRouter = createTRPCRouter({ + getUser: protectedProcedure.query(async ({ ctx }) => { + if (!ctx.user?.id) return; + + const { data } = await ctx.db + .from("user") + .select(`id, name, email`) + .eq("id", ctx.user.id) + .limit(1) + .single(); + + return data; + }), login: publicProcedure .input(z.object({ email: z.string() })) .mutation(async ({ ctx, input }) => {