From cabdca08f78ee31d1b3dcee0c632394a49f5bbb8 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Wed, 18 Oct 2023 14:27:46 +0200 Subject: [PATCH] Do not include `"email": null` in JWT (if there is no email) This just makes the JWTs larger. --- backend/src/auth/jwt.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/src/auth/jwt.rs b/backend/src/auth/jwt.rs index 49227c430..aa7219300 100644 --- a/backend/src/auth/jwt.rs +++ b/backend/src/auth/jwt.rs @@ -74,12 +74,15 @@ impl JwtContext { + chrono::Duration::from_std(self.config.expiration_time) .expect("failed to convert from std Duration to chrono::Duration"); - let payload = json!({ + let mut payload = json!({ "name": user.display_name, "username": user.username, - "email": user.email, "exp": exp.timestamp(), }); + if let Some(email) = &user.email { + payload.as_object_mut().unwrap().insert("email".into(), email.clone().into()); + } + self.encode(&payload) }