Crates.io | rusty-jwt |
lib.rs | rusty-jwt |
version | 0.1.14 |
source | src |
created_at | 2024-10-16 16:48:37.397301 |
updated_at | 2024-10-17 06:17:28.507741 |
description | A simple Rust library for encoding and decoding JSON Web Tokens (JWT). |
homepage | https://github.com/HashiramaSenjuhari/http_jwt |
repository | https://github.com/HashiramaSenjuhari/http_jwt |
max_upload_size | |
id | 1412010 |
size | 10,415 |
This Rust module provides functionality to encode and decode JSON Web Tokens (JWT) for user authentication. It uses the http-scrap
,jsonwebtoken
, serde
, and uuid
crates to handle JWTs securely.
This module demonstrates how to encode and decode JWTs, using secret keys stored in environment variables. JWTs are commonly used for user authentication, storing details like user IDs, names, and expiration times.
Make sure to add the following dependencies to your Cargo.toml
:
[dependencies]
serde = { version = "1.0", features = ["derive"] }
jsonwebtoken = "8.0"
uuid = "1.2"
[encode]
//encode token
let user = Users {
name: String::from("John Doe"),
email: String::from("john.doe@example.com"),
exp: 3600, // Token expires in 1 hour
};
let jwt = Cookie::encode("JWT_SECRET_NAME".to_string(), user).unwrap();
println!("Generated JWT: {}", jwt);
[decode]
//decode
use http_scrap::Response;
let response = Response::new(&response); //use http-scrap
let cookie = response.cookie();
let decoded = Cookie::decode("JWT_SECRET_NAME".to_string(), cookie).unwrap();
println!("Decoded Claims: {:?}", decoded.claims);