v0.1.0 — Moon Base initial scaffold: auth, garden plant registry + log
This commit is contained in:
110
src/lib/auth.ts
Normal file
110
src/lib/auth.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { NextAuthOptions, getServerSession } from "next-auth";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { compare } from "bcryptjs";
|
||||
import { db } from "@/lib/db";
|
||||
import { UserRole } from "@prisma/client";
|
||||
|
||||
declare module "next-auth" {
|
||||
interface Session {
|
||||
user: {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string;
|
||||
role: UserRole;
|
||||
mustChangePassword: boolean;
|
||||
};
|
||||
}
|
||||
interface User {
|
||||
id: string;
|
||||
role: UserRole;
|
||||
mustChangePassword: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "next-auth/jwt" {
|
||||
interface JWT {
|
||||
id: string;
|
||||
role: UserRole;
|
||||
mustChangePassword: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
export const authOptions: NextAuthOptions = {
|
||||
session: { strategy: "jwt" },
|
||||
pages: { signIn: "/login", error: "/login" },
|
||||
providers: [
|
||||
CredentialsProvider({
|
||||
name: "credentials",
|
||||
credentials: {
|
||||
email: { label: "Username or email", type: "text" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
async authorize(credentials) {
|
||||
if (!credentials?.email || !credentials?.password) return null;
|
||||
const id = credentials.email.trim();
|
||||
const user = await db.user.findFirst({
|
||||
where: {
|
||||
active: true,
|
||||
OR: [
|
||||
{ email: id.toLowerCase() },
|
||||
{ username: { equals: id, mode: "insensitive" } },
|
||||
],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
passwordHash: true,
|
||||
role: true,
|
||||
mustChangePassword: true,
|
||||
},
|
||||
});
|
||||
if (!user) return null;
|
||||
const ok = await compare(credentials.password, user.passwordHash);
|
||||
if (!ok) return null;
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
role: user.role,
|
||||
mustChangePassword: user.mustChangePassword,
|
||||
};
|
||||
},
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user, trigger }) {
|
||||
if (user) {
|
||||
token.id = user.id;
|
||||
token.role = user.role;
|
||||
token.mustChangePassword = user.mustChangePassword;
|
||||
}
|
||||
if (trigger === "update") {
|
||||
const fresh = await db.user.findUnique({
|
||||
where: { id: token.id },
|
||||
select: { mustChangePassword: true },
|
||||
});
|
||||
if (fresh) token.mustChangePassword = fresh.mustChangePassword;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
async session({ session, token }) {
|
||||
session.user.id = token.id;
|
||||
session.user.role = token.role;
|
||||
session.user.mustChangePassword = token.mustChangePassword;
|
||||
return session;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const getSession = () => getServerSession(authOptions);
|
||||
|
||||
export async function requireAuth() {
|
||||
const session = await getSession();
|
||||
if (!session) throw new Error("Unauthorized");
|
||||
return session;
|
||||
}
|
||||
|
||||
export function isAdmin(role: UserRole): boolean {
|
||||
return role === "ADMIN";
|
||||
}
|
||||
18
src/lib/changelog.ts
Normal file
18
src/lib/changelog.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
// Append new entries to the TOP. Match APP_VERSION in version.ts.
|
||||
// Write for Bonna, not engineers — plain English, what changed and why.
|
||||
|
||||
export type ChangelogEntry = {
|
||||
version: string;
|
||||
date: string; // ISO yyyy-mm-dd
|
||||
changes: string[];
|
||||
};
|
||||
|
||||
export const CHANGELOG: ChangelogEntry[] = [
|
||||
{
|
||||
version: "0.1.0",
|
||||
date: "2026-06-15",
|
||||
changes: [
|
||||
"Moon Base is live! Garden module: add plants to your food forest, record where they are on your property, and keep a log of care, harvests, and observations.",
|
||||
],
|
||||
},
|
||||
];
|
||||
14
src/lib/db.ts
Normal file
14
src/lib/db.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var __db: PrismaClient | undefined;
|
||||
}
|
||||
|
||||
export const db =
|
||||
global.__db ??
|
||||
new PrismaClient({
|
||||
log: process.env.NODE_ENV === "development" ? ["error", "warn"] : ["error"],
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== "production") global.__db = db;
|
||||
49
src/lib/garden/categories.ts
Normal file
49
src/lib/garden/categories.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { PlantCategory, PlantLogType } from "@prisma/client";
|
||||
|
||||
export const CATEGORY_LABELS: Record<PlantCategory, string> = {
|
||||
CANOPY_TREE: "Canopy tree",
|
||||
UNDERSTORY_TREE: "Understory tree",
|
||||
LARGE_SHRUB: "Large shrub",
|
||||
SHRUB: "Shrub",
|
||||
HERB: "Herb",
|
||||
GROUNDCOVER: "Groundcover",
|
||||
VINE: "Vine",
|
||||
ANNUAL: "Annual",
|
||||
PERENNIAL: "Perennial",
|
||||
BULB: "Bulb",
|
||||
MUSHROOM: "Mushroom",
|
||||
OTHER: "Other",
|
||||
};
|
||||
|
||||
export const CATEGORY_ORDER: PlantCategory[] = [
|
||||
"CANOPY_TREE",
|
||||
"UNDERSTORY_TREE",
|
||||
"LARGE_SHRUB",
|
||||
"SHRUB",
|
||||
"HERB",
|
||||
"GROUNDCOVER",
|
||||
"VINE",
|
||||
"PERENNIAL",
|
||||
"ANNUAL",
|
||||
"BULB",
|
||||
"MUSHROOM",
|
||||
"OTHER",
|
||||
];
|
||||
|
||||
export const LOG_TYPE_LABELS: Record<PlantLogType, string> = {
|
||||
OBSERVATION: "Observation",
|
||||
CARE: "Care",
|
||||
HARVEST: "Harvest",
|
||||
TREATMENT: "Treatment",
|
||||
TRANSPLANT: "Transplanted",
|
||||
DIED: "Died / removed",
|
||||
};
|
||||
|
||||
export const LOG_TYPE_COLORS: Record<PlantLogType, string> = {
|
||||
OBSERVATION: "text-blue-600 dark:text-blue-400",
|
||||
CARE: "text-[hsl(var(--leaf))]",
|
||||
HARVEST: "text-amber-600 dark:text-amber-400",
|
||||
TREATMENT: "text-orange-600 dark:text-orange-400",
|
||||
TRANSPLANT: "text-purple-600 dark:text-purple-400",
|
||||
DIED: "text-destructive",
|
||||
};
|
||||
22
src/lib/utils.ts
Normal file
22
src/lib/utils.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { type ClassValue, clsx } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export function initials(name: string): string {
|
||||
return name
|
||||
.split(/\s+/)
|
||||
.map((p) => p[0])
|
||||
.filter(Boolean)
|
||||
.slice(0, 2)
|
||||
.join("")
|
||||
.toUpperCase();
|
||||
}
|
||||
|
||||
export function formatDate(date: Date | string | null | undefined): string {
|
||||
if (!date) return "—";
|
||||
const d = typeof date === "string" ? new Date(date) : date;
|
||||
return d.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" });
|
||||
}
|
||||
2
src/lib/version.ts
Normal file
2
src/lib/version.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
// Bump on every user-visible release. Changelog entries live in `src/lib/changelog.ts`.
|
||||
export const APP_VERSION = "0.1.0";
|
||||
Reference in New Issue
Block a user