Simple backend app with Actix-web, JWT and MongoDB
(JWT Token, Protect Route, Login & Register)
While developing the web service, I couldn't find good documentation or example (or up-to-date) to integrate jwt into my code. That's why I developed this example :)
- Create database and collection in mongodb.
- Replace
DATABASE_NAME
andUSER_COLLECTION_NAME
with your database settings inconfig.env
file. - If you want to change
SECRET_KEY
forJWT
, you can change inconfig.env
file.
DATABASE_NAME=YOUR_DATABASE_NAME
USER_COLLECTION_NAME=YOUR_USERS_COLLECTION_NAME
SECRET_KEY=Xqv8jTGLxT
curl -X POST -i 'http://127.0.0.1:8080/user/register' \
-H "Content-Type: application/json" \
--data '{
"name": "name",
"surname": "surname",
"email": "[email protected]",
"password": "password"
}'
{
"message": String,
"status": bool
}
curl -X POST -i 'http://127.0.0.1:8080/user/login' \
-H "Content-Type: application/json" \
--data '{
"email": "[email protected]",
"password": "password"
}'
{
"message": String,
"status": bool,
"token": String
}
curl -X GET -H 'Content-Type: application/json' \
-H 'Authorization: Bearer TOKEN' \
-i 'http://127.0.0.1:8080/user/userInformations'
{
"user_id": String,
"name": String,
"surname": String,
"phone": String,
"email": String,
"password": String,
"birth_date": String
}
curl -X GET -H 'Content-Type: application/json' \
-H 'Authorization: Bearer TOKEN' \
-i 'http://127.0.0.1:8080/user/protectedRoute'
bool