Educational project to learn how to integrate a database and authentication in Next.
|
|
|
|
- Create a
Prisma ORMschema and connect to theVercel PostgreSQLDatabase. - Add various SQL relations:
one-to-one,one-to-many,many-to-many. - Query the database via
Prisma ORMand "populate" relations during querying. - Integrate a simple pagination mechanism to read the database.
- Create client and server components.
- Handle form submit in Next.js via:
useFormState()+useFormStatus()to control the "pending-status" and "response-messages" on the client-side. - Use Next.js
Server Actionsfor CRUD operations. Read and write data from the database viaPrisma ORMon the server-side. - Validate form data against a Zod schema.
- Authenticate a user via GitHub OAuth procedure.
- Lock certain features based on the session/login status.
- TypeScript
- Next.js
- Next-Auth (OAuth with Auth.js)
- Vercel PostgreSQL Database
- Prisma ORM
-
Create a Auth secret:
npx auth secret. -
Create and register a new OAuth app on GitHub: "New OAuth App"
-
Add to the
.envfile:AUTH_SECRET="auto generated in step 1" AUTH_GITHUB_ID="optained in step 2" AUTH_GITHUB_SECRET="optained in step 2"
There seem to be issues with yarn PnP and the prisma client. Here is my workaround:
- Generate the client manually in a specif direction:
generator client { output = "./client" } - Use yarn pnpfify to generate the prisma client:
yarn pnpify prisma generate - Use the this client in the project instead of the one installed via package.json:
import { PrismaClient } from "../prisma/client";
yarn add @prisma/client
yarn add -D prisma @yarnpkg/pnpifyyarn prisma init --datasource-provider PostgreSQLcredentials can be optained from: vercel.com > login > user dashboard > Storage > your database > ".env.local" tab > Copy Snippet.
touch .envPOSTGRES_PRISMA_URL="postgres://default:xxxxxxxxx.aws.neon.tech:5432/verceldb?xxxxxx"
POSTGRES_URL_NON_POOLING="postgres://default:xxxxxxxxx.aws.neon.tech:5432/verceldb?xxxxxx"add this line: output = "./client" in the generator client {} object.
generator client {
provider = "prisma-client-js"
output = "./client" // <== this line, here
}
datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL")
directUrl = env("POSTGRES_URL_NON_POOLING")
}
model ... {
....
}yarn prisma migrate dev --name init --skip-generate
yarn pnpify prisma generate// root/prisma/db.ts
import { PrismaClient } from './client';
const prisma = new PrismaClient();
export default prisma;