{"id":15018053,"url":"https://github.com/tvallotton/rocket_auth","last_synced_at":"2025-04-07T11:10:53.988Z","repository":{"id":37012681,"uuid":"349800874","full_name":"tvallotton/rocket_auth","owner":"tvallotton","description":"An implementation for an authentication API for Rocket applications. ","archived":false,"fork":false,"pushed_at":"2024-03-20T18:50:13.000Z","size":641,"stargazers_count":76,"open_issues_count":38,"forks_count":41,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T10:05:09.801Z","etag":null,"topics":["auth","authentication","login","rocket","rust","users","web","web-development","webapp"],"latest_commit_sha":null,"homepage":"https://docs.rs/rocket_auth/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tvallotton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","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":"2021-03-20T18:07:50.000Z","updated_at":"2025-03-14T09:55:32.000Z","dependencies_parsed_at":"2024-09-16T12:32:25.292Z","dependency_job_id":"92a5c6f9-a656-4dd2-adc0-bf10476d622b","html_url":"https://github.com/tvallotton/rocket_auth","commit_stats":{"total_commits":151,"total_committers":5,"mean_commits":30.2,"dds":"0.22516556291390732","last_synced_commit":"1a7011fd6d2e8b818fbc0e504135d11212472409"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvallotton%2Frocket_auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvallotton%2Frocket_auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvallotton%2Frocket_auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvallotton%2Frocket_auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tvallotton","download_url":"https://codeload.github.com/tvallotton/rocket_auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247640465,"owners_count":20971557,"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":["auth","authentication","login","rocket","rust","users","web","web-development","webapp"],"created_at":"2024-09-24T19:51:22.370Z","updated_at":"2025-04-07T11:10:53.970Z","avatar_url":"https://github.com/tvallotton.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# rocket_auth\nrocket_auth provides a ready-to-use  backend agnostic API for authentication management.\nIt supports connections for SQLite and Postgresql. It lets you create, delete, and authenticate users.\nThe available features are:\n* `sqlx-sqlite`: for interacting with a SQLite database using `sqlx`.\n* `sqlx-postgres`: for interacting with a Postgresql database with `sqlx`.\n* `sqlx-mysql`: for interacting with a MySql database with `sqlx`.\n* `redis`: for storing sessions on a redis server using `redis`.\n* `rusqlite`: for interacting with a SQLite database using `rusqlite`.\n* `tokio-postgres`: for interacting with a Postgresql database with `tokio-postgres`.\n\n`rocket_auth` uses private cookies to store session data.\nThis means that in order for cookies to be properly decrypted between launches, a `secret_key` must be set.\nFor more information visit rocket's [configuration guide](https://rocket.rs/v0.5-rc/guide/configuration/#configuration).\n\n\n\n\n\nTo use `rocket_auth` include it as a dependency in your Cargo.toml file:\n```ini\n[dependencies.rocket_auth]\nversion = \"0.4.0\"\nfeatures = [\"sqlx-sqlite\"]\n```\n# Quick overview\nThis crate provides three guards:\n* `Auth`: Manages authentication.\n* `Session`: It's used to retrieve session data from client cookies.\n* `User`: It restricts content, so it can be viewed by authenticated clients only.\n\n\nIt also includes two structs to be parsed from forms and json data:\n* `Signup`: Used to create new users.\n* `Login`: Used to authenticate users.\n\n\nFinally, it has two structures for queries:\n* `Users`: It allows to query users to the database.\n* `User`: It is the response of a query.\n\n\nThe `Auth` guard allows to log in, log out, sign up, modify, and delete the currently (un)authenticated user.\nFor more information see `Auth`.\n A working example:\n```rust\nuse rocket::{get, post, form::Form, routes};\nuse rocket_auth::{Users, Error, Auth, Signup, Login};\n\n#[post(\"/signup\", data=\"\u003cform\u003e\")]\nasync fn signup(form: Form\u003cSignup\u003e, auth: Auth\u003c'_\u003e) -\u003e Result\u003c\u0026'static str, Error\u003e {\n    auth.signup(\u0026form).await?;\n    auth.login(\u0026form.into());\n    Ok(\"You signed up.\")\n}\n\n#[post(\"/login\", data=\"\u003cform\u003e\")]\nasync fn login(form: Form\u003cLogin\u003e, auth: Auth\u003c'_\u003e) -\u003e Result\u003c\u0026'static str, Error\u003e{\n    auth.login(\u0026form).await?;\n    Ok(\"You're logged in.\")\n}\n\n#[post(\"/logout\")]\nfn logout(auth: Auth\u003c'_\u003e) {\n    auth.logout();\n}\n#[tokio::main]\nasync fn main() -\u003e Result\u003c(), Error\u003e{\n    let users = Users::open_sqlite(\"mydb.db\").await?;\n\n    rocket::build()\n        .mount(\"/\", routes![signup, login, logout])\n        .manage(users)\n        .launch();\n    Ok(())\n}\n```\n\n## Users struct\nThe `Users` struct administers interactions with the database.\nIt lets you query, create, modify and delete users.\nUnlike the `Auth` guard, a `Users` instance can manage any user in the database.\nNote that the `Auth` guards includes a `Users` instance stored on the public `users` field.\nSo it is not necessary to retrieve Users when using `Auth`.\nA simple example of how to query a user with the `Users` struct:\n\n```rust\nuse rocket_auth::Users;\n\n#[get(\"/see-user/\u003cid\u003e\")]\nasync fn see_user(id: i32, users: \u0026State\u003cUsers\u003e) -\u003e String {\n    let user = users.get_by_id(id).await.unwrap();\n    format!(\"{}\", json!(user))\n}\n```\n\nA `Users` instance can be constructed by connecting it to the database with the methods `open_sqlite`,\n`open_postgres`. Furthermore, it can be constructed from a working connection.\n\n\n## User guard\nThe `User` guard can be used to restrict content, so that it can only be viewed by authenticated users.\nAdditionally, you can use it to render special content if the client is authenticated or not.\n```rust\n#[get(\"/private-content\")]\nfn private_content(user: User) -\u003e \u0026'static str {\n    \"If you can see this, you are logged in.\"\n}\n\n#[get(\"/special-content\")]\nfn special_content(option: Option\u003cUser\u003e) -\u003e String {\n    if let Some(user) = option {\n        format!(\"hello, {}.\", user.email())\n    } else {\n        \"hello, anonymous user\".into()\n    }\n}\n#[get(\"/admins-only\")]\nfn admins_only(user: AdminUser) -\u003e \u0026'static str {\n   \"Hello administrator.\"\n}\n```\n\n## AdminUser guard\nThe `AdminUser` guard can be used analogously to `User`.\nIt will restrict content, so that it can be viewed by admins only.\n```rust\n# use rocket::*;\n# use rocket_auth::AdminUser;\n#[get(\"/admin-panel\")]\nfn admin_panel(user: AdminUser) -\u003e String {\n   format!(\"Hello {}.\", user.email());\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftvallotton%2Frocket_auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftvallotton%2Frocket_auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftvallotton%2Frocket_auth/lists"}