Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
73e885e
this is a start?
Oct 17, 2023
1baa805
add template
Oct 18, 2023
fc0b94c
for posterity
Oct 18, 2023
3ed36a4
landing page
Oct 18, 2023
ecf8fb4
TODO: signin redirect
Nov 7, 2023
7567d14
rmv not needed func
Nov 7, 2023
a32338e
rmv images
Nov 7, 2023
9e10dff
add redirect to login
Nov 7, 2023
1709fb6
almost?
Nov 8, 2023
2e68344
add logout button to settings, chng redirect to settings page
Nov 8, 2023
9fb4b8a
so clearly theres an issue with pnpm and turbo bc why am i always goi…
Nov 8, 2023
9de8c0d
add redirects to reset
Nov 13, 2023
b85e6c1
Next res app 133 duh
Nov 13, 2023
e3ab95f
useRouter in settings, check member priviledge
Nov 14, 2023
45e63bb
incl refresh in login for userouter
Nov 14, 2023
34b0706
yer a fan of me lobster arent ye
Nov 14, 2023
94758ab
rmv stuff
Nov 14, 2023
ef6dd89
continue pairing
Nov 14, 2023
39e77ba
no iv'e seen it yer a fan of me lobster
Nov 14, 2023
8718b8b
whats the matter with ye
Nov 14, 2023
e6d92b0
hark
Nov 14, 2023
2fcdf60
Hark Triton, hark! Bellow, bid our father the Sea King rise from the …
Nov 14, 2023
9ec71d7
nly when he, crowned in cockle shells with slitherin’ tentacle tail a…
Nov 14, 2023
ee3ff50
revert chnges
Nov 14, 2023
b553053
cnt revert
Nov 14, 2023
b8b954f
Update globals.css
qweliant Nov 14, 2023
788d57e
Update globals.css
qweliant Nov 14, 2023
b89ebaa
Update page.tsx
qweliant Nov 14, 2023
c75deb3
bulging bladder no more, but a blasted bloody film now and nothing fo…
Nov 14, 2023
748b001
lock cg
Nov 14, 2023
81e67d7
forgotten to any man, to any time, forgotten to any god or devil, for…
Nov 14, 2023
e0c46ea
Update icon.svg
qweliant Nov 14, 2023
b4fad0d
last fix
Nov 14, 2023
047b955
feat: redirects
Nov 14, 2023
e700fc8
Update LoginForm.tsx
qweliant Nov 14, 2023
16ac132
Update globals.css
qweliant Nov 14, 2023
6666c2f
Update globals.css
qweliant Nov 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions core/app/(user)/login/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 (
<div className="border p-4">
<h1 className="text-2xl text-center">Login</h1>
<div className="my-10">
<form onSubmit={handleSubmit}>
<div>
Expand All @@ -38,6 +51,7 @@ export default function LoginForm() {
<input
id="email"
className="w-full"
placeholder="Enter your email address"
name="email"
value={email}
onChange={(evt) => setEmail(evt.target.value)}
Expand All @@ -50,6 +64,7 @@ export default function LoginForm() {
<input
id="password"
className="w-full"
placeholder="Enter your password"
name="password"
value={password}
type="password"
Expand All @@ -58,12 +73,7 @@ export default function LoginForm() {
</div>

<div className="my-6 text-center">
<Button
className="mr-4"
variant="outline"
type="submit"
disabled={!email || !password}
>
<Button className="mr-4" type="submit" disabled={!email || !password}>
Login
</Button>
<Link href="/forgot" className="text-sm text-gray-600 hover:underline">
Expand Down
41 changes: 39 additions & 2 deletions core/app/(user)/login/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="max-w-lg m-auto">
<div className="max-w-sm mx-auto">
<LoginForm />

<div className="text-gray-600 text-center mt-6">
Don't have an account?{" "}
<Link
href="/signup"
className="text-black hover:underline transition duration-150 ease-in-out"
>
Sign up
</Link>
</div>
</div>
);
}
17 changes: 15 additions & 2 deletions core/app/(user)/reset/ResetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand All @@ -47,7 +58,9 @@ export default function ResetForm() {
Set new password
</Button>
{error && (
<div className={"text-red-700 my-4"}>Error resetting password: {error}</div>
<div className={"text-red-700 my-4"}>
Error resetting password: {error}
</div>
)}
</form>
</div>
Expand Down
1 change: 1 addition & 0 deletions core/app/(user)/reset/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ResetForm from "./ResetForm";

export default async function Page() {
// TODO: add reset token validation
return (
<div className="max-w-lg m-auto">
<ResetForm />
Expand Down
10 changes: 7 additions & 3 deletions core/app/(user)/settings/SettingsForm.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -162,6 +163,9 @@ export default function SettingsForm({
Password reset email sent! Please check your inbox.
</div>
)}
<div className="mt-8">
<LogoutButton />
</div>
</div>
</>
);
Expand Down
28 changes: 28 additions & 0 deletions core/app/api/member/route.ts
Original file line number Diff line number Diff line change
@@ -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");
}
});
}
2 changes: 1 addition & 1 deletion core/app/api/user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}${
Expand Down
2 changes: 1 addition & 1 deletion core/app/c/[communitySlug]/LoginSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions core/app/c/[communitySlug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="flex min-h-screen">
Expand Down
3 changes: 0 additions & 3 deletions core/app/c/[communitySlug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { redirect } from "next/navigation";
import { getLoginData } from "~/lib/auth/loginData";

export default async function Page() {
return (
<>
Expand Down
1 change: 0 additions & 1 deletion core/app/c/[communitySlug]/pubs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<Button onClick={handleSignout} variant="outline" size="sm">
Expand Down
2 changes: 1 addition & 1 deletion core/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
@tailwind utilities;

/* Not clear why we need this here if we
also import ui/styles which includes it */
also import ui/styles which includes it */
4 changes: 2 additions & 2 deletions core/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Toaster } from "ui";
import "ui/styles.css";
import "./globals.css";
import InitClient from "./InitClient";
import { Toaster } from "ui";
import "./globals.css";

export const metadata = {
title: "PubPub v7 Mockup Demo",
Expand Down
40 changes: 21 additions & 19 deletions core/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import prisma from "~/prisma/db";
import { redirect } from "next/navigation";
import { getLoginData } from "~/lib/auth/loginData";
import prisma from "~/prisma/db";

export default async function Page() {
const loginData = await getLoginData();
// if user and no commuhnmitiy, redirect to settings
if (loginData) {
/* If we have a logged in user navigating to `/`, check */
/* if they are a member of any community, and if so, */
/* redirect them to that community by default. We could */
/* eventually have a query param override, but this */
/* assumes that logged in users landing on pubpub.org */
/* want to go to their dashboard a la github or twitter */
/* TODO: Does not select for member-group access yet */
const community = await prisma.community.findFirst({
where: { members: { some: { userId: loginData.id } } },
select: { slug: true },
});
if (community) {
redirect(`/c/${community.slug}`);
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 (
<>
<h1>Home</h1>
</>
);
return <>Home...</>;
}
4 changes: 2 additions & 2 deletions core/lib/auth/loginData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export const getLoginData = cache(async () => {
create: {
communityId,
canAdmin,
}
}
},
},
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion core/public/logos/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading