- TypeScript
- Prisma
- JWT
- BCrypt
- Express
You can install all dependencies by using npm install
or yarn
, you will also need tsx in order to run the without using ts-node and ts-node-dev, unlike ts-node and ts-node-dev, tsx is powered by esbuild, making it really fast.
In order to install tsx globally, run:
npm install tsx --global
or yarn global add tsx
then after installing tsx (and setting up prisma) you can simply run:
npm run dev
or yarn dev
But if you don't want install it, you can run by using:
npx tsx watch ./src/index.ts
this way it will run and automatically restart when a file changes.
This secret should be a long random string that will be used to encryt and decrypt our data. To generate one we can use the node library called crypto
.To do this, you can run node on your terminal by using:
node
You should see something similar to this:
Welcome to Node.js v16.17.1.
Type ".help" for more information.
>
Now insert
require('crypto').randomBytes(64).toString('hex')
and hit enter.
it should result on something similar to this:
> require('crypto').randomBytes(64).toString('hex')
'461a4ba8e54ba021c25588d547844deeea8efa5ac8482228675fa8d4ba6bff1979d6266666172a28acf3b6eafa3c54826dcbfbbcae958908ceff34872dc8603c'
Copy this string and paste into TOKEN_SECRET
inside the .env
(rename the .env.example to .env).
In order to init our Prisma Client we can run the following command:
npx prisma init --datasource-provider sqlite
, to generate our prisma client we need to run npx prisma generate
and
after it, a migration in order to create our tables
npx prisma migrate dev --name init
and there you go, now you can crank it up!
GET
/users/@me
- Get your information (Protected)GET
/users/:id
- Get a specific user information (Protected)POST
/auth/login
- Used to generate a JWT tokenPOST
/auth/register
- Used to create a new user
(Status code 200)
{
"id": 1,
"email": "[email protected]",
"created_at": "2023-02-02T15:40:06.891Z",
"username": "Dummy"
}
(Status code 200)
{
"id": 1,
"created_at": "2023-02-02T15:40:06.891Z",
"username": "Dummy"
}
{
"email": "[email protected]",
"password": "youshallnotpass"
}
(Status code 200)
{
"username": "Dummy",
"email": "[email protected]",
"token": "eyJhbGciOiJIU..."
}
(Status code 401)
{
"message": "Wrong password."
}
(Status code 500)
{
"message": "Unable to sign a JWT",
"error": {}
}
{
"username": "Dummy",
"email": "[email protected]",
"password": "youshallnotpass"
}
(Status code 200)
{
"username": "Dummy",
"token": "eyJhbGciOiJIU..."
}
(Status code 500)
{
"message": "Unable to sign a JWT",
"error": {}
}