Codes | Meaning |
---|---|
"[!]" | Error |
"[+]" | Warning |
"[*]" | Information |
"[#]" | Process Started |
Here, I will explain how the API works for safe usage. The API is well-designed because it is based on the Route File system (yes, I made up this term, why not :-) ). To explain this simply, when you create a directory in the ./api/
folder, it is transformed into an API route.
As I mentioned earlier, you need to create a directory in the ./api/
folder. At the end create the file route.js
.
> NOTE: When you create routes, you can also create subfolders, and it will work as well (as long as there is a folder inside xD). You can access it here: example:
./src/api/your/subfolder/route.js
>http://localhost:XXXX/api/your/subfolder
You need to create all routes in the/api/
folder.
To start, you've created file(s)—good! Now, how do you add logic to the API route? Simple!
// Import express and declare router
const express = require("express");
const router = express.Router();
// Define route with router
router.get("/", async (req, res) => {
/* You're logic here */
});
module.exports = router;
> NOTE: The parameters req and res are necessary. "req" represents the request we send, and "res" is what we receive in response (just like any other JavaScript request 😄).
> Make sure to use "/" always. Otherwise, the route will be detected in the console but will be unusable.
If you want to protect a few routes, follow these steps:
- Import middleware
- Use Express Router
- Add the middleware
- Export the router
Here's the syntax:
const { checkUserConnection } = require("./path/to/checkUserConnection");
// Past the middleware function (! without "()" !)
router.get("/", checkUserConnection, async (req, res) => {
/* ... */
});
module.exports = router;
Afterwards, check out the official documentation of the EXPRESS module for more details.
The middleware is really well implemented.
- For any request where you use the middleware, you need to pass an authorization header with the format:
"Bearer <YOUR SECRET TOKEN>"
. You won't be able to access the route if you don't have a valid token. - To get the token, it's simple: you just need to log in with your
username
andpassword
, which you previously registered in the database.
To start, you need a .env
file at the root of your project clone. You need to add these values in your .env
file:
TOKEN=<YOUR DISCORD BOT TOKEN>
PORT=<YOUR API LISTENING PORT>
DATABASE_URL="<YOUR DATABASE LINK>"
JWT_TIMING=<YOUR JWT TIMING, e.g., 1h>
Next, execute npx prisma init
and copy paste the same schema.prisma
structure as below:
// This is your Prisma schema file.
// Learn more about it in the docs: https://pris.ly/d/prisma-schema
// Looking for ways to speed up your queries or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
// ----------------------------- Database Config ---------------------------- //
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-3.0.x"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// ---------------------------- Database Content ---------------------------- //
enum Group {
USER
AUTHUSER
NOTAUTHUSER
}
model APIUser {
id Int @id @default(autoincrement())
password String
email String?
discordTag String @unique
username String @unique
group Group
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([email])
}
model AnonymousUserVisit {
id Int @id @default(autoincrement())
EcoleDirectePlusUserId Int @unique
}
> NOTE: After migrating with
npx prisma migrate dev
for create and init tables, remember to generate your Prisma client withnpx prisma generate
Once you have all the files set up as above, you need to generate your private and public keys. Here's how to proceed:
-
Open a new Terminal window in your
.certs
folder. -
Use your preferred tool (SSH-KeyGen or OpenSSL).
-
To generate keys, you can use either OpenSSL or ssh-keygen:
- Generate the private key:
openssl genrsa -out private.pem 4096
- Generate the public key from the private key:
openssl rsa -in private.pem -pubout -out public.pem
Note: Make sure to name them
private.pem
andpublic.pem
and use the PEM format.
- Generate the private key:
ssh-keygen -t rsa -b 4096 -m PEM -f private.pem
- Generate the public key:
ssh-keygen -f private.pem -e -m PEM > public.pem
Note: Make sure the keys are saved as
private.pem
andpublic.pem
.
- Generate the private key:
Now you are able to lunch API and log in