A demo repo for JWT authorization and simple CRUD with Actix-web@4
, sea-orm
, redis
and mysql
.
- Create a database in mysql, prepare an usable redis server.
- Prepare
.env
file in project's root directory, set your database url and redis url, see.env.example
. - Database migration, this will create tables in your database.
cargo run -p migration -- fresh
- Generate secret for encoding and decoding JWT.
head -c16 /dev/urandom > ./api/.secret
(You can also use the sample secret by rename.secret.example
to.secret
) - Run.
cargo run -p api
A postman workspace for quick test. https://www.postman.com/martian-robot-631196/workspace/actix-jwt-crud
Health checker
- Response:
200 OK
Hello, Actix
User register
- Request body
{
"name": "user1",
"password": "password"
}
- Response:
200 OK
1
User login
- Request body
{
"name": "user1",
"password": "password"
}
- Response:
200 OK
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJpYXQiOjE2ODA3NzQxNDYsImV4cCI6MTY4MTM3ODk0Nn0.ivj_KEJSbOictAa6OGMXBYMrejWfZJeZJijclGev-WQ",
"info": {
"name": "user1",
"count": 1
}
}
User logout
- Request Header
Authorization: bearer <token>
- Response:
204 No Content
Get user info
- Request Header
Authorization: bearer <token>
- Response:
200 OK
{
"name": "user1",
"count": 1
}
Add count of user by one.
- Request Header
Authorization: bearer <token>
- Response:
200 OK
{
"name": "user1",
"count": 2
}