Reimplemented outdated code, added encryption keys for each room, added support for profile images, added friend requests via url/qr-code
This commit is contained in:
1 parent
508f17315e
commit
32e4bf8b32
58 files changed
+3270
-362
No files matched your search
@@ -0,0 +1,107 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
|
||||
export type UpdateNameState = { error: string } | { success: true } | null;
|
||||
|
||||
export async function updateNameAction(
|
||||
_prevState: UpdateNameState,
|
||||
formData: FormData,
|
||||
): Promise<UpdateNameState> {
|
||||
const name = formData.get("name");
|
||||
if (typeof name !== "string" || !name.trim()) {
|
||||
return { error: "Name cannot be empty." };
|
||||
}
|
||||
|
||||
const trimmed = name.trim();
|
||||
if (trimmed.length > 50) {
|
||||
return { error: "Name must be 50 characters or less." };
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
if (!user) return { error: "Not authenticated" };
|
||||
|
||||
const { error } = await supabase
|
||||
.from("users")
|
||||
.update({ name: trimmed })
|
||||
.eq("id", user.id);
|
||||
|
||||
if (error) {
|
||||
return { error: error.message };
|
||||
}
|
||||
|
||||
// Update friend_alias in all friends rows where this user is the friend
|
||||
// so that other users see the new display name
|
||||
await supabase
|
||||
.from("friends")
|
||||
.update({ friend_alias: trimmed })
|
||||
.eq("friend_id", user.id);
|
||||
|
||||
revalidatePath("/profile");
|
||||
revalidatePath("/friends");
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export type UploadAvatarState = { error: string } | { success: true } | null;
|
||||
|
||||
export async function uploadAvatarAction(
|
||||
_prevState: UploadAvatarState,
|
||||
formData: FormData,
|
||||
): Promise<UploadAvatarState> {
|
||||
const file = formData.get("avatar");
|
||||
if (!(file instanceof File) || file.size === 0) {
|
||||
return { error: "Please select an image file." };
|
||||
}
|
||||
|
||||
// Validate file type
|
||||
if (file.type !== "image/png" && file.type !== "image/jpeg") {
|
||||
return { error: "Only PNG and JPG images are allowed." };
|
||||
}
|
||||
|
||||
// Validate file size (2MB)
|
||||
if (file.size > 2 * 1024 * 1024) {
|
||||
return { error: "Image must be smaller than 2MB." };
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
if (!user) return { error: "Not authenticated" };
|
||||
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const filePath = `${user.id}/avatar`;
|
||||
|
||||
const { error: uploadError } = await supabase.storage
|
||||
.from("avatars")
|
||||
.upload(filePath, arrayBuffer, {
|
||||
contentType: file.type,
|
||||
upsert: true,
|
||||
});
|
||||
|
||||
if (uploadError) {
|
||||
return { error: uploadError.message };
|
||||
}
|
||||
|
||||
// Store the storage path — signed URLs are generated on read
|
||||
const avatarPath = filePath;
|
||||
|
||||
// Update user's avatar_url with the storage path
|
||||
const { error: updateError } = await supabase
|
||||
.from("users")
|
||||
.update({ avatar_url: avatarPath })
|
||||
.eq("id", user.id);
|
||||
|
||||
if (updateError) {
|
||||
return { error: updateError.message };
|
||||
}
|
||||
|
||||
revalidatePath("/profile");
|
||||
revalidatePath("/friends");
|
||||
revalidatePath("/rooms");
|
||||
return { success: true };
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
"use client";
|
||||
|
||||
import { useActionState, useRef } from "react";
|
||||
import { uploadAvatarAction, type UploadAvatarState } from "./actions";
|
||||
import Image from "next/image";
|
||||
|
||||
const initialState: UploadAvatarState = null;
|
||||
|
||||
export function AvatarUpload({ avatarUrl }: { avatarUrl: string | null }) {
|
||||
const [state, action, pending] = useActionState(
|
||||
uploadAvatarAction,
|
||||
initialState,
|
||||
);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const handleClick = () => {
|
||||
inputRef.current?.click();
|
||||
};
|
||||
|
||||
const handleChange = () => {
|
||||
const file = inputRef.current?.files?.[0];
|
||||
if (!file) return;
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("avatar", file);
|
||||
|
||||
// Submit via the form's requestSubmit to trigger the action
|
||||
const form = inputRef.current?.closest("form");
|
||||
if (form) {
|
||||
form.requestSubmit();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<form action={action} className="inline-block">
|
||||
<input
|
||||
ref={inputRef}
|
||||
name="avatar"
|
||||
type="file"
|
||||
accept="image/png,image/jpeg,image/jpg"
|
||||
className="hidden"
|
||||
onChange={handleChange}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClick}
|
||||
disabled={pending}
|
||||
className="group relative"
|
||||
>
|
||||
{avatarUrl ? (
|
||||
<Image
|
||||
src={avatarUrl}
|
||||
alt=""
|
||||
width={200}
|
||||
height={200}
|
||||
className="size-20 rounded-full border-4 border-white object-cover shadow-md transition group-hover:opacity-80"
|
||||
/>
|
||||
) : (
|
||||
<div className="flex size-20 items-center justify-center rounded-full border-4 border-white bg-neutral-900 text-2xl font-semibold text-white shadow-md transition group-hover:bg-neutral-700">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
className="size-6"
|
||||
>
|
||||
<path d="M10 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6ZM3.465 14.493a1.23 1.23 0 0 0 .41 1.412A9.957 9.957 0 0 0 10 18c2.31 0 4.438-.784 6.131-2.1.43-.333.604-.903.408-1.41a7.002 7.002 0 0 0-13.074.003Z" />
|
||||
</svg>
|
||||
</div>
|
||||
)}
|
||||
{pending && (
|
||||
<div className="absolute inset-0 flex items-center justify-center rounded-full bg-black/30">
|
||||
<div className="size-5 animate-spin rounded-full border-2 border-white border-t-transparent" />
|
||||
</div>
|
||||
)}
|
||||
</button>
|
||||
{state && "error" in state && (
|
||||
<p className="mt-2 text-center text-xs text-red-600">{state.error}</p>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
useActionState,
|
||||
useRef,
|
||||
useEffect,
|
||||
useState,
|
||||
useCallback,
|
||||
} from "react";
|
||||
import { updateNameAction, type UpdateNameState } from "./actions";
|
||||
|
||||
const initialState: UpdateNameState = null;
|
||||
|
||||
export function EditableName({ currentName }: { currentName: string }) {
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [state, action, pending] = useActionState(
|
||||
updateNameAction,
|
||||
initialState,
|
||||
);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
// Focus input when entering edit mode
|
||||
useEffect(() => {
|
||||
if (editing) {
|
||||
inputRef.current?.focus();
|
||||
inputRef.current?.select();
|
||||
}
|
||||
}, [editing]);
|
||||
|
||||
const handleEdit = useCallback(() => setEditing(true), []);
|
||||
|
||||
if (!editing) {
|
||||
return (
|
||||
<div className="mt-3 flex items-center gap-3">
|
||||
<h2 className="text-xl font-semibold text-neutral-900">
|
||||
{currentName}
|
||||
</h2>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleEdit}
|
||||
className="rounded-md px-2 py-1 text-xs font-medium text-neutral-500 transition hover:bg-neutral-100 hover:text-neutral-900"
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<form action={action} className="mt-3">
|
||||
<div className="flex items-start gap-2">
|
||||
<input
|
||||
ref={inputRef}
|
||||
name="name"
|
||||
type="text"
|
||||
defaultValue={currentName}
|
||||
required
|
||||
maxLength={50}
|
||||
className="w-64 rounded-lg border border-neutral-300 bg-white px-3 py-1.5 text-lg font-semibold text-neutral-900 shadow-sm focus:border-neutral-900 focus:outline-none"
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={pending}
|
||||
className="rounded-lg bg-neutral-900 px-3 py-1.5 text-sm font-medium text-white shadow-sm transition hover:bg-neutral-800 disabled:cursor-not-allowed disabled:bg-neutral-400"
|
||||
>
|
||||
{pending ? "..." : "Save"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setEditing(false)}
|
||||
className="rounded-lg border border-neutral-300 px-3 py-1.5 text-sm font-medium text-neutral-700 transition hover:bg-neutral-50"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
{state && "error" in state && (
|
||||
<p className="mt-1 text-xs text-red-600">{state.error}</p>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import { redirect } from "next/navigation";
|
||||
import { headers } from "next/headers";
|
||||
import type { Profile } from "@/types/profile";
|
||||
import { getSignedAvatarUrl } from "@/utils/assets/avatars";
|
||||
import { EditableName } from "./editable-name";
|
||||
import { ShareProfileButton } from "./share-profile";
|
||||
import { AvatarUpload } from "./avatar-upload";
|
||||
|
||||
export default async function ProfilePage() {
|
||||
const supabase = await createClient();
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (!user) return redirect("/login");
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from("users")
|
||||
.select("*")
|
||||
.eq("id", user.id)
|
||||
.single<Profile>();
|
||||
|
||||
if (!profile) {
|
||||
return (
|
||||
<div className="flex h-full items-center justify-center">
|
||||
<p className="text-neutral-500">Profile not found.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Generate a signed URL for the user's own avatar
|
||||
const avatarUrl = profile.avatar_url
|
||||
? await getSignedAvatarUrl(profile.id)
|
||||
: null;
|
||||
|
||||
const memberSince = new Date(profile.created_at).toLocaleDateString("en-US", {
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
});
|
||||
|
||||
// Build the full share URL for the QR code
|
||||
const requestHeaders = await headers();
|
||||
const host = requestHeaders.get("host") ?? "localhost:3000";
|
||||
const protocol = requestHeaders.get("x-forwarded-proto") ?? "https";
|
||||
const basePath = process.env.BASE_PATH ?? "";
|
||||
const shareUrl = `${protocol}://${host}${basePath}/friends/add?id=${profile.id}`;
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-3xl px-6 py-10">
|
||||
{/* Header */}
|
||||
<h1 className="text-2xl font-semibold text-neutral-900">Profile</h1>
|
||||
<p className="mt-1 text-sm text-neutral-500">
|
||||
Manage your account information.
|
||||
</p>
|
||||
|
||||
{/* Profile Card */}
|
||||
<div className="mt-8 rounded-xl border border-neutral-200 bg-white shadow-sm">
|
||||
{/* Banner area */}
|
||||
<div className="h-28 rounded-t-xl bg-linear-to-br from-neutral-800 to-neutral-600" />
|
||||
|
||||
{/* Avatar + name */}
|
||||
<div className="relative px-6 pb-6">
|
||||
<AvatarUpload avatarUrl={avatarUrl} />
|
||||
|
||||
<EditableName key={profile.name} currentName={profile.name} />
|
||||
<div className="mt-3 flex items-center gap-3">
|
||||
<p className="text-sm text-neutral-500">{profile.email}</p>
|
||||
<ShareProfileButton userId={profile.id} shareUrl={shareUrl} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Details */}
|
||||
<div className="mt-6 space-y-4">
|
||||
<div className="rounded-xl border border-neutral-200 bg-white shadow-sm">
|
||||
<div className="border-b border-neutral-100 px-6 py-4">
|
||||
<h3 className="text-sm font-semibold text-neutral-900">
|
||||
Account Details
|
||||
</h3>
|
||||
</div>
|
||||
<dl className="divide-y divide-neutral-100">
|
||||
<DetailRow label="User ID" value={profile.id} mono />
|
||||
<DetailRow label="Display Name" value={profile.name} />
|
||||
<DetailRow label="Email" value={profile.email} />
|
||||
<DetailRow label="Member Since" value={memberSince} />
|
||||
<DetailRow
|
||||
label="Identity Verified"
|
||||
value={profile.has_identity ? "Yes" : "No"}
|
||||
badge={profile.has_identity ? "green" : "yellow"}
|
||||
/>
|
||||
<DetailRow
|
||||
label="Encryption Keys"
|
||||
value={
|
||||
profile.private_key_enc && profile.public_key_dec
|
||||
? "Set up"
|
||||
: "Not configured"
|
||||
}
|
||||
badge={
|
||||
profile.private_key_enc && profile.public_key_dec
|
||||
? "green"
|
||||
: "yellow"
|
||||
}
|
||||
/>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function DetailRow({
|
||||
label,
|
||||
value,
|
||||
mono,
|
||||
badge,
|
||||
}: {
|
||||
label: string;
|
||||
value: string;
|
||||
mono?: boolean;
|
||||
badge?: "green" | "yellow";
|
||||
}) {
|
||||
return (
|
||||
<div className="flex items-center justify-between px-6 py-3.5">
|
||||
<dt className="text-sm text-neutral-500">{label}</dt>
|
||||
<dd>
|
||||
{badge ? (
|
||||
<span
|
||||
className={`inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium ${
|
||||
badge === "green"
|
||||
? "bg-emerald-50 text-emerald-700"
|
||||
: "bg-amber-50 text-amber-700"
|
||||
}`}
|
||||
>
|
||||
{value}
|
||||
</span>
|
||||
) : (
|
||||
<span
|
||||
className={`text-sm text-neutral-900 ${mono ? "font-mono text-xs" : ""}`}
|
||||
>
|
||||
{value}
|
||||
</span>
|
||||
)}
|
||||
</dd>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useCallback } from "react";
|
||||
import { QRCodeSVG } from "qrcode.react";
|
||||
|
||||
export function ShareProfileButton({ userId, shareUrl }: { userId: string; shareUrl: string }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
const handleCopy = useCallback(() => {
|
||||
navigator.clipboard.writeText(shareUrl).then(() => {
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
});
|
||||
}, [shareUrl]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen(true)}
|
||||
className="rounded-lg bg-neutral-900 px-4 py-2 text-sm font-medium text-white shadow-sm transition hover:bg-neutral-800"
|
||||
>
|
||||
Share Profile
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<>
|
||||
{/* Backdrop */}
|
||||
<div
|
||||
className="fixed inset-0 z-40 bg-black/40"
|
||||
onClick={() => setOpen(false)}
|
||||
/>
|
||||
|
||||
{/* Modal */}
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
|
||||
<div className="w-full max-w-sm rounded-2xl border border-neutral-200 bg-white p-6 shadow-xl">
|
||||
<div className="flex items-center justify-between">
|
||||
<h2 className="text-lg font-semibold text-neutral-900">
|
||||
Share Profile
|
||||
</h2>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen(false)}
|
||||
className="flex size-8 items-center justify-center rounded-lg text-neutral-400 transition hover:bg-neutral-100 hover:text-neutral-600"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
className="size-5"
|
||||
>
|
||||
<path d="M6.28 5.22a.75.75 0 0 0-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 1 0 1.06 1.06L10 11.06l3.72 3.72a.75.75 0 1 0 1.06-1.06L11.06 10l3.72-3.72a.75.75 0 0 0-1.06-1.06L10 8.94 6.28 5.22Z" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p className="mt-2 text-sm text-neutral-500">
|
||||
Scan this QR code or share the link to add you as a friend.
|
||||
</p>
|
||||
|
||||
{/* QR Code */}
|
||||
<div className="mt-5 flex justify-center rounded-xl border border-neutral-100 bg-white p-4">
|
||||
<QRCodeSVG
|
||||
value={shareUrl}
|
||||
size={200}
|
||||
level="M"
|
||||
includeMargin={false}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Link + Copy */}
|
||||
<div className="mt-4 flex items-center gap-2">
|
||||
<input
|
||||
type="text"
|
||||
readOnly
|
||||
value={shareUrl}
|
||||
className="flex-1 rounded-lg border border-neutral-200 bg-neutral-50 px-3 py-2 text-xs font-mono text-neutral-600 focus:outline-none"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleCopy}
|
||||
className="shrink-0 rounded-lg border border-neutral-200 px-3 py-2 text-xs font-medium text-neutral-700 transition hover:bg-neutral-100"
|
||||
>
|
||||
{copied ? "Copied!" : "Copy"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* User ID reference */}
|
||||
<p className="mt-3 text-center text-xs text-neutral-400">
|
||||
User ID: {userId}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user