This application uses the standard NestJS template with Prisma and Passport.
npm install
npx prisma migrate deploy
npm run start:dev
You need to rename the .env.example
file to .env
and write any non-empty values for ACCESS_JWT_SECRET
and REFRESH_JWT_SECRET
.
The ACCESS_JWT_EXPIRES_IN
and REFRESH_JWT_EXPIRES_IN
values are used to set the lifetime of tokens, and to set the Max-Age
parameter of the cookies being sent.
/tokens
GET /tokens
- Get new tokens pair
- Require
Authorization
header withBearer {token}
value. See above. - Returns nothing with
Set-Cookie
headers:AccessToken
&RefreshToken
PATCH /tokens
- Refresh tokens
- Require
Cookie
header withRefreshToken={jwt}
- Returns nothing with
Set-Cookie
headers:AccessToken
&RefreshToken
DELETE /tokens
- Delete hash of
RefreshToken
from the database - Requires
Cookie
header withAccessToken={jwt}
- Returns nothing with
Set-Cookie
headers: emptyAccessToken
&RefreshToken
withMax-Age=0
- Delete hash of
/users
GET /users
- Get full list of users
- Require
Cookie
header withAccessToken={jwt}
- Returns array of objects with limited user data:
[ { id: string, login: string, }, { ... } ]
GET /users/{userId}
- Get current user data
- Require
Cookie
header withAccessToken={jwt}
- Limited by current
userId
, extracted fromAccessToken
. You can't read data of another user. - Returns object with full user data:
{ id: string; login: string; passwordHash: string; refreshTokenHash: string; createdAt: string; updatedAt: string; }
DELETE /users/{userId}
- Delete current user
- Require
Cookie
header withAccessToken={jwt}
- Limited by current
userId
, extracted fromAccessToken
. You can't delete another user. - Returns nothing
In the root folder of the project there is a REST_API_Clients
folder with files for importing into popular REST API clients: Insomnia, Bruno and general OpenAPI specification.
To get Bearer token we need to take an object with authorization data:
const loginData = {
login: user-login,
password: user-password
}
Process all fields using encodeURIComponent
:
loginData.login = encodeURIComponent(loginData.login);
loginData.password = encodeURIComponent(loginData.password);
Convert an object to a string using JSON.stringify
:
const jsonString = JSON.stringify(loginData);
Convert the received string to BASE64 format:
const b64String = btoa(jsonString);
The result should be used as a Bearer token.