torrust_index_backend/web/api/v1/contexts/user/
responses.rs

1use axum::Json;
2use serde::{Deserialize, Serialize};
3
4use crate::models::user::{UserCompact, UserId};
5use crate::web::api::v1::responses::OkResponseData;
6
7// Registration
8
9#[derive(Serialize, Deserialize, Debug)]
10pub struct NewUser {
11    pub user_id: UserId,
12}
13
14/// Response after successfully creating a new user.
15pub fn added_user(user_id: i64) -> Json<OkResponseData<NewUser>> {
16    Json(OkResponseData {
17        data: NewUser { user_id },
18    })
19}
20
21// Authentication
22
23#[derive(Serialize, Deserialize, Debug)]
24pub struct TokenResponse {
25    pub token: String,
26    pub username: String,
27    pub admin: bool,
28}
29
30/// Response after successfully logging in a user.
31pub fn logged_in_user(token: String, user_compact: UserCompact) -> Json<OkResponseData<TokenResponse>> {
32    Json(OkResponseData {
33        data: TokenResponse {
34            token,
35            username: user_compact.username,
36            admin: user_compact.administrator,
37        },
38    })
39}
40
41/// Response after successfully renewing a JWT.
42pub fn renewed_token(token: String, user_compact: UserCompact) -> Json<OkResponseData<TokenResponse>> {
43    Json(OkResponseData {
44        data: TokenResponse {
45            token,
46            username: user_compact.username,
47            admin: user_compact.administrator,
48        },
49    })
50}