When not logged in redirection to login page
Every page is locked behind a jwt, when it is not supplied neither other pages not api calls will work
This commit is contained in:
@@ -12,23 +12,28 @@ pub struct Error {
|
||||
|
||||
pub fn encode_token(header: &Header, id: String, key: &EncodingKey) -> String {
|
||||
let now = chrono::Utc::now();
|
||||
let issued = now.timestamp() as usize;
|
||||
let expires = (now + chrono::Duration::minutes(60)).timestamp() as usize;
|
||||
let expires = (now + chrono::Duration::minutes(60)).timestamp();
|
||||
let claims: Claims = Claims {
|
||||
subject: id,
|
||||
issued: issued,
|
||||
expires: expires,
|
||||
sub: id,
|
||||
issued: now.timestamp() as usize,
|
||||
expires: expires as usize,
|
||||
};
|
||||
let token = encode(header, &claims, key);
|
||||
return token.expect("token return failed");
|
||||
}
|
||||
|
||||
pub fn decode_token(token: String, key: &DecodingKey) -> Result<Claims, (StatusCode, Json<Error>)> {
|
||||
let claims = decode::<Claims>(&token, key, &Validation::default())
|
||||
.map_err(|_| {
|
||||
let mut validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::HS256);
|
||||
validation.validate_exp = false;
|
||||
validation.validate_nbf = false;
|
||||
validation.leeway = 0;
|
||||
|
||||
let claims = decode::<Claims>(&token, key, &validation)
|
||||
.map_err(|err| {
|
||||
let message = format!("Invalid Token: {}", err);
|
||||
let error = Error {
|
||||
status: "error",
|
||||
message: "Invalid Token".to_string(),
|
||||
message,
|
||||
};
|
||||
(StatusCode::UNAUTHORIZED, Json(error))
|
||||
})?
|
||||
|
||||
Reference in New Issue
Block a user