diff --git a/core/app/(user)/login/LoginForm.tsx b/core/app/(user)/login/LoginForm.tsx index 5f3a95fa9f..6b9c65c509 100644 --- a/core/app/(user)/login/LoginForm.tsx +++ b/core/app/(user)/login/LoginForm.tsx @@ -3,8 +3,10 @@ import React, { useState, FormEvent } from "react"; import { Button } from "ui"; import { supabase } from "lib/supabase"; import Link from "next/link"; +import { useRouter } from "next/navigation"; export default function LoginForm() { + const router = useRouter(); const [password, setPassword] = useState(""); const [email, setEmail] = useState(""); const [isLoading, setIsLoading] = useState(false); @@ -22,13 +24,24 @@ export default function LoginForm() { setIsLoading(false); setFailure(true); } else if (data) { - window.location.href = "/"; + // check if user is in a community + const response = await fetch(`/api/member?email=${data.user.email}`, { + method: "GET", + headers: { "content-type": "application/json" }, + }); + const { member } = await response.json(); + setIsLoading(false); + router.refresh(); + if (member) { + router.push(`/c/${member.community.slug}`); + } else { + router.push("/settings"); + } } - }; + } return (
-

Login

@@ -38,6 +51,7 @@ export default function LoginForm() { setEmail(evt.target.value)} @@ -50,6 +64,7 @@ export default function LoginForm() {
- diff --git a/core/app/(user)/login/page.tsx b/core/app/(user)/login/page.tsx index 736f09360f..dd5724ef3a 100644 --- a/core/app/(user)/login/page.tsx +++ b/core/app/(user)/login/page.tsx @@ -1,9 +1,46 @@ +import Link from "next/link"; import LoginForm from "./LoginForm"; +import { getLoginData } from "~/lib/auth/loginData"; +import { redirect } from "next/navigation"; +import prisma from "~/prisma/db"; -export default async function Page() { +export default async function Login() { + const loginData = await getLoginData(); + // if user and no commuhnmitiy, redirect to settings + if (loginData) { + let user; + try { + user = await prisma.user.findUnique({ + where: { email: loginData.email }, + }); + + const member = await prisma.member.findFirst({ + where: { userId: user.id }, + include: { community: true }, + }); + + if (member) { + redirect(`/c/${member.community.slug}`); + } else { + redirect("/settings"); + } + } catch { + throw new Error("Not able to redirect user"); + } + } return ( -
+
+ +
+ Don't have an account?{" "} + + Sign up + +
); } diff --git a/core/app/(user)/reset/ResetForm.tsx b/core/app/(user)/reset/ResetForm.tsx index ed252cfda9..b0014a79c4 100644 --- a/core/app/(user)/reset/ResetForm.tsx +++ b/core/app/(user)/reset/ResetForm.tsx @@ -25,8 +25,19 @@ export default function ResetForm() { setIsLoading(false); setSuccess(true); router.refresh(); + // check if user is in a community + const response = await fetch(`/api/member?email=${data.user.email}`, { + method: "GET", + headers: { "content-type": "application/json" }, + }); + const { member } = await response.json(); + setIsLoading(false); setTimeout(() => { - router.push("/"); + if (member) { + router.push(`/c/${member.community.slug}`); + } else { + router.push("/settings"); + } }, 5000); } }; @@ -47,7 +58,9 @@ export default function ResetForm() { Set new password {error && ( -
Error resetting password: {error}
+
+ Error resetting password: {error} +
)}
diff --git a/core/app/(user)/reset/page.tsx b/core/app/(user)/reset/page.tsx index 0a0058bbf2..9126581789 100644 --- a/core/app/(user)/reset/page.tsx +++ b/core/app/(user)/reset/page.tsx @@ -1,6 +1,7 @@ import ResetForm from "./ResetForm"; export default async function Page() { + // TODO: add reset token validation return (
diff --git a/core/app/(user)/settings/SettingsForm.tsx b/core/app/(user)/settings/SettingsForm.tsx index 20fa7247db..3893c63abb 100644 --- a/core/app/(user)/settings/SettingsForm.tsx +++ b/core/app/(user)/settings/SettingsForm.tsx @@ -1,9 +1,10 @@ "use client"; -import React, { useState, FormEvent } from "react"; -import { Button } from "ui"; +import { getSlugSuffix, slugifyString } from "lib/string"; import { supabase } from "lib/supabase"; import { useRouter } from "next/navigation"; -import { getSlugSuffix, slugifyString } from "lib/string"; +import { FormEvent, useState } from "react"; +import { Button } from "ui"; +import LogoutButton from "~/app/components/LogoutButton"; import { UserPutBody, UserSettings } from "~/lib/types"; type Props = UserSettings; @@ -162,6 +163,9 @@ export default function SettingsForm({ Password reset email sent! Please check your inbox.
)} +
+ +
); diff --git a/core/app/api/member/route.ts b/core/app/api/member/route.ts new file mode 100644 index 0000000000..5449429b54 --- /dev/null +++ b/core/app/api/member/route.ts @@ -0,0 +1,28 @@ +import { NextRequest, NextResponse } from "next/server"; + +import prisma from "prisma/db"; +import { handleErrors } from "~/lib/server"; + +export async function GET(req: NextRequest) { + return await handleErrors(async () => { + const email = req.nextUrl.searchParams.get("email"); + let user; + if (email) { + try { + user = await prisma.user.findUnique({ + where: { email }, + }); + } catch { + throw new Error("No user found"); + } + + const member = await prisma.member.findFirst({ + where: { userId: user.id }, + include: { community: true }, + }); + return NextResponse.json({ member, status: 200 }); + } else { + throw new Error("No email provided"); + } + }); +} diff --git a/core/app/api/user/route.ts b/core/app/api/user/route.ts index 75cfa99c09..629b339bfa 100644 --- a/core/app/api/user/route.ts +++ b/core/app/api/user/route.ts @@ -80,7 +80,7 @@ export async function POST(req: NextRequest) { // }); return NextResponse.json({ message: "Existing account claimed" }, { status: 200 }); } else { - const newUser = await prisma.user.create({ + await prisma.user.create({ data: { supabaseId: data.user.id, slug: `${slugifyString(firstName)}${ diff --git a/core/app/c/[communitySlug]/LoginSwitcher.tsx b/core/app/c/[communitySlug]/LoginSwitcher.tsx index addc3716f5..907b771193 100644 --- a/core/app/c/[communitySlug]/LoginSwitcher.tsx +++ b/core/app/c/[communitySlug]/LoginSwitcher.tsx @@ -1,6 +1,6 @@ import { getLoginData } from "~/lib/auth/loginData"; import { Avatar, AvatarFallback, AvatarImage } from "ui"; -import LogoutButton from "./LogoutButton"; +import LogoutButton from "../../components/LogoutButton"; import Link from "next/link"; export default async function LoginSwitcher() { diff --git a/core/app/c/[communitySlug]/layout.tsx b/core/app/c/[communitySlug]/layout.tsx index 16aa6da28b..c5a86e807b 100644 --- a/core/app/c/[communitySlug]/layout.tsx +++ b/core/app/c/[communitySlug]/layout.tsx @@ -31,6 +31,13 @@ export default async function MainLayout({ children, params }: Props) { if (!community) { return null; } + const member = await prisma.member.findFirst({ + where: { userId: loginData.id, communityId: community.id }, + }); + if (!member) { + redirect("/settings"); + } + const availableCommunities = await getAvailableCommunities(loginData); return (
diff --git a/core/app/c/[communitySlug]/page.tsx b/core/app/c/[communitySlug]/page.tsx index 0aa784faad..873a358fba 100644 --- a/core/app/c/[communitySlug]/page.tsx +++ b/core/app/c/[communitySlug]/page.tsx @@ -1,6 +1,3 @@ -import { redirect } from "next/navigation"; -import { getLoginData } from "~/lib/auth/loginData"; - export default async function Page() { return ( <> diff --git a/core/app/c/[communitySlug]/pubs/page.tsx b/core/app/c/[communitySlug]/pubs/page.tsx index a90c0bb34c..f6b4f36fdc 100644 --- a/core/app/c/[communitySlug]/pubs/page.tsx +++ b/core/app/c/[communitySlug]/pubs/page.tsx @@ -4,7 +4,6 @@ import PubList from "./PubList"; import PubHeader from "./PubHeader"; import { createToken } from "~/lib/server/token"; import { pubInclude, stageInclude } from "~/lib/types"; -import { redirect } from "next/navigation"; import { expect } from "utils"; const getCommunityPubs = async (communitySlug: string) => { diff --git a/core/app/c/[communitySlug]/LogoutButton.tsx b/core/app/components/LogoutButton.tsx similarity index 72% rename from core/app/c/[communitySlug]/LogoutButton.tsx rename to core/app/components/LogoutButton.tsx index 19660cb01d..550a44292e 100644 --- a/core/app/c/[communitySlug]/LogoutButton.tsx +++ b/core/app/components/LogoutButton.tsx @@ -1,11 +1,15 @@ "use client"; import { supabase } from "~/lib/supabase"; import { Button } from "ui"; +import { useRouter } from "next/navigation"; export default function LogoutButton() { + const router = useRouter(); + const handleSignout = async () => { await supabase.auth.signOut(); - window.location.href = "/"; + router.refresh(); + router.push("/"); }; return (