| Crates.io | rocket_oauth2 |
| lib.rs | rocket_oauth2 |
| version | 0.5.0 |
| created_at | 2018-11-01 05:33:27.946699+00 |
| updated_at | 2023-11-22 13:41:39.257073+00 |
| description | OAuth2 for Rocket applications |
| homepage | https://github.com/jebrosen/rocket_oauth2 |
| repository | https://github.com/jebrosen/rocket_oauth2 |
| max_upload_size | |
| id | 93958 |
| size | 68,318 |
rocket_oauth2 helps set up an OAuth 2.0 client in Rocket
applications.
rocket 0.4.x, are based on the previous master branchrocket 0.5.x, are based on the newer main branchFor more detailed examples and explanations, see the crate documentation and the
projects in the repository's examples directory.
use rocket::http::{Cookie, CookieJar, SameSite};
use rocket::Request;
use rocket::response::Redirect;
use rocket_oauth2::{OAuth2, TokenResponse};
struct GitHub;
#[get("/login/github")]
fn github_login(oauth2: OAuth2<GitHub>, cookies: &CookieJar<'_>) -> Redirect {
oauth2.get_redirect(cookies, &["user:read"]).unwrap()
}
#[get("/auth/github")]
fn github_callback(token: TokenResponse<GitHub>, cookies: &CookieJar<'_>) -> Redirect
{
cookies.add_private(
Cookie::build(("token", token.access_token().to_string()))
.same_site(SameSite::Lax)
.build()
);
Redirect::to("/")
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![github_callback, github_login])
.attach(OAuth2::<GitHub>::fairing("github"))
}
Rocket.toml)[default.oauth.github]
provider = "GitHub"
client_id = "..."
client_secret = "..."
redirect_uri = "http://localhost:8000/auth/github"
rocket_oauth2 is licensed under either of the following, at your option: