pour créer les containers avec la base de donnée PosgreSQL et PGAdmin
***********************************
_Explication rapide en français :_
npm install
Une fois le repo cloné lancer les commandes suivantes :
npx prisma migrate dev
va hydrater la db et jouer le fichier de seed
**docker compose up**
pour créer les containers avec la base de donnée PosgreSQL et pgAdmin
npx prisma db
**npm install**
va installer les dépendances
**npx prisma migrate dev**
va hydrater la db et jouer le fichier de seed
(important)
**npx prisma db** (pour reset les data de la db)
va reset la db et re-executer le fichier de seed
npm run dev
va lancer l'application sur le localhost:3000
**npm run dev**
va lancer l'application sur le _localhost:3000_
npx prisma studio
pour naviguer dans la db via le localhost:5555
**npx prisma studio**
pour naviguer dans la db via le _localhost:5555_ sans utiliser pgAdmin
***********************************
http://localhost:3000/api/superadmin?pass=1234
pour upgrade votre compte en superadmin (vous devez être connecté)
http://localhost:3000/api/gain
pour générer un gain (vous devez être connecté en tant que superadmin ou admin)
This example shows how to implement a **fullstack app in TypeScript with [Next.js](https://nextjs.org/)** using [React](https://reactjs.org/) (frontend), [Next.js API routes](https://nextjs.org/docs/api-routes/introduction) and [Prisma Client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client) (backend). It also demonstrates how to implement authentication using [NextAuth.js](https://next-auth.js.org/). The example uses an SQLite database file with some initial dummy data which you can find at [`./prisma/dev.db`](./prisma/dev.db).
***********************************
Note that the app uses a mix of server-side rendering with `getServerSideProps` (SSR) and static site generation with `getStaticProps` (SSG). When possible, SSG is used to make database queries already at build-time (e.g. when fetching the [public feed](./pages/index.tsx)). Sometimes, the user requesting data needs to be authenticated, so SSR is being used to render data dynamically on the server-side (e.g. when viewing a user's [drafts](./pages/drafts.tsx)).
## Getting started
This project use TypeScript with [Next.js](https://nextjs.org/)** using [React](https://reactjs.org/) (frontend), [Next.js API routes](https://nextjs.org/docs/api-routes/introduction) and [Prisma Client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client) (backend). It implement authentication using [NextAuth.js](https://next-auth.js.org/). The app uses an PostgreSQL database file on docker's container.
### 1. Download example and install dependencies
Note that the app uses a mix of server-side rendering with `getServerSideProps` (SSR) and static site generation with `getStaticProps` (SSG). When possible, SSG is used to make database queries already at build-time . Sometimes, the user requesting data needs to be authenticated, so SSR is being used to render data dynamically on the server-side.
Download this example:
## Getting started
```
curl https://codeload.github.com/prisma/prisma-examples/tar.gz/latest | tar -xz --strip=2 prisma-examples-latest/typescript/rest-nextjs-api-routes-auth
```
Install npm dependencies:
```
cd rest-nextjs-api-routes-auth
npm install
```
<details><summary><strong>Alternative:</strong> Clone the entire repo</summary>
Run the following command to create your PostgreSQL docker's container
```
cd prisma-examples/typescript/rest-nextjs-api-routes-auth
npm install
docker compose up
```
</details>
### 2. Create and seed the database
Run the following command to create your SQLite database file. This also creates the `User` and `Post` tables that are defined in [`prisma/schema.prisma`](./prisma/schema.prisma):
```
npx prisma migrate dev --name init
npx prisma migrate dev
```
When `npx prisma migrate dev` is executed against a newly created database, seeding is also triggered. The seed file in [`prisma/seed.ts`](./prisma/seed.ts) will be executed and your database will be populated with the sample data.
### 3. Configuring your authentication provider
In order to get this example to work, you need to configure the [GitHub](https://next-auth.js.org/providers/github) and/or [Email](https://next-auth.js.org/providers/email) authentication providers from NextAuth.js.
#### Configuring the GitHub authentication provider
<details><summary>Expand to learn how you can configure the GitHub authentication provider</summary>
First, log into your [GitHub](https://github.com/) account.
Then, navigate to [**Settings**](https://github.com/settings/profile), then open to [**Developer Settings**](https://github.com/settings/apps), then switch to [**OAuth Apps**](https://github.com/settings/developers).
Clicking on the **Register a new application** button will redirect you to a registration form to fill out some information for your app. The **Authorization callback URL** should be the Next.js `/api/auth` route.
An important thing to note here is that the **Authorization callback URL** field only supports a single URL, unlike e.g. Auth0, which allows you to add additional callback URLs separated with a comma. This means if you want to deploy your app later with a production URL, you will need to set up a new GitHub OAuth app.
Click on the **Register application** button, and then you will be able to find your newly generated **Client ID** and **Client Secret**. Copy and paste this info into the [`.env`](./env) file in the root directory.
The resulting section in the `.env` file might look like this:
If you want to reset the database data run the following command
#### Configuring the Email authentication provider
You can [follow the instructions in the NextAuth.js documentation](https://next-auth.js.org/providers/email#configuration) to configure the Email authentication provider. Once your email authentication provider is configured, you can set the environment variables in [`.env`](./env) accordingly.
### 4. Start the app
@ -113,192 +76,3 @@ npm run dev
The app is now running, navigate to [`http://localhost:3000/`](http://localhost:3000/) in your browser to explore its UI.
## Evolving the app
Evolving the application typically requires three steps:
1. Migrate your database using Prisma Migrate
1. Update your server-side application code
1. Build new UI features in React
For the following example scenario, assume you want to add a "profile" feature to the app where users can create a profile and write a short bio about themselves.
### 1. Migrate your database using Prisma Migrate
The first step is to add a new table, e.g. called `Profile`, to the database. You can do this by adding a new model to your [Prisma schema file](./prisma/schema.prisma) file and then running a migration afterwards:
Once you have added a new endpoint to the API (e.g. `/api/profile` with `/POST`, `/PUT` and `GET` operations), you can start building a new UI component in React. It could e.g. be called `profile.tsx` and would be located in the `pages` directory.
In the application code, you can access the new endpoint via `fetch` operations and populate the UI with the data you receive from the API calls.
## Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB)
If you want to try this example with another database than SQLite, you can adjust the the database connection in [`prisma/schema.prisma`](./prisma/schema.prisma) by reconfiguring the `datasource` block.
Learn more about the different connection configurations in the [docs](https://www.prisma.io/docs/reference/database-reference/connection-urls).
<details><summary>Expand for an overview of example configurations with different databases</summary>
### PostgreSQL
For PostgreSQL, the connection URL has the following structure:
Because MongoDB is currently in [Preview](https://www.prisma.io/docs/about/releases#preview), you need to specify the `previewFeatures` on your `generator` block:
```
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongodb"]
}
```
</details>
## Next steps
- Check out the [Prisma docs](https://www.prisma.io/docs)
- Share your feedback in the [`prisma2`](https://prisma.slack.com/messages/CKQTGR6T0/) channel on the [Prisma Slack](https://slack.prisma.io/)
- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/)
- Watch our biweekly "What's new in Prisma" livestreams on [Youtube](https://www.youtube.com/channel/UCptAHlN1gdwD89tFM3ENb6w)