Better example

This commit is contained in:
Vincent Prouillet 2015-11-03 16:00:52 +00:00
parent 31d99324e2
commit fb229e1425
1 changed files with 14 additions and 7 deletions

View File

@ -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 {
@ -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!()
}
};
}