[][src]Function jsonwebtoken::encode

pub fn encode<T: Serialize>(
    header: &Header,
    claims: &T,
    key: &EncodingKey
) -> Result<String>

Encode the header and claims given and sign the payload using the algorithm from the header and the key. If the algorithm given is RSA or EC, the key needs to be in the PEM format.

use serde::{Deserialize, Serialize};
use jsonwebtoken::{encode, Algorithm, Header, EncodingKey};

#[derive(Debug, Serialize, Deserialize)]
struct Claims {
   sub: String,
   company: String
}

let my_claims = Claims {
    sub: "b@b.com".to_owned(),
    company: "ACME".to_owned()
};

// my_claims is a struct that implements Serialize
// This will create a JWT using HS256 as algorithm
let token = encode(&Header::default(), &my_claims, &EncodingKey::from_secret("secret".as_ref())).unwrap();