Skip to content

Commit

Permalink
Better example
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Prouillet committed Nov 3, 2015
1 parent 31d9932 commit fb229e1
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions examples/claims.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
extern crate jsonwebtoken as jwt;
extern crate rustc_serialize;

use jwt::{
Algorithm,
encode,
decode
};
use jwt::{encode, decode, Algorithm};
use jwt::errors::{Error};

#[derive(Debug, RustcEncodable, RustcDecodable)]
struct Claims {
Expand All @@ -19,6 +16,16 @@ fn main() {
company: "ACME".to_owned()
};
let key = "secret";
let token = encode::<Claims>(my_claims, key.to_owned(), Algorithm::HS256).unwrap();
let claims = decode::<Claims>(token.to_owned(), key.to_owned(), Algorithm::HS256).unwrap();
let token = match encode::<Claims>(my_claims, key.to_owned(), Algorithm::HS256) {
Ok(t) => t,
Err(_) => panic!() // in practice you would return the error
};

let claims = match decode::<Claims>(token.to_owned(), key.to_owned(), Algorithm::HS256) {
Ok(c) => c,
Err(err) => match err {
Error::InvalidToken => panic!(), // Example on how to handle a specific error
_ => panic!()
}
};
}

0 comments on commit fb229e1

Please sign in to comment.