Update examples and readme for exp use
This commit is contained in:
parent
e725bea1d2
commit
ab0ab9b48b
|
@ -28,7 +28,8 @@ use jwt::{encode, decode, Header, Algorithm, Validation};
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Claims {
|
struct Claims {
|
||||||
sub: String,
|
sub: String,
|
||||||
company: String
|
company: String,
|
||||||
|
exp: usize,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -9,13 +9,15 @@ use jwt::errors::{ErrorKind};
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Claims {
|
struct Claims {
|
||||||
sub: String,
|
sub: String,
|
||||||
company: String
|
company: String,
|
||||||
|
exp: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_claims = Claims {
|
let my_claims = Claims {
|
||||||
sub: "b@b.com".to_owned(),
|
sub: "b@b.com".to_owned(),
|
||||||
company: "ACME".to_owned()
|
company: "ACME".to_owned(),
|
||||||
|
exp: 10000000000,
|
||||||
};
|
};
|
||||||
let key = "secret";
|
let key = "secret";
|
||||||
|
|
||||||
|
|
|
@ -9,13 +9,15 @@ use jwt::errors::{ErrorKind};
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct Claims {
|
struct Claims {
|
||||||
sub: String,
|
sub: String,
|
||||||
company: String
|
company: String,
|
||||||
|
exp: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_claims = Claims {
|
let my_claims = Claims {
|
||||||
sub: "b@b.com".to_owned(),
|
sub: "b@b.com".to_owned(),
|
||||||
company: "ACME".to_owned()
|
company: "ACME".to_owned(),
|
||||||
|
exp: 10000000000,
|
||||||
};
|
};
|
||||||
let key = "secret";
|
let key = "secret";
|
||||||
let token = match encode(&Header::default(), &my_claims, key.as_ref()) {
|
let token = match encode(&Header::default(), &my_claims, key.as_ref()) {
|
||||||
|
@ -27,13 +29,12 @@ fn main() {
|
||||||
sub: Some("b@b.com".to_string()),
|
sub: Some("b@b.com".to_string()),
|
||||||
..Validation::default()
|
..Validation::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let token_data = match decode::<Claims>(&token, key.as_ref(), &validation) {
|
let token_data = match decode::<Claims>(&token, key.as_ref(), &validation) {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(err) => match *err.kind() {
|
Err(err) => match *err.kind() {
|
||||||
ErrorKind::InvalidToken => panic!(), // Example on how to handle a specific error
|
ErrorKind::InvalidToken => panic!("Token is invalid"), // Example on how to handle a specific error
|
||||||
ErrorKind::InvalidIssuer => panic!(), // Example on how to handle a specific error
|
ErrorKind::InvalidIssuer => panic!("Issuer is invalid"), // Example on how to handle a specific error
|
||||||
_ => panic!()
|
_ => panic!("Some other errors")
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
println!("{:?}", token_data.claims);
|
println!("{:?}", token_data.claims);
|
||||||
|
|
Loading…
Reference in New Issue