DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

3a729b27a2d7c122eecda8127e364a399c07fa6c — Jonni Liljamo 1 year, 9 months ago 329b57a
feat(api): reimpl user registration
A api/src/actions/mod.rs => api/src/actions/mod.rs +9 -0
@@ 0,0 1,9 @@
/*
 * This file is part of laurelin/api
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

pub(crate) mod user;

A api/src/actions/user/create.rs => api/src/actions/user/create.rs +47 -0
@@ 0,0 1,47 @@
/*
 * This file is part of laurelin/api
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use argon2::{
    password_hash::{rand_core::OsRng, SaltString},
    Argon2, PasswordHasher,
};
use diesel::{PgConnection, RunQueryDsl};
use laurelin_shared::error::api::APIError;

use crate::{
    models::{InsertableUser, User},
    schema::users,
};

pub(crate) fn create(conn: &mut PgConnection, user: &InsertableUser) -> Result<User, APIError> {
    let salt = SaltString::generate(&mut OsRng);
    let password_hash_result = Argon2::default().hash_password(user.password.as_bytes(), &salt);

    let password_hash = match password_hash_result {
        Err(_) => return Err(APIError::UserPasswordHashFailed),
        Ok(password_hash) => password_hash.to_string(),
    };

    let new_user = InsertableUser {
        username: user.username.clone(),
        email: user.email.clone(),
        password: password_hash,
    };

    let user: Result<User, diesel::result::Error> = diesel::insert_into(users::table)
        .values(&new_user)
        .get_result::<User>(conn);

    match user {
        Err(_) => {
            // TODO: we certainly could handle Diesel errors here...
            return Err(APIError::UserCreationFailed);
        }
        Ok(u) => return Ok(u),
    }
}

A api/src/actions/user/mod.rs => api/src/actions/user/mod.rs +10 -0
@@ 0,0 1,10 @@
/*
 * This file is part of laurelin/api
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

mod create;
pub(crate) use create::*;

M api/src/handlers/mod.rs => api/src/handlers/mod.rs +2 -0
@@ 8,3 8,5 @@

mod info;
pub(crate) use info::*;

pub(crate) mod user;

A api/src/handlers/user/create.rs => api/src/handlers/user/create.rs +54 -0
@@ 0,0 1,54 @@
/*
 * This file is part of laurelin/api
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use actix_web::{post, web, HttpResponse, Responder};
use email_address::EmailAddress;

use laurelin_shared::error::api::APIError;

use crate::{actions, models, PgPool};

#[post("/api/user")]
pub(crate) async fn create(
    pool: web::Data<PgPool>,
    user: web::Json<models::InsertableUser>,
) -> impl Responder {
    if user.username.len() < 3 {
        return HttpResponse::BadRequest().json(APIError::UserUsernameTooShort);
    }

    if user.password.len() < 8 {
        return HttpResponse::BadRequest().json(APIError::UserPasswordTooShort);
    }

    if !EmailAddress::is_valid(&user.email) {
        return HttpResponse::BadRequest().json(APIError::UserEmailInvalid);
    }

    let user_create = web::block(move || {
        let mut conn = match pool.get() {
            Err(_) => return Err(APIError::DatabasePoolGetFailed),
            Ok(conn) => conn,
        };
        actions::user::create(&mut conn, &user.0)
    })
    .await;

    match user_create {
        Err(_err) => {
            // TODO: handle?
            return HttpResponse::InternalServerError().json(APIError::Undefined);
        }
        Ok(user_res) => match user_res {
            Err(err) => {
                return HttpResponse::InternalServerError().json(err);
            }
            Ok(user) => HttpResponse::Ok().json(user),
        },
    }
}

A api/src/handlers/user/mod.rs => api/src/handlers/user/mod.rs +10 -0
@@ 0,0 1,10 @@
/*
 * This file is part of laurelin/api
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

mod create;
pub(crate) use create::*;

M api/src/main.rs => api/src/main.rs +2 -0
@@ 30,6 30,7 @@ fn run_migrations(conn: &mut PgConnection) {

mod schema;

mod actions;
mod handlers;
mod models;



@@ 57,6 58,7 @@ async fn main() -> std::io::Result<()> {
            .wrap(Logger::new(log_format))
            .service(ping)
            .service(handlers::info)
            .service(handlers::user::create)
    })
    .bind(("0.0.0.0", 8080))?
    .run()