This code shows how to implement SpryJs in order to create a Web API with JWT authentication and MongoDB database implementation.
This example has been created with educational purposes. This example might lack of security configuration or other production-related configuration.
Clone this repository and install all the dependencies. Run npm i
in a console.
This code uses dotenv for environment variables. First thing to do, is to create a .env
file in the root with the following
PORT=
TOKEN_SECRET=
SALT=
MONGO_CS=
Values are plain text (no quotes)
PORT Port that will be used to create the server
TOKEN_SECRET Secret string for token generation. Use a random string
SALT Secret string for password generation. Use a random string
MONGO_CS MongoDB Connection string. You can create a free cluster in MongoDB
Once the above is configured, run npm run dev
command and the server should start listening on the given port and will create the following endpoints.
(automatically generated at the moment of calling spryjs.useAuthentication()
)
-
- POST (Register) -
username
andpassword
must be in the payload - GET
- POST (Register) -
-
- POST -
username
andpassword
must be in the payload
- POST -
-
- POST -
password
andnewPassword
must be in the payload
- POST -
-
- DELETE
- GETBYID
(Generated via spryjs.registerEntity()
)
Some of these endpoints are private. We must send the Bearer <token>
in a authorization
header.
-
- POST (protected, need to send JWT)
- GET
-
- DELETE (protected, need to send JWT)
- PUT (protected, need to send JWT)
- GETBYID