- Added `bcrypt`, `jsonwebtoken`, and `once_cell` dependencies to manage password hashing and JWT handling. - Created `Claims` struct for JWT claims and implemented token generation in the login route. - Implemented user registration and authentication logic in the `auth` module. - Updated error handling to include validation errors. - Created new routes for user registration and login, and integrated them into the main router. - Added tests for the authentication flow, including registration and login scenarios. - Updated user model to include a password hash field. - Refactored user creation logic to include password validation. - Adjusted feed and user routes to utilize JWT for authentication.
27 lines
984 B
Rust
27 lines
984 B
Rust
#[derive(Debug)]
|
|
pub enum UserError {
|
|
NotFound,
|
|
NotFollowing,
|
|
Forbidden,
|
|
UsernameTaken,
|
|
AlreadyFollowing,
|
|
Validation(String), // Added Validation variant
|
|
Internal(String),
|
|
}
|
|
|
|
impl std::fmt::Display for UserError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
UserError::NotFound => write!(f, "User not found"),
|
|
UserError::NotFollowing => write!(f, "You are not following this user"),
|
|
UserError::Forbidden => write!(f, "You do not have permission to perform this action"),
|
|
UserError::UsernameTaken => write!(f, "Username is already taken"),
|
|
UserError::AlreadyFollowing => write!(f, "You are already following this user"),
|
|
UserError::Validation(msg) => write!(f, "Validation error: {}", msg),
|
|
UserError::Internal(msg) => write!(f, "Internal server error: {}", msg),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for UserError {}
|