1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// src/lib.rs
//! Simple authentication for Rust web apps with JWT tokens and user management.
//!
//! Supports both Rocket backends and Yew frontends with role-based permissions.
//!
//! # Quick Start
//!
//! ## Backend (Rocket)
//!
//! ```rust
//! use rocket::State;
//! use prisma_auth::multi_user::{AuthService, MultiUserTokenStore, MultiUserAuthGuard, multi_user_login_handler};
//! use prisma_auth::storage::InMemoryStorage;
//! use prisma_auth::{LoginRequest, TokenResponse};
//!
//! #[rocket::post("/login", format = "json", data = "<body>")]
//! pub fn login(
//! body: Json<LoginRequest>,
//! token_store: &State<MultiUserTokenStore>,
//! auth_service: &State<AuthService<InMemoryStorage>>,
//! ) -> Result<Json<TokenResponse>, Status> {
//! multi_user_login_handler(body, token_store, auth_service)
//! }
//!
//! #[rocket::get("/protected")]
//! pub fn protected_route(auth: MultiUserAuthGuard) -> String {
//! format!("Hello, {}! Your role: {}", auth.username, auth.role)
//! }
//!
//! #[launch]
//! fn rocket() -> _ {
//! let storage = InMemoryStorage::new().with_default_roles();
//! let auth_service = AuthService::new(storage);
//!
//! rocket::build()
//! .manage(MultiUserTokenStore::new())
//! .manage(auth_service)
//! .mount("/api", routes![login, protected_route])
//! }
//! ```
//!
//! ## Frontend (Yew)
//!
//! ```rust
//! use yew::prelude::*;
//! use prisma_auth::frontend::{MultiUserLoginComponent, AuthGuardComponent, UserInfo};
//!
//! #[function_component(App)]
//! pub fn app() -> Html {
//! let on_success = Callback::from(|user_info: UserInfo| {
//! web_sys::console::log_1(&format!("Login successful! User: {}", user_info.username).into());
//! });
//!
//! html! {
//! <div>
//! <MultiUserLoginComponent
//! api_base_url={"http://localhost:8000/api".to_string()}
//! on_success={on_success}
//! title={"My App".to_string()}
//! show_username={true}
//! />
//!
//! <AuthGuardComponent>
//! <div>{"This content is only visible to authenticated users"}</div>
//! </AuthGuardComponent>
//! </div>
//! }
//! }
//! ```
/// Data types for authentication requests and responses
/// Error handling for authentication operations
/// Rocket route handlers for authentication endpoints
/// User and role data storage implementations
/// Multi-user authentication service with role management
/// Yew components and utilities for frontend authentication
pub use *;
pub use AuthError;