feat: App mobile Expo + API mobile dédiée
memento-mobile/ (Expo + React Native + expo-router): - Auth: login email/password → Bearer token (expo-secure-store) - Layout: guard auth → redirect /(auth)/login ou /(tabs)/home - Tabs: Accueil, Carnets, Recherche, Profil - Screens: login, home (recent notes + quick actions), notebooks list, note viewer (WebView HTML), search (texte), notebook detail, profile - Design: tokens brand-accent (#A47148), ink, concrete, paper, border - lib/config.ts: API_URL dev/prod configurable - lib/api.ts: apiFetch avec Bearer token automatique - lib/store.ts: Zustand auth store (login/logout/restore) memento-note/ (API mobile dédiée): - lib/mobile-auth.ts: createMobileToken / verifyMobileToken (HMAC-SHA256, 90j) - POST /api/mobile/auth/login: email+password → token + user - GET /api/mobile/auth/me: valider token, retourner profil - GET /api/mobile/notebooks: liste carnets avec nb notes - GET /api/mobile/notes: notes récentes (filtre par carnet optionnel) - GET /api/mobile/notes/[id]: contenu complet d'une note - GET /api/mobile/search: recherche fulltext titre+contenu Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
45
memento-note/app/api/mobile/auth/login/route.ts
Normal file
45
memento-note/app/api/mobile/auth/login/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { createMobileToken } from '@/lib/mobile-auth'
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { email, password } = await req.json()
|
||||
if (!email || !password) {
|
||||
return NextResponse.json({ error: 'Email et mot de passe requis' }, { status: 400 })
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email: email.toLowerCase().trim() },
|
||||
select: {
|
||||
id: true, name: true, email: true, password: true,
|
||||
subscription: { select: { tier: true } },
|
||||
},
|
||||
})
|
||||
|
||||
if (!user?.password) {
|
||||
return NextResponse.json({ error: 'Identifiants invalides' }, { status: 401 })
|
||||
}
|
||||
|
||||
const valid = await bcrypt.compare(password, user.password)
|
||||
if (!valid) {
|
||||
return NextResponse.json({ error: 'Identifiants invalides' }, { status: 401 })
|
||||
}
|
||||
|
||||
const token = createMobileToken(user.id)
|
||||
return NextResponse.json({
|
||||
token,
|
||||
user: {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
tier: user.subscription?.tier ?? 'FREE',
|
||||
},
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('[mobile/auth/login]', e)
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
25
memento-note/app/api/mobile/auth/me/route.ts
Normal file
25
memento-note/app/api/mobile/auth/me/route.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { getMobileUserId } from '@/lib/mobile-auth'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const userId = getMobileUserId(req)
|
||||
if (!userId) return NextResponse.json({ error: 'Non autorisé' }, { status: 401 })
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: {
|
||||
id: true, name: true, email: true,
|
||||
subscription: { select: { tier: true } },
|
||||
},
|
||||
})
|
||||
if (!user) return NextResponse.json({ error: 'Introuvable' }, { status: 404 })
|
||||
|
||||
return NextResponse.json({
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
tier: user.subscription?.tier ?? 'FREE',
|
||||
})
|
||||
}
|
||||
|
||||
16
memento-note/app/api/mobile/notebooks/route.ts
Normal file
16
memento-note/app/api/mobile/notebooks/route.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { getMobileUserId } from '@/lib/mobile-auth'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const userId = getMobileUserId(req)
|
||||
if (!userId) return NextResponse.json({ error: 'Non autorisé' }, { status: 401 })
|
||||
|
||||
const notebooks = await prisma.notebook.findMany({
|
||||
where: { userId, trashedAt: null },
|
||||
include: { _count: { select: { notes: { where: { trashedAt: null } } } } },
|
||||
orderBy: { order: 'asc' },
|
||||
})
|
||||
|
||||
return NextResponse.json({ notebooks })
|
||||
}
|
||||
30
memento-note/app/api/mobile/notes/[id]/route.ts
Normal file
30
memento-note/app/api/mobile/notes/[id]/route.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { getMobileUserId } from '@/lib/mobile-auth'
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const userId = getMobileUserId(req)
|
||||
if (!userId) return NextResponse.json({ error: 'Non autorisé' }, { status: 401 })
|
||||
|
||||
const { id } = await params
|
||||
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { id, userId, trashedAt: null },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
content: true,
|
||||
updatedAt: true,
|
||||
createdAt: true,
|
||||
color: true,
|
||||
notebook: { select: { id: true, name: true } },
|
||||
},
|
||||
})
|
||||
|
||||
if (!note) return NextResponse.json({ error: 'Note introuvable' }, { status: 404 })
|
||||
|
||||
return NextResponse.json({ note })
|
||||
}
|
||||
43
memento-note/app/api/mobile/notes/route.ts
Normal file
43
memento-note/app/api/mobile/notes/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { getMobileUserId } from '@/lib/mobile-auth'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const userId = getMobileUserId(req)
|
||||
if (!userId) return NextResponse.json({ error: 'Non autorisé' }, { status: 401 })
|
||||
|
||||
const { searchParams } = new URL(req.url)
|
||||
const notebookId = searchParams.get('notebookId')
|
||||
|
||||
const notes = await prisma.note.findMany({
|
||||
where: {
|
||||
userId,
|
||||
trashedAt: null,
|
||||
...(notebookId ? { notebookId } : {}),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
updatedAt: true,
|
||||
color: true,
|
||||
notebook: { select: { name: true } },
|
||||
},
|
||||
orderBy: { updatedAt: 'desc' },
|
||||
take: 50,
|
||||
})
|
||||
|
||||
const notebookName = notebookId
|
||||
? (await prisma.notebook.findUnique({ where: { id: notebookId }, select: { name: true } }))?.name
|
||||
: undefined
|
||||
|
||||
return NextResponse.json({
|
||||
notes: notes.map((n) => ({
|
||||
id: n.id,
|
||||
title: n.title,
|
||||
updatedAt: n.updatedAt,
|
||||
color: n.color,
|
||||
notebookName: n.notebook?.name,
|
||||
})),
|
||||
...(notebookName ? { notebookName } : {}),
|
||||
})
|
||||
}
|
||||
42
memento-note/app/api/mobile/search/route.ts
Normal file
42
memento-note/app/api/mobile/search/route.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { getMobileUserId } from '@/lib/mobile-auth'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const userId = getMobileUserId(req)
|
||||
if (!userId) return NextResponse.json({ error: 'Non autorisé' }, { status: 401 })
|
||||
|
||||
const { searchParams } = new URL(req.url)
|
||||
const q = searchParams.get('q')?.trim()
|
||||
if (!q) return NextResponse.json({ results: [] })
|
||||
|
||||
const notes = await prisma.note.findMany({
|
||||
where: {
|
||||
userId,
|
||||
trashedAt: null,
|
||||
OR: [
|
||||
{ title: { contains: q, mode: 'insensitive' } },
|
||||
{ content: { contains: q, mode: 'insensitive' } },
|
||||
],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
content: true,
|
||||
notebook: { select: { name: true } },
|
||||
},
|
||||
take: 20,
|
||||
orderBy: { updatedAt: 'desc' },
|
||||
})
|
||||
|
||||
const results = notes.map((n) => ({
|
||||
id: n.id,
|
||||
title: n.title,
|
||||
notebookName: n.notebook?.name,
|
||||
snippet: n.content
|
||||
? n.content.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 160)
|
||||
: '',
|
||||
}))
|
||||
|
||||
return NextResponse.json({ results })
|
||||
}
|
||||
Reference in New Issue
Block a user