+ {mode === 'signin' ? 'Sign in to rNotes' : 'Create Account'} +
++ {mode === 'signin' + ? 'Use your passkey to sign in' + : 'Register with a passkey for passwordless auth'} +
++ Powered by EncryptID — passwordless, decentralized identity +
+diff --git a/Dockerfile b/Dockerfile index 1a51686..637b2dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,15 +3,18 @@ FROM node:20-alpine AS base # Dependencies stage FROM base AS deps WORKDIR /app -COPY package.json package-lock.json* ./ -COPY prisma ./prisma/ +COPY rnotes-online/package.json rnotes-online/package-lock.json* ./ +COPY rnotes-online/prisma ./prisma/ +# Copy local SDK dependency +COPY encryptid-sdk ./encryptid-sdk/ RUN npm ci || npm install # Build stage FROM base AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules -COPY . . +COPY --from=deps /app/encryptid-sdk ./encryptid-sdk +COPY rnotes-online/ . RUN npx prisma generate RUN npm run build diff --git a/docker-compose.yml b/docker-compose.yml index f7e6873..a0cee7f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,8 @@ services: rnotes: - build: . + build: + context: .. + dockerfile: rnotes-online/Dockerfile container_name: rnotes-online restart: unless-stopped environment: diff --git a/package-lock.json b/package-lock.json index 9402662..3251a5c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "rnotes-online", "version": "0.1.0", "dependencies": { + "@encryptid/sdk": "file:../encryptid-sdk", "@prisma/client": "^6.19.2", "@tiptap/extension-code-block-lowlight": "^3.19.0", "@tiptap/extension-image": "^3.19.0", @@ -38,6 +39,30 @@ "typescript": "^5" } }, + "../encryptid-sdk": { + "name": "@encryptid/sdk", + "version": "0.1.0", + "license": "MIT", + "dependencies": { + "hono": "^4.11.0" + }, + "devDependencies": { + "@types/react": "^19.0.0", + "typescript": "^5.7.0" + }, + "peerDependencies": { + "next": ">=14.0.0", + "react": ">=18.0.0" + }, + "peerDependenciesMeta": { + "next": { + "optional": true + }, + "react": { + "optional": true + } + } + }, "node_modules/@alloc/quick-lru": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/@alloc/quick-lru/-/quick-lru-5.2.0.tgz", @@ -51,6 +76,10 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/@encryptid/sdk": { + "resolved": "../encryptid-sdk", + "link": true + }, "node_modules/@floating-ui/core": { "version": "1.7.4", "resolved": "https://registry.npmjs.org/@floating-ui/core/-/core-1.7.4.tgz", diff --git a/package.json b/package.json index 4d69a97..d094480 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@tiptap/pm": "^3.19.0", "@tiptap/react": "^3.19.0", "@tiptap/starter-kit": "^3.19.0", + "@encryptid/sdk": "file:../encryptid-sdk", "dompurify": "^3.2.0", "lowlight": "^3.3.0", "marked": "^15.0.0", diff --git a/src/app/api/notebooks/[id]/canvas/route.ts b/src/app/api/notebooks/[id]/canvas/route.ts index 8559d85..33bcac2 100644 --- a/src/app/api/notebooks/[id]/canvas/route.ts +++ b/src/app/api/notebooks/[id]/canvas/route.ts @@ -1,6 +1,7 @@ import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { pushShapesToCanvas } from '@/lib/canvas-sync'; +import { requireAuth, isAuthed, getNotebookRole } from '@/lib/auth'; /** * POST /api/notebooks/[id]/canvas @@ -9,10 +10,18 @@ import { pushShapesToCanvas } from '@/lib/canvas-sync'; * with initial shapes from the notebook's notes. */ export async function POST( - _request: NextRequest, + request: NextRequest, { params }: { params: { id: string } } ) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; + const { user } = auth; + const role = await getNotebookRole(user.id, params.id); + if (!role || role === 'VIEWER') { + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); + } + const notebook = await prisma.notebook.findUnique({ where: { id: params.id }, include: { diff --git a/src/app/api/notebooks/[id]/notes/route.ts b/src/app/api/notebooks/[id]/notes/route.ts index 0867a5c..f3206b6 100644 --- a/src/app/api/notebooks/[id]/notes/route.ts +++ b/src/app/api/notebooks/[id]/notes/route.ts @@ -1,6 +1,7 @@ import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { stripHtml } from '@/lib/strip-html'; +import { requireAuth, isAuthed, getNotebookRole } from '@/lib/auth'; export async function GET( _request: NextRequest, @@ -27,6 +28,14 @@ export async function POST( { params }: { params: { id: string } } ) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; + const { user } = auth; + const role = await getNotebookRole(user.id, params.id); + if (!role || role === 'VIEWER') { + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); + } + const body = await request.json(); const { title, content, type, url, language, tags, fileUrl, mimeType, fileSize } = body; @@ -54,6 +63,7 @@ export async function POST( const note = await prisma.note.create({ data: { notebookId: params.id, + authorId: user.id, title: title.trim(), content: content || '', contentPlain, diff --git a/src/app/api/notebooks/[id]/route.ts b/src/app/api/notebooks/[id]/route.ts index b4fbe0c..d4ffb10 100644 --- a/src/app/api/notebooks/[id]/route.ts +++ b/src/app/api/notebooks/[id]/route.ts @@ -1,5 +1,6 @@ import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; +import { requireAuth, isAuthed, getNotebookRole } from '@/lib/auth'; export async function GET( _request: NextRequest, @@ -38,6 +39,14 @@ export async function PUT( { params }: { params: { id: string } } ) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; + const { user } = auth; + const role = await getNotebookRole(user.id, params.id); + if (!role || role === 'VIEWER') { + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); + } + const body = await request.json(); const { title, description, coverColor, isPublic } = body; @@ -59,10 +68,18 @@ export async function PUT( } export async function DELETE( - _request: NextRequest, + request: NextRequest, { params }: { params: { id: string } } ) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; + const { user } = auth; + const role = await getNotebookRole(user.id, params.id); + if (role !== 'OWNER') { + return NextResponse.json({ error: 'Only the owner can delete a notebook' }, { status: 403 }); + } + await prisma.notebook.delete({ where: { id: params.id } }); return NextResponse.json({ ok: true }); } catch (error) { diff --git a/src/app/api/notebooks/route.ts b/src/app/api/notebooks/route.ts index d73a7a0..a015625 100644 --- a/src/app/api/notebooks/route.ts +++ b/src/app/api/notebooks/route.ts @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { generateSlug } from '@/lib/slug'; import { nanoid } from 'nanoid'; +import { requireAuth, isAuthed } from '@/lib/auth'; export async function GET() { try { @@ -24,6 +25,9 @@ export async function GET() { export async function POST(request: NextRequest) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; + const { user } = auth; const body = await request.json(); const { title, description, coverColor } = body; @@ -44,6 +48,9 @@ export async function POST(request: NextRequest) { slug: finalSlug, description: description?.trim() || null, coverColor: coverColor || '#f59e0b', + collaborators: { + create: { userId: user.id, role: 'OWNER' }, + }, }, }); diff --git a/src/app/api/notes/[id]/route.ts b/src/app/api/notes/[id]/route.ts index 0775b90..9c9617d 100644 --- a/src/app/api/notes/[id]/route.ts +++ b/src/app/api/notes/[id]/route.ts @@ -1,6 +1,7 @@ import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { stripHtml } from '@/lib/strip-html'; +import { requireAuth, isAuthed } from '@/lib/auth'; export async function GET( _request: NextRequest, @@ -32,6 +33,22 @@ export async function PUT( { params }: { params: { id: string } } ) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; + const { user } = auth; + + // Verify the user is the author + const existing = await prisma.note.findUnique({ + where: { id: params.id }, + select: { authorId: true }, + }); + if (!existing) { + return NextResponse.json({ error: 'Note not found' }, { status: 404 }); + } + if (existing.authorId && existing.authorId !== user.id) { + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); + } + const body = await request.json(); const { title, content, type, url, language, isPinned, notebookId, tags } = body; @@ -84,10 +101,25 @@ export async function PUT( } export async function DELETE( - _request: NextRequest, + request: NextRequest, { params }: { params: { id: string } } ) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; + const { user } = auth; + + const existing = await prisma.note.findUnique({ + where: { id: params.id }, + select: { authorId: true }, + }); + if (!existing) { + return NextResponse.json({ error: 'Note not found' }, { status: 404 }); + } + if (existing.authorId && existing.authorId !== user.id) { + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); + } + await prisma.note.delete({ where: { id: params.id } }); return NextResponse.json({ ok: true }); } catch (error) { diff --git a/src/app/api/notes/route.ts b/src/app/api/notes/route.ts index af2b5ee..27daa51 100644 --- a/src/app/api/notes/route.ts +++ b/src/app/api/notes/route.ts @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { stripHtml } from '@/lib/strip-html'; import { NoteType } from '@prisma/client'; +import { requireAuth, isAuthed } from '@/lib/auth'; export async function GET(request: NextRequest) { try { @@ -38,6 +39,9 @@ export async function GET(request: NextRequest) { export async function POST(request: NextRequest) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; + const { user } = auth; const body = await request.json(); const { title, content, type, notebookId, url, language, tags, fileUrl, mimeType, fileSize } = body; @@ -69,6 +73,7 @@ export async function POST(request: NextRequest) { contentPlain, type: type || 'NOTE', notebookId: notebookId || null, + authorId: user.id, url: url || null, language: language || null, fileUrl: fileUrl || null, diff --git a/src/app/api/uploads/route.ts b/src/app/api/uploads/route.ts index 79f4059..defb67d 100644 --- a/src/app/api/uploads/route.ts +++ b/src/app/api/uploads/route.ts @@ -3,6 +3,7 @@ import { writeFile, mkdir } from 'fs/promises'; import { existsSync } from 'fs'; import path from 'path'; import { nanoid } from 'nanoid'; +import { requireAuth, isAuthed } from '@/lib/auth'; const UPLOAD_DIR = process.env.UPLOAD_DIR || '/app/uploads'; const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB @@ -26,6 +27,8 @@ function sanitizeFilename(name: string): string { export async function POST(request: NextRequest) { try { + const auth = await requireAuth(request); + if (!isAuthed(auth)) return auth; const formData = await request.formData(); const file = formData.get('file') as File | null; diff --git a/src/app/auth/signin/page.tsx b/src/app/auth/signin/page.tsx new file mode 100644 index 0000000..435c8d6 --- /dev/null +++ b/src/app/auth/signin/page.tsx @@ -0,0 +1,187 @@ +'use client'; + +import { useState, useEffect, Suspense } from 'react'; +import { useRouter, useSearchParams } from 'next/navigation'; +import Link from 'next/link'; +import { useEncryptID } from '@encryptid/sdk/ui/react'; + +function SignInForm() { + const router = useRouter(); + const searchParams = useSearchParams(); + const returnUrl = searchParams.get('returnUrl') || '/'; + const { isAuthenticated, loading: authLoading, login, register } = useEncryptID(); + + const [mode, setMode] = useState<'signin' | 'register'>('signin'); + const [username, setUsername] = useState(''); + const [error, setError] = useState(''); + const [busy, setBusy] = useState(false); + + // Redirect if already authenticated + useEffect(() => { + if (isAuthenticated && !authLoading) { + router.push(returnUrl); + } + }, [isAuthenticated, authLoading, router, returnUrl]); + + const handleSignIn = async () => { + setError(''); + setBusy(true); + try { + await login(); + router.push(returnUrl); + } catch (err) { + setError(err instanceof Error ? err.message : 'Sign in failed. Make sure you have a registered passkey.'); + } finally { + setBusy(false); + } + }; + + const handleRegister = async () => { + if (!username.trim()) { + setError('Username is required'); + return; + } + setError(''); + setBusy(true); + try { + await register(username.trim()); + router.push(returnUrl); + } catch (err) { + setError(err instanceof Error ? err.message : 'Registration failed.'); + } finally { + setBusy(false); + } + }; + + if (authLoading) { + return ( +
+ {mode === 'signin' + ? 'Use your passkey to sign in' + : 'Register with a passkey for passwordless auth'} +
++ Powered by EncryptID — passwordless, decentralized identity +
+