yew_oauth2/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2//! Yew components to implement OAuth2 and OpenID Connect logins.
3//!
4//! ## OAuth2 or Open ID Connect
5//!
6//! This crate supports both plain OAuth2 and Open ID Connect (OIDC). OIDC layers a few features
7//! on top of OAuth2 (like logout URLs, discovery, …).
8//!
9//! In order to use OIDC, you will need to enable the feature `openid`.
10//!
11//! ## Example
12//!
13//! **NOTE:** Also see the [readme](https://github.com/ctron/yew-oauth2/blob/main/README.md#examples) for more examples.
14//!
15//! The following is a basic example:
16//!
17//! ```rust
18//! use yew::prelude::*;
19//! use yew_oauth2::prelude::*;
20//! use yew_oauth2::oauth2::*; // use `openid::*` when using OpenID connect
21//!
22//! #[function_component(MyApplication)]
23//! fn my_app() -> Html {
24//!   let config = Config::new(
25//!     "my-client",
26//!     "https://my-sso/auth/realms/my-realm/protocol/openid-connect/auth",
27//!     "https://my-sso/auth/realms/my-realm/protocol/openid-connect/token"
28//!   );
29//!
30//!   html!(
31//!     <OAuth2 {config}>
32//!       <MyApplicationMain/>
33//!     </OAuth2>
34//!   )
35//! }
36//!
37//! #[function_component(MyApplicationMain)]
38//! fn my_app_main() -> Html {
39//!   let agent = use_auth_agent().expect("Must be nested inside an OAuth2 component");
40//!
41//!   let login = use_callback(agent.clone(), |_, agent| {
42//!     let _ = agent.start_login();
43//!   });
44//!   let logout = use_callback(agent, |_, agent| {
45//!     let _ = agent.logout();
46//!   });
47//!
48//!   html!(
49//!     <>
50//!       <Failure><FailureMessage/></Failure>
51//!       <Authenticated>
52//!         <button onclick={logout}>{ "Logout" }</button>
53//!       </Authenticated>
54//!       <NotAuthenticated>
55//!         <button onclick={login}>{ "Login" }</button>
56//!       </NotAuthenticated>
57//!     </>
58//!   )
59//! }
60//! ```
61
62pub mod agent;
63pub mod components;
64pub mod config;
65pub mod context;
66pub mod hook;
67pub mod prelude;
68
69#[cfg(feature = "openid")]
70pub mod openid {
71    //! Common used Open ID Connect features
72    pub use crate::agent::client::OpenIdClient as Client;
73    pub use crate::components::context::openid::*;
74    pub use crate::components::redirect::location::openid::*;
75    #[cfg(feature = "yew-nested-router")]
76    pub use crate::components::redirect::router::openid::*;
77    pub use crate::config::openid::*;
78
79    #[yew::hook]
80    pub fn use_auth_agent() -> Option<crate::components::context::Agent<Client>> {
81        crate::components::context::use_auth_agent::<Client>()
82    }
83}
84
85pub mod oauth2 {
86    //! Common used OAuth2 features
87    pub use crate::agent::client::OAuth2Client as Client;
88    pub use crate::components::context::oauth2::*;
89    pub use crate::components::redirect::location::oauth2::*;
90    #[cfg(feature = "yew-nested-router")]
91    pub use crate::components::redirect::router::oauth2::*;
92    pub use crate::config::oauth2::*;
93
94    #[yew::hook]
95    pub fn use_auth_agent() -> Option<crate::components::context::Agent<Client>> {
96        crate::components::context::use_auth_agent::<Client>()
97    }
98}