Skip to content

Commit

Permalink
build: simple example prints valid time
Browse files Browse the repository at this point in the history
  • Loading branch information
simbleau committed Oct 21, 2023
1 parent f4ac1ac commit 16616fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ bevy-async-task = "1.2"
async-trait = "0.1"
bevy = { version = "0.11", default-features = false }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
getrandom = { version = "0.2" }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3"
getrandom = { version = "0.2", features = ["js"] }
33 changes: 25 additions & 8 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl AuthProvider for MyAuthProvider {
Ok(Keystore {
username,
password,
access_token: "123".to_string(),
refresh_token: "456".to_string(),
access_token: random_token(),
refresh_token: random_token(),
access_expires: bevy_key_rotation::Instant::now()
+ Duration::from_secs(10),
refresh_expires: bevy_key_rotation::Instant::now()
Expand All @@ -36,7 +36,7 @@ impl AuthProvider for MyAuthProvider {
Ok(Keystore {
username: keystore.username,
password: keystore.password,
access_token: "789".to_string(),
access_token: random_token(),
refresh_token: keystore.refresh_token,
access_expires: keystore.access_expires + Duration::from_secs(5),
refresh_expires: keystore.refresh_expires,
Expand All @@ -49,17 +49,25 @@ fn status_check(
mut update_every: Local<Option<Timer>>,
keystore: Res<Keystore>,
) {
// Print status every 2s
let update_every = update_every
.get_or_insert(Timer::from_seconds(2.0, TimerMode::Repeating));
// Print status every few seconds...
const PRINT_EVERY_SECONDS: f32 = 1.0;
let update_every = update_every.get_or_insert(Timer::from_seconds(
PRINT_EVERY_SECONDS,
TimerMode::Repeating,
));
update_every.tick(time.delta());
if !update_every.finished() {
return;
}
update_every.reset();

// Log current access token
log::info!("{:?}", *keystore);
log::info!(
token = keystore.access_token,
refresh_token = keystore.refresh_token,
"token valid for: {:.0?}, refresh token valid for: {:.0?}",
keystore.access_token_valid_for(),
keystore.refresh_token_valid_for(),
);
}

pub fn main() {
Expand All @@ -80,3 +88,12 @@ pub fn main() {
.add_systems(Update, status_check)
.run();
}

fn random_token() -> String {
let mut token = vec![0; 6];
getrandom::getrandom(&mut token).unwrap();
for byte in token.as_mut_slice() {
*byte = (*byte % 26) + b'A'; // Convert to A-Z character
}
String::from_utf8(token).unwrap()
}

0 comments on commit 16616fc

Please sign in to comment.