(State(ctx): State<AppContext>, Json(params): Json<LoginParams>)
| 128 | /// Creates a user login and returns a token |
| 129 | #[debug_handler] |
| 130 | async fn login(State(ctx): State<AppContext>, Json(params): Json<LoginParams>) -> Result<Response> { |
| 131 | let user = users::Model::find_by_email(&ctx.db, ¶ms.email).await?; |
| 132 | |
| 133 | let valid = user.verify_password(¶ms.password); |
| 134 | |
| 135 | if !valid { |
| 136 | return unauthorized("unauthorized!"); |
| 137 | } |
| 138 | |
| 139 | let jwt_secret = ctx.config.get_jwt_config()?; |
| 140 | |
| 141 | let token = user |
| 142 | .generate_jwt(&jwt_secret.secret, &jwt_secret.expiration) |
| 143 | .or_else(|_| unauthorized("unauthorized!"))?; |
| 144 | |
| 145 | format::json(LoginResponse::new(&user, &token)) |
| 146 | } |
| 147 | |
| 148 | pub fn routes() -> Routes { |
| 149 | Routes::new() |
nothing calls this directly
no test coverage detected