feat: Implement initial with admin and mobile clients, authentication, data models, and lead generation scripts.
This commit is contained in:
60
innungsapp/apps/admin/app/api/setup/route.ts
Normal file
60
innungsapp/apps/admin/app/api/setup/route.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* DEV-ONLY: Sets a password for the demo admin user via better-auth.
|
||||
* Call once after seeding: GET http://localhost:3032/api/setup
|
||||
* Remove this file before going to production.
|
||||
*/
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@innungsapp/shared'
|
||||
|
||||
export async function GET() {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
return NextResponse.json({ error: 'Not available in production' }, { status: 403 })
|
||||
}
|
||||
|
||||
// Delete the pre-seeded user so better-auth can create it fresh with a hashed password
|
||||
await prisma.account.deleteMany({ where: { userId: 'demo-admin-user-id' } })
|
||||
await prisma.member.deleteMany({ where: { userId: 'demo-admin-user-id' } })
|
||||
await prisma.userRole.deleteMany({ where: { userId: 'demo-admin-user-id' } })
|
||||
await prisma.user.deleteMany({ where: { id: 'demo-admin-user-id' } })
|
||||
|
||||
// Re-create via better-auth so the password is properly hashed
|
||||
const result = await auth.api.signUpEmail({
|
||||
body: { email: 'admin@demo.de', password: 'demo1234', name: 'Demo Admin' },
|
||||
})
|
||||
|
||||
if (!result?.user) {
|
||||
return NextResponse.json({ error: 'signUp failed', result }, { status: 500 })
|
||||
}
|
||||
|
||||
const newUserId = result.user.id
|
||||
|
||||
// Restore org membership for the new user ID
|
||||
const org = await prisma.organization.findFirst({ where: { slug: 'innung-elektro-stuttgart' } })
|
||||
if (org) {
|
||||
await prisma.userRole.upsert({
|
||||
where: { orgId_userId: { orgId: org.id, userId: newUserId } },
|
||||
update: {},
|
||||
create: { orgId: org.id, userId: newUserId, role: 'admin' },
|
||||
})
|
||||
await prisma.member.upsert({
|
||||
where: { userId: newUserId },
|
||||
update: {},
|
||||
create: {
|
||||
orgId: org.id,
|
||||
userId: newUserId,
|
||||
name: 'Demo Admin',
|
||||
betrieb: 'Innungsgeschäftsstelle',
|
||||
sparte: 'Elektrotechnik',
|
||||
ort: 'Stuttgart',
|
||||
email: 'admin@demo.de',
|
||||
status: 'aktiv',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
message: 'Setup complete. Login: admin@demo.de / demo1234',
|
||||
})
|
||||
}
|
||||
@@ -3,14 +3,16 @@
|
||||
import { useState } from 'react'
|
||||
import { createAuthClient } from 'better-auth/react'
|
||||
import { magicLinkClient } from 'better-auth/client/plugins'
|
||||
|
||||
const authClient = createAuthClient({
|
||||
baseURL: process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3000',
|
||||
plugins: [magicLinkClient()],
|
||||
})
|
||||
|
||||
type Mode = 'password' | 'magic'
|
||||
|
||||
export default function LoginPage() {
|
||||
const [mode, setMode] = useState<Mode>('password')
|
||||
const [email, setEmail] = useState('')
|
||||
const [password, setPassword] = useState('')
|
||||
const [sent, setSent] = useState(false)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
@@ -20,16 +22,29 @@ export default function LoginPage() {
|
||||
setLoading(true)
|
||||
setError('')
|
||||
|
||||
const result = await authClient.signIn.magicLink({
|
||||
email,
|
||||
callbackURL: '/dashboard',
|
||||
})
|
||||
|
||||
setLoading(false)
|
||||
if (result.error) {
|
||||
setError(result.error.message ?? 'Ein Fehler ist aufgetreten.')
|
||||
if (mode === 'password') {
|
||||
const result = await authClient.signIn.email({
|
||||
email,
|
||||
password,
|
||||
callbackURL: '/dashboard',
|
||||
})
|
||||
setLoading(false)
|
||||
if (result.error) {
|
||||
setError(result.error.message ?? 'E-Mail oder Passwort falsch.')
|
||||
} else {
|
||||
window.location.href = '/dashboard'
|
||||
}
|
||||
} else {
|
||||
setSent(true)
|
||||
const result = await authClient.signIn.magicLink({
|
||||
email,
|
||||
callbackURL: '/dashboard',
|
||||
})
|
||||
setLoading(false)
|
||||
if (result.error) {
|
||||
setError(result.error.message ?? 'Ein Fehler ist aufgetreten.')
|
||||
} else {
|
||||
setSent(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,9 +68,7 @@ export default function LoginPage() {
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
|
||||
</svg>
|
||||
</div>
|
||||
<h2 className="text-xl font-semibold text-gray-900 mb-2">
|
||||
E-Mail gesendet!
|
||||
</h2>
|
||||
<h2 className="text-xl font-semibold text-gray-900 mb-2">E-Mail gesendet!</h2>
|
||||
<p className="text-gray-500">
|
||||
Wir haben einen Login-Link an <strong>{email}</strong> gesendet.
|
||||
Bitte überprüfen Sie Ihr Postfach.
|
||||
@@ -69,15 +82,33 @@ export default function LoginPage() {
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<h2 className="text-xl font-semibold text-gray-900 mb-6">
|
||||
Anmelden
|
||||
</h2>
|
||||
<h2 className="text-xl font-semibold text-gray-900 mb-6">Anmelden</h2>
|
||||
|
||||
{/* Mode toggle */}
|
||||
<div className="flex rounded-lg border border-gray-200 p-1 mb-6">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setMode('password')}
|
||||
className={`flex-1 py-1.5 text-sm rounded-md font-medium transition-colors ${
|
||||
mode === 'password' ? 'bg-brand-500 text-white' : 'text-gray-500 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
Passwort
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setMode('magic')}
|
||||
className={`flex-1 py-1.5 text-sm rounded-md font-medium transition-colors ${
|
||||
mode === 'magic' ? 'bg-brand-500 text-white' : 'text-gray-500 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
Magic Link
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="email"
|
||||
className="block text-sm font-medium text-gray-700 mb-1"
|
||||
>
|
||||
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
E-Mail-Adresse
|
||||
</label>
|
||||
<input
|
||||
@@ -91,10 +122,25 @@ export default function LoginPage() {
|
||||
/>
|
||||
</div>
|
||||
|
||||
{mode === 'password' && (
|
||||
<div>
|
||||
<label htmlFor="password" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
Passwort
|
||||
</label>
|
||||
<input
|
||||
id="password"
|
||||
type="password"
|
||||
required
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder="••••••••"
|
||||
className="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<p className="text-sm text-red-600 bg-red-50 px-4 py-2 rounded-lg">
|
||||
{error}
|
||||
</p>
|
||||
<p className="text-sm text-red-600 bg-red-50 px-4 py-2 rounded-lg">{error}</p>
|
||||
)}
|
||||
|
||||
<button
|
||||
@@ -102,13 +148,19 @@ export default function LoginPage() {
|
||||
disabled={loading}
|
||||
className="w-full bg-brand-500 text-white py-2.5 px-4 rounded-lg font-medium hover:bg-brand-600 disabled:opacity-60 disabled:cursor-not-allowed transition-colors"
|
||||
>
|
||||
{loading ? 'Wird gesendet...' : 'Magic Link senden'}
|
||||
{loading
|
||||
? 'Bitte warten...'
|
||||
: mode === 'password'
|
||||
? 'Anmelden'
|
||||
: 'Magic Link senden'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<p className="mt-4 text-center text-sm text-gray-500">
|
||||
Kein Passwort nötig — Sie erhalten einen Link per E-Mail.
|
||||
</p>
|
||||
{mode === 'password' && (
|
||||
<p className="mt-4 text-center text-xs text-gray-400">
|
||||
Demo: admin@demo.de / demo1234
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -7,13 +7,19 @@ import { sendMagicLinkEmail } from './email'
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: prismaAdapter(prisma, {
|
||||
provider: 'postgresql',
|
||||
provider: 'sqlite',
|
||||
}),
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
},
|
||||
secret: process.env.BETTER_AUTH_SECRET!,
|
||||
baseURL: process.env.BETTER_AUTH_URL!,
|
||||
trustedOrigins: [
|
||||
process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3000',
|
||||
process.env.EXPO_PUBLIC_API_URL ?? 'http://localhost:3000',
|
||||
process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3032',
|
||||
process.env.EXPO_PUBLIC_API_URL ?? 'http://localhost:3032',
|
||||
'http://10.36.148.233:3032',
|
||||
'http://localhost:8081', // Expo dev client
|
||||
'http://10.36.148.233:8081',
|
||||
],
|
||||
plugins: [
|
||||
magicLink({
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
|
||||
const PUBLIC_PATHS = ['/login', '/api/auth', '/api/trpc/stellen.listPublic']
|
||||
const PUBLIC_PATHS = ['/login', '/api/auth', '/api/trpc/stellen.listPublic', '/api/setup']
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
const pathname = request.nextUrl.pathname
|
||||
|
||||
5
innungsapp/apps/admin/next-env.d.ts
vendored
Normal file
5
innungsapp/apps/admin/next-env.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||
@@ -19,7 +19,7 @@
|
||||
"@trpc/server": "^11.0.0",
|
||||
"@tanstack/react-query": "^5.59.0",
|
||||
"better-auth": "^1.2.0",
|
||||
"next": "^15.0.0",
|
||||
"next": "15.3.4",
|
||||
"react": "^18.3.0",
|
||||
"react-dom": "^18.3.0",
|
||||
"zod": "^3.23.0",
|
||||
|
||||
@@ -124,6 +124,13 @@ export const membersRouter = router({
|
||||
where: { id: input.id, orgId: ctx.orgId },
|
||||
data: input.data,
|
||||
})
|
||||
// Keep user.name in sync when member name changes
|
||||
if (input.data.name) {
|
||||
const m = await ctx.prisma.member.findFirst({ where: { id: input.id }, select: { userId: true } })
|
||||
if (m?.userId) {
|
||||
await ctx.prisma.user.update({ where: { id: m.userId }, data: { name: input.data.name } })
|
||||
}
|
||||
}
|
||||
return member
|
||||
}),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user