{"id":15138205,"url":"https://github.com/codecraft26/rust-jwt-auth-mogodb","last_synced_at":"2026-01-19T07:32:47.228Z","repository":{"id":213397704,"uuid":"734038643","full_name":"codecraft26/rust-jwt-auth-mogodb","owner":"codecraft26","description":"In this repository, you can find the backend Rust rocket mongodb rest-api boilerplate with token authorization.","archived":false,"fork":false,"pushed_at":"2023-12-21T09:12:33.000Z","size":50,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T12:21:55.048Z","etag":null,"topics":["backend","mongodb","rocket","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codecraft26.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-20T17:59:47.000Z","updated_at":"2023-12-21T09:15:49.000Z","dependencies_parsed_at":"2024-09-21T12:00:33.613Z","dependency_job_id":"c463e8ee-789d-418e-aac1-68e42f8ecea7","html_url":"https://github.com/codecraft26/rust-jwt-auth-mogodb","commit_stats":null,"previous_names":["codecraft26/rust-jwt-auth-mogodb"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecraft26%2Frust-jwt-auth-mogodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecraft26%2Frust-jwt-auth-mogodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecraft26%2Frust-jwt-auth-mogodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecraft26%2Frust-jwt-auth-mogodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codecraft26","download_url":"https://codeload.github.com/codecraft26/rust-jwt-auth-mogodb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445651,"owners_count":20939953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backend","mongodb","rocket","rust"],"created_at":"2024-09-26T07:21:10.079Z","updated_at":"2026-01-19T07:32:47.222Z","avatar_url":"https://github.com/codecraft26.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Rust Rocket MongoDB token-auth REST API boilerplate\n\n\nIn this repository, you can find the backend Rust rocket mongodb rest-api\nboilerplate with token authorization.\n\n```rust\n#[get(\"/public/hello\")]\npub async fn hello_world() -\u003e Json\u003c\u0026'static str\u003e {\n  Json(\"Hello world\")\n}\n```\n\nVisiting `localhost:8000/api/v1/public/hello`, for example, will trigger the `hello`\nroute resulting in the string `Hello world` being sent to the\nbrowser. In this example there is no authorization.\n\n## How to start\n1. First you need to download this repository to your computer\n\n* Write this in your terminal:\n\n```sh\ngit clone https://github.com/codecraft26/rust-jwt-auth-mogodb\n```\n* After open the project in your IDE\n\n2. You need to make your private.rs file as in the private.sample.rs:\n\n```rust\npub const JWT_SECRET: \u0026[u8] = b\"\u003cYOUR_JWT_SECRET_KEY\u003e\";\npub const REFRESH_JWT_SECRET: \u0026'static str = b\"\u003cYOUR_REFRESH_JWT_SECRET_KEY\u003e\";\npub(crate) const URL_DB: \u0026str = \"mongodb+srv://\u003cYOUR_LOGIN\u003e:\u003cYOUR_PASSWORD\u003e@cluster0.d5yn0.mongodb.net/\u003cYOUR_DB_NAME\u003e\";\n```\n\n * `JWT_SECRET` and `REFRESH_JWT_SECRET` you need to come up with your own secret word to encrypt the tokens\n * For `URL_DB` you need registration your mongoDB acc here https://cloud.mongodb.com/\n\n3. To run write this in your terminal:\n\n```sh\ncargo check \u0026\u0026 cargo run\n```\n\n## Routes\n\n* /api/v1/registration (POST)\n* /api/v1/login (POST)\n* /api/v1/refresh (POST)\n* /api/v1/public/hello (GET)\n* /api/v1/hello (GET)\n* /api/v1/public/user (DELETE, PATCH)\n\n## Registration acc \n\n### Registration request:\n* `login` (must be unique \u0026\u0026 len login must be from 3 to 200 letter)\n* `password` (len password must be from 8 to 200 letter and password is hashed before being saved to the database.)\n* `mail` (must be unique and mail)\n* `first_name` (len must be from 2 to 150 letter and this field is optional) \n* `last_name` (len must be from 2 to 200 letter and this field is optional)\n\n```rust\npub struct RegistrationRequest {\n    pub login: String,\n    pub password: String,\n\n    pub mail: String,\n\n    pub first_name: String,\n    pub last_name: String,\n}\n```\n#### Example registration request:\n\n```json\n{\n    \"login\": \"test\",\n    \"password\": \"12345678\",\n    \"mail\": \"test@gmail.com\",\n    \"first_name\": \"Test\",\n    \"last_name\": \"\"\n}\n```\n\n### Registration response\n\nIn response, the server will send 2 JWT tokens. \nThe `token` is valid for 1 hour. `Refresh token` is valid for 7 days.\n`token` is needed to verify user authorization.\nWith each private request, the `token` will be sent fronted to \nthe headers in the authorization field.\n\nMore about jwt authentication https://blog.logrocket.com/jwt-authentication-in-rust/\n\n#### If everything is correct:\n```rust\npub struct Token {\n    pub token: String,\n    pub refresh_token: String,\n}\n```\n#### Example:\n\n```json\n{\n    \"token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjJiNGRhOTk4ZjgyMzc2YTk1MzM1MWIxIiwiZXhwIjoxNjU2MDIzMjA5fQ.aJFDZVyMBuNYh5EAArYYfzYCTnHHCQ7IHuZpKNCXHs0\",\n    \"refresh_token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjJiNGRhOTk4ZjgyMzc2YTk1MzM1MWIxIiwiZXhwIjoxNjU4NjExNjA5fQ.2_DjxtQxtsLsprvhBfYU8rKAoDfWMdshoPKDUqq6QZQ\"\n}\n```\n#### Possible error:\n\n* `bad login` -\u003e Status 400 and string \"Bad login\" in json\n* `already registered login` -\u003e Status 400 and string \"Already registered by login\" in json\n* `bad password` -\u003e Status 400 and string \"Bad password\" in json\n* `already registered password` -\u003e Status 400 and string \"Already registered by password\" in json\n* `bad mail` -\u003e Status 400 and string \"Bad mail\" in json\n* `already registered mail` -\u003e Status 400 and string \"Already registered by mail\" in json\n\n#### Example error:\n\nStatus 400 Bad Request\n```json\n{\n    \"cause\": \"Already registered by login\"\n}\n```\n\n## Login acc\n\n### Login request:\n\n* `login`(The server checks 2 logins from the database and the request)\n* `password`(The server checks the encrypted password in the\n  database with the password from the request)\n\n```rust\npub struct LoginRequest {\n  pub login: String,\n  pub password: String,\n}\n```\n\n#### Example:\n```json\n{\n    \"login\": \"test\",\n    \"password\": \"12345678\"\n}\n```\n\n### Login response:\n\n#### If everything is correct:\n```rust\npub struct Token {\n    pub token: String,\n    pub refresh_token: String,\n}\n```\n\n#### Example:\n\n```json\n{\n    \"token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjJhM2I3Zjg4MTE1OWVkYWJmNTcwZjYwIiwiZXhwIjoxNjU2MDI0MDM1fQ.5Nu0lbN5X656JhuY8PrK1IJhWFVjHxKbh8CssKqHQqk\",\n    \"refresh_token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjJhM2I3Zjg4MTE1OWVkYWJmNTcwZjYwIiwiZXhwIjoxNjU4NjEyNDM1fQ.5OUiED1no-uizfYmq1xk6Z6XpX9TsbezDx8QxPSbyV0\"\n}\n```\n\n#### Possible error:\n* `Bad request`(In any case)\n \n#### Example:\n\nStatus 400\n```json\n{\n    \"cause\": \"Wrong request\"\n}\n```\n\n## Refresh token\nIn this route, the frontend asks the server to\nrefresh the `token` with a `refresh token` in json\n\n### Refresh token request\n* `refresh token`(specific user)\n\n```rust\npub struct RefreshToken {\n    pub(crate) refresh_token: String,\n}\n\n```\n\n#### Example refresh token request:\n```json\n{\n    \"refresh_token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjJhM2I3Zjg4MTE1OWVkYWJmNTcwZjYwIiwiZXhwIjoxNjU3NDg5NDcyfQ.BcTanbs5lyT-Yv2ekf5-xl_NzEqpKsh5S59AEuZrmVQ\"\n}\n```\n\n### Refresh token response\n\n#### If everything is correct:\n```rust\npub struct Token {\n    pub token: String,\n    pub refresh_token: String,\n}\n```\n\n#### Example:\n\n```json\n{\n    \"token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjJhM2I3Zjg4MTE1OWVkYWJmNTcwZjYwIiwiZXhwIjoxNjU2MDI0MDM1fQ.5Nu0lbN5X656JhuY8PrK1IJhWFVjHxKbh8CssKqHQqk\",\n    \"refresh_token\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNjJhM2I3Zjg4MTE1OWVkYWJmNTcwZjYwIiwiZXhwIjoxNjU4NjEyNDM1fQ.5OUiED1no-uizfYmq1xk6Z6XpX9TsbezDx8QxPSbyV0\"\n}\n```\n\n#### Possible error:\n* `Unauthorized`\n\n#### Example:\n\nStatus 400\n```json\n{\n    \"cause\": \"Unauthorized\"\n}\n```\n\n## Public hello\n\nThis is the only route without authorization. Therefore there is no request in this route.\n\n### Rust code:\n```rust\n#[get(\"/public/hello\")]\npub async fn hello_world() -\u003e Json\u003c\u0026'static str\u003e {\n  Json(\"Hello world\")\n}\n```\n### Response in json:\n```\n{\n  \"Hello world\"\n}\n```\n\n## Private hello\nIn this route, the server checks in the headers \ntoken if the token is valid then the server executes the program.\n\n### Private hello request:\n#### From headers:\n\n`authorization`   `Bearer` (TOKEN)\n\n#### Example:\n\n`authorization`       `Bearer eyJ0eXAiOiJKV1QiLCJhbGci....`\n\n### Private hello response\n\nthe response will be a greeting with the user.\nIf the database contains his first name and surname, \nthen the program will greet you by the first name and\nsurname; if not, the program will greet you by login\n\n#### If everything is correct:\n\n```rust\npub struct HelloNameResponse {\n    pub(crate) greetings: String,\n}\n```\n#### Example:\n```json\n{\n    \"greetings\": \"Hello test\"\n}\n```\n\n#### Possible error:\n* `Unauthorized`\n\n#### Example:\nStatus 401\n\n```json\n{\n\"cause\": \"Unauthorized\"\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecraft26%2Frust-jwt-auth-mogodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodecraft26%2Frust-jwt-auth-mogodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecraft26%2Frust-jwt-auth-mogodb/lists"}