The npm install command failed in my project. Analyze the following er
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
|
||||
"use client"
|
||||
|
||||
import * as React from "react";
|
||||
import { auth, signOut } from "@/auth"
|
||||
import Link from 'next/link';
|
||||
import {
|
||||
Sidebar,
|
||||
@@ -13,8 +11,6 @@ import {
|
||||
SidebarContent,
|
||||
SidebarInset,
|
||||
SidebarProvider,
|
||||
SidebarTrigger,
|
||||
useSidebar,
|
||||
} from "@/components/ui/sidebar"
|
||||
import {
|
||||
Home,
|
||||
@@ -29,14 +25,21 @@ import {
|
||||
Workflow
|
||||
} from "lucide-react"
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
||||
import { logout } from "@/lib/actions/auth";
|
||||
|
||||
function AdminLayout({
|
||||
async function AdminLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const session = await auth()
|
||||
|
||||
if (!session) {
|
||||
// This should be handled by middleware, but as a fallback
|
||||
const { redirect } = await import("next/navigation")
|
||||
redirect("/login")
|
||||
}
|
||||
|
||||
return (
|
||||
<SidebarProvider>
|
||||
<Sidebar>
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
import NextAuth from 'next-auth';
|
||||
import { authConfig } from '@/auth.config';
|
||||
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth(authConfig);
|
||||
|
||||
export const GET = handlers.GET;
|
||||
export const POST = handlers.POST;
|
||||
+28
-55
@@ -1,13 +1,10 @@
|
||||
|
||||
'use client';
|
||||
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import * as z from 'zod';
|
||||
import { useActionState } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { useToast } from '@/hooks/use-toast';
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
@@ -16,48 +13,23 @@ import {
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { login } from '@/lib/actions/auth';
|
||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
|
||||
import { AlertCircle } from 'lucide-react';
|
||||
|
||||
const loginSchema = z.object({
|
||||
email: z.string().email({ message: 'Invalid email address.' }),
|
||||
password: z.string().min(1, { message: 'Password is required.' }),
|
||||
});
|
||||
function SubmitButton() {
|
||||
// This component will be updated by useFormStatus in a real app,
|
||||
// but for now, we just show a static text.
|
||||
return (
|
||||
<Button type="submit" className="w-full">
|
||||
Sign In
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
type LoginFormValues = z.infer<typeof loginSchema>;
|
||||
|
||||
export default function LoginPage() {
|
||||
const router = useRouter();
|
||||
const { toast } = useToast();
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors, isSubmitting },
|
||||
} = useForm<LoginFormValues>({
|
||||
resolver: zodResolver(loginSchema),
|
||||
});
|
||||
|
||||
const onSubmit = async (data: LoginFormValues) => {
|
||||
try {
|
||||
const result = await login(data);
|
||||
|
||||
if (result.success) {
|
||||
toast({
|
||||
title: 'Login Successful',
|
||||
description: 'Redirecting to your dashboard...',
|
||||
});
|
||||
router.push('/admin');
|
||||
} else {
|
||||
throw new Error(result.message);
|
||||
}
|
||||
} catch (error: any) {
|
||||
toast({
|
||||
variant: 'destructive',
|
||||
title: 'Login Failed',
|
||||
description: error.message || 'An unexpected error occurred.',
|
||||
});
|
||||
}
|
||||
};
|
||||
const [state, formAction] = useActionState(login, undefined);
|
||||
|
||||
return (
|
||||
<main className="flex min-h-screen flex-col items-center justify-center bg-background p-8">
|
||||
@@ -71,36 +43,37 @@ export default function LoginPage() {
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
||||
<form action={formAction} className="space-y-4">
|
||||
{state?.message && (
|
||||
<Alert variant="destructive">
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
<AlertTitle>Error</AlertTitle>
|
||||
<AlertDescription>{state.message}</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="email">Email</Label>
|
||||
<Input
|
||||
id="email"
|
||||
name="email"
|
||||
type="email"
|
||||
placeholder="admin@example.com"
|
||||
{...register('email')}
|
||||
defaultValue="admin@example.com"
|
||||
required
|
||||
/>
|
||||
{errors.email && (
|
||||
<p className="text-sm text-destructive">{errors.email.message}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="password">Password</Label>
|
||||
<Input
|
||||
id="password"
|
||||
name="password"
|
||||
type="password"
|
||||
placeholder="password"
|
||||
{...register('password')}
|
||||
defaultValue="password"
|
||||
required
|
||||
/>
|
||||
{errors.password && (
|
||||
<p className="text-sm text-destructive">
|
||||
{errors.password.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<Button type="submit" className="w-full" disabled={isSubmitting}>
|
||||
{isSubmitting ? 'Signing In...' : 'Sign In'}
|
||||
</Button>
|
||||
<SubmitButton />
|
||||
</form>
|
||||
</CardContent>
|
||||
<CardFooter>
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
|
||||
import type { NextAuthConfig } from 'next-auth';
|
||||
import Credentials from 'next-auth/providers/credentials';
|
||||
import { z } from 'zod';
|
||||
import db from '@/lib/db';
|
||||
import { BetterSqlite3Adapter } from "next-auth/adapters"
|
||||
|
||||
export const authConfig = {
|
||||
pages: {
|
||||
signIn: '/login',
|
||||
},
|
||||
adapter: BetterSqlite3Adapter(db),
|
||||
session: {
|
||||
strategy: 'database',
|
||||
},
|
||||
providers: [
|
||||
Credentials({
|
||||
async authorize(credentials) {
|
||||
const parsedCredentials = z
|
||||
.object({ email: z.string().email(), password: z.string().min(1) })
|
||||
.safeParse(credentials);
|
||||
|
||||
if (parsedCredentials.success) {
|
||||
const { email, password } = parsedCredentials.data;
|
||||
|
||||
try {
|
||||
const userStmt = db.prepare('SELECT * FROM users WHERE email = ?');
|
||||
const user = userStmt.get(email) as any;
|
||||
|
||||
if (!user) return null;
|
||||
|
||||
// WARNING: Storing passwords in plaintext is insecure.
|
||||
// This is for demonstration purposes only.
|
||||
// In a real application, you MUST hash and salt passwords.
|
||||
const passwordsMatch = password === user.password;
|
||||
|
||||
if (passwordsMatch) return user;
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
authorized({ auth, request: { nextUrl } }) {
|
||||
const isLoggedIn = !!auth?.user;
|
||||
const isOnAdmin = nextUrl.pathname.startsWith('/admin');
|
||||
|
||||
if (isOnAdmin) {
|
||||
return isLoggedIn;
|
||||
} else if (isLoggedIn) {
|
||||
// Redirect logged-in users from the login page to the admin dashboard
|
||||
if (nextUrl.pathname === '/login') {
|
||||
return Response.redirect(new URL('/admin', nextUrl));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
},
|
||||
} satisfies NextAuthConfig;
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
import NextAuth from 'next-auth';
|
||||
import { authConfig } from './auth.config';
|
||||
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth(authConfig);
|
||||
+16
-32
@@ -1,44 +1,28 @@
|
||||
|
||||
'use server';
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
import { z } from 'zod';
|
||||
import db from '@/lib/db';
|
||||
|
||||
const loginSchema = z.object({
|
||||
email: z.string().email(),
|
||||
password: z.string(),
|
||||
});
|
||||
|
||||
export async function login(data: z.infer<typeof loginSchema>): Promise<{ success: boolean, message: string }> {
|
||||
const validatedFields = loginSchema.safeParse(data);
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return { success: false, message: 'Invalid fields.' };
|
||||
}
|
||||
|
||||
const { email, password } = validatedFields.data;
|
||||
import { signIn, signOut } from '@/auth';
|
||||
|
||||
export async function login(
|
||||
prevState: { message: string } | undefined,
|
||||
formData: FormData
|
||||
) {
|
||||
try {
|
||||
const stmt = db.prepare('SELECT * FROM users WHERE email = ? AND password = ?');
|
||||
const user = stmt.get(email, password);
|
||||
|
||||
if (user) {
|
||||
// In a real app, you would set a session cookie here.
|
||||
// For this simulated login, we'll just return success.
|
||||
return { success: true, message: 'Login successful.' };
|
||||
} else {
|
||||
return { success: false, message: 'Invalid email or password.' };
|
||||
await signIn('credentials', formData);
|
||||
} catch (error: any) {
|
||||
if (error) {
|
||||
switch (error.type) {
|
||||
case 'CredentialsSignin':
|
||||
return { message: 'Invalid credentials.' };
|
||||
default:
|
||||
return { message: 'Something went wrong.' };
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Login error:', error);
|
||||
return { success: false, message: 'An internal error occurred.' };
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function logout() {
|
||||
// In a real app with authentication, this would handle signing out the user.
|
||||
// For now, it redirects to the login page to simulate logging out.
|
||||
redirect('/login');
|
||||
await signOut({ redirectTo: '/login' });
|
||||
}
|
||||
|
||||
+17
-15
@@ -4,6 +4,7 @@
|
||||
import { z } from 'zod';
|
||||
import db from '@/lib/db';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { auth } from '@/auth';
|
||||
|
||||
const formSchema = z.object({
|
||||
name: z.string().min(1, 'Name is required'),
|
||||
@@ -14,28 +15,29 @@ const formSchema = z.object({
|
||||
type UserFormValues = z.infer<typeof formSchema>;
|
||||
|
||||
/**
|
||||
* Gets the user from the database.
|
||||
* Since authentication isn't fully implemented, it defaults to the user with id 1.
|
||||
* Gets the currently logged-in user from the session.
|
||||
*/
|
||||
export async function getUser(): Promise<{ id: number; name: string; email: string } | null> {
|
||||
try {
|
||||
const stmt = db.prepare('SELECT id, name, email FROM users WHERE id = ?');
|
||||
// For now, we'll hardcode the user ID to 1 as login is simulated.
|
||||
const user = stmt.get(1) as { id: number; name: string; email: string } | undefined;
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
return user;
|
||||
} catch (error) {
|
||||
console.error('Failed to get user:', error);
|
||||
export async function getUser(): Promise<{ id: string; name: string; email: string } | null> {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id || !session.user.email || !session.user.name) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
id: session.user.id,
|
||||
email: session.user.email,
|
||||
name: session.user.name,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a user's profile information in the database.
|
||||
*/
|
||||
export async function updateUser(data: UserFormValues): Promise<{ success: boolean; message: string }> {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return { success: false, message: 'Not authenticated.' };
|
||||
}
|
||||
|
||||
const validation = formSchema.safeParse(data);
|
||||
if (!validation.success) {
|
||||
return { success: false, message: 'Invalid data provided.' };
|
||||
@@ -44,8 +46,7 @@ export async function updateUser(data: UserFormValues): Promise<{ success: boole
|
||||
const { name, email, password } = validation.data;
|
||||
|
||||
try {
|
||||
// For now, we'll assume we're updating the user with ID 1.
|
||||
const userId = 1;
|
||||
const userId = session.user.id;
|
||||
|
||||
// Check if the new email is already taken by another user
|
||||
const checkEmailStmt = db.prepare('SELECT id FROM users WHERE email = ? AND id != ?');
|
||||
@@ -58,6 +59,7 @@ export async function updateUser(data: UserFormValues): Promise<{ success: boole
|
||||
if (password) {
|
||||
// If a new password is provided, update it along with name and email
|
||||
const stmt = db.prepare('UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?');
|
||||
// In a real app, hash the password! For this example, we store it as plain text.
|
||||
stmt.run(name, email, password, userId);
|
||||
} else {
|
||||
// If no new password, only update name and email
|
||||
|
||||
+49
-7
@@ -3,17 +3,58 @@ import Database from 'better-sqlite3';
|
||||
|
||||
// Use a file-based database in development
|
||||
const db = new Database('local.db');
|
||||
db.pragma('journal_mode = WAL');
|
||||
|
||||
// --- SCHEMA CREATION ---
|
||||
// Auth.js tables
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
name TEXT NOT NULL
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT,
|
||||
email TEXT UNIQUE,
|
||||
emailVerified INTEGER,
|
||||
image TEXT,
|
||||
password TEXT
|
||||
)
|
||||
`);
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
userId TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
provider TEXT NOT NULL,
|
||||
providerAccountId TEXT NOT NULL,
|
||||
refresh_token TEXT,
|
||||
access_token TEXT,
|
||||
expires_at INTEGER,
|
||||
token_type TEXT,
|
||||
scope TEXT,
|
||||
id_token TEXT,
|
||||
session_state TEXT,
|
||||
PRIMARY KEY (provider, providerAccountId),
|
||||
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE
|
||||
)
|
||||
`);
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
sessionToken TEXT NOT NULL PRIMARY KEY,
|
||||
userId TEXT NOT NULL,
|
||||
expires INTEGER NOT NULL,
|
||||
FOREIGN KEY (userId) REFERENCES users (id) ON DELETE CASCADE
|
||||
)
|
||||
`);
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS verification_tokens (
|
||||
identifier TEXT NOT NULL,
|
||||
token TEXT NOT NULL,
|
||||
expires INTEGER NOT NULL,
|
||||
PRIMARY KEY (identifier, token)
|
||||
)
|
||||
`);
|
||||
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
@@ -54,13 +95,14 @@ db.exec(`
|
||||
console.log('Running database checks and seeding if necessary...');
|
||||
|
||||
// Seed default user
|
||||
const userStmt = db.prepare('SELECT id FROM users WHERE id = ?');
|
||||
const defaultUser = userStmt.get(1);
|
||||
const userStmt = db.prepare('SELECT id FROM users WHERE email = ?');
|
||||
const defaultUser = userStmt.get('admin@example.com');
|
||||
if (!defaultUser) {
|
||||
const insertUser = db.prepare(
|
||||
"INSERT INTO users (id, email, password, name) VALUES (?, ?, ?, ?)"
|
||||
);
|
||||
insertUser.run(1, 'admin@example.com', 'password', 'Admin User');
|
||||
// Note: In a real app, hash the password!
|
||||
insertUser.run('cl-admin-user-id', 'admin@example.com', 'password', 'Admin User');
|
||||
console.log('Default user created.');
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
import NextAuth from 'next-auth';
|
||||
import { authConfig } from './auth.config';
|
||||
|
||||
export default NextAuth(authConfig).auth;
|
||||
|
||||
export const config = {
|
||||
// https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
|
||||
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
|
||||
};
|
||||
Reference in New Issue
Block a user