24 releases (10 breaking)

new 0.11.0 Apr 19, 2024
0.10.2 Mar 11, 2024
0.10.1 Jan 26, 2024
0.9.1 Dec 19, 2023
0.3.0 Jun 20, 2022

#5 in #oidc

Download history 1/week @ 2023-12-23 34/week @ 2023-12-30 133/week @ 2024-01-06 166/week @ 2024-01-13 180/week @ 2024-01-20 176/week @ 2024-01-27 186/week @ 2024-02-03 215/week @ 2024-02-10 191/week @ 2024-02-17 255/week @ 2024-02-24 295/week @ 2024-03-02 367/week @ 2024-03-09 97/week @ 2024-03-16 8/week @ 2024-03-23 193/week @ 2024-03-30 126/week @ 2024-04-06

473 downloads per month

Apache-2.0

90KB
2K SLoC

OAuth2 (and OIDC) component for Yew

crates.io docs.rs CI

Add to your Cargo.toml:

yew-oauth2 = "0.11"

By default, the yew-nested-router integration for yew-nested-router is disabled. You can enable it using:

yew-oauth2 = { version = "0.10", features = ["yew-nested-router"] }

OpenID Connect

OpenID Connect requires an additional dependency and can be enabled using the feature openid.

Examples

A quick example of how to use it (see below for more complete examples):

use yew::prelude::*;
use yew_oauth2::prelude::*;
use yew_oauth2::oauth2::*; // use `openid::*` when using OpenID connect

#[function_component(MyApplication)]
fn my_app() -> Html {
    let config = Config::new(
        "my-client",
        "https://my-sso/auth/realms/my-realm/protocol/openid-connect/auth",
        "https://my-sso/auth/realms/my-realm/protocol/openid-connect/token"
    );

    html!(
    <OAuth2 {config}>
      <MyApplicationMain/>
    </OAuth2>
  )
}

#[function_component(MyApplicationMain)]
fn my_app_main() -> Html {
    let agent = use_auth_agent().expect("Must be nested inside an OAuth2 component");

    let login = use_callback(agent.clone(), |_, agent| {
        let _ = agent.start_login();
    });
    let logout = use_callback(agent, |_, agent| {
        let _ = agent.logout();
    });

    html!(
    <>
      <Failure><FailureMessage/></Failure>
      <Authenticated>
        <button onclick={logout}>{ "Logout" }</button>
      </Authenticated>
      <NotAuthenticated>
        <button onclick={login}>{ "Login" }</button>
      </NotAuthenticated>
    </>
  )
}

This repository also has some complete examples:

yew-oauth2-example

A complete example, hiding everything behind a "login" page, and revealing the content once the user logged in.

Use with either OpenID Connect or OAuth2.

yew-oauth2-redirect-example

A complete example, showing the full menu structure, but redirecting the user automatically to the login server when required.

Use with either OpenID Connect or OAuth2.

Testing

Testing the example projects locally can be done using a local Keycloak instance and trunk.

Start the Keycloak instance using:

podman-compose -f develop/docker-compose.yaml up

Then start trunk with the local developer instance:

cd yew-oauth2-example # or yew-oauth2-redirect-example
trunk serve

And navigate your browser to http://localhost:8080.

NOTE: It is important to use http://localhost:8080 instead of e.g. http://127.0.0.1:8080, as Keycloak is configured by default to use http://localhost:* as a valid redirect URL when in dev-mode. Otherwise, you will get an "invalid redirect" error from Keycloak.

Dependencies

~18–36MB
~560K SLoC