Update examples and readme for exp use

This commit is contained in:
Vincent Prouillet 2018-10-28 19:54:35 +01:00
parent e725bea1d2
commit ab0ab9b48b
3 changed files with 13 additions and 9 deletions

View File

@ -28,7 +28,8 @@ use jwt::{encode, decode, Header, Algorithm, Validation};
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: String,
company: String
company: String,
exp: usize,
}
```

View File

@ -9,13 +9,15 @@ use jwt::errors::{ErrorKind};
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: String,
company: String
company: String,
exp: usize,
}
fn main() {
let my_claims = Claims {
sub: "b@b.com".to_owned(),
company: "ACME".to_owned()
company: "ACME".to_owned(),
exp: 10000000000,
};
let key = "secret";

View File

@ -9,13 +9,15 @@ use jwt::errors::{ErrorKind};
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
sub: String,
company: String
company: String,
exp: usize,
}
fn main() {
let my_claims = Claims {
sub: "b@b.com".to_owned(),
company: "ACME".to_owned()
company: "ACME".to_owned(),
exp: 10000000000,
};
let key = "secret";
let token = match encode(&Header::default(), &my_claims, key.as_ref()) {
@ -27,13 +29,12 @@ fn main() {
sub: Some("b@b.com".to_string()),
..Validation::default()
};
let token_data = match decode::<Claims>(&token, key.as_ref(), &validation) {
Ok(c) => c,
Err(err) => match *err.kind() {
ErrorKind::InvalidToken => panic!(), // Example on how to handle a specific error
ErrorKind::InvalidIssuer => panic!(), // Example on how to handle a specific error
_ => panic!()
ErrorKind::InvalidToken => panic!("Token is invalid"), // Example on how to handle a specific error
ErrorKind::InvalidIssuer => panic!("Issuer is invalid"), // Example on how to handle a specific error
_ => panic!("Some other errors")
}
};
println!("{:?}", token_data.claims);