forked from gorb/backend
feat: add changing username, email and display_name to /me endpoint
This commit is contained in:
parent
1ff3fa69a7
commit
9728769b8c
6 changed files with 95 additions and 29 deletions
|
@ -9,10 +9,12 @@ use uuid::Uuid;
|
|||
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::{EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX},
|
||||
error::Error,
|
||||
schema::*,
|
||||
utils::{generate_access_token, generate_refresh_token, refresh_token_cookie},
|
||||
utils::{
|
||||
EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX, generate_access_token, generate_refresh_token,
|
||||
refresh_token_cookie,
|
||||
},
|
||||
};
|
||||
|
||||
use super::Response;
|
||||
|
|
|
@ -22,15 +22,6 @@ struct Response {
|
|||
access_token: String,
|
||||
}
|
||||
|
||||
static EMAIL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(r"[-A-Za-z0-9!#$%&'*+/=?^_`{|}~]+(?:\.[-A-Za-z0-9!#$%&'*+/=?^_`{|}~]+)*@(?:[A-Za-z0-9](?:[-A-Za-z0-9]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[-A-Za-z0-9]*[A-Za-z0-9])?").unwrap()
|
||||
});
|
||||
|
||||
static USERNAME_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-z0-9_.-]+$").unwrap());
|
||||
|
||||
// Password is expected to be hashed using SHA3-384
|
||||
static PASSWORD_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[0-9a-f]{96}").unwrap());
|
||||
|
||||
pub fn web() -> Scope {
|
||||
web::scope("/auth")
|
||||
.service(register::res)
|
||||
|
|
|
@ -13,14 +13,16 @@ use uuid::Uuid;
|
|||
use super::Response;
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::{EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX},
|
||||
error::Error,
|
||||
schema::{
|
||||
access_tokens::{self, dsl as adsl},
|
||||
refresh_tokens::{self, dsl as rdsl},
|
||||
users::{self, dsl as udsl},
|
||||
},
|
||||
utils::{generate_access_token, generate_refresh_token, refresh_token_cookie},
|
||||
utils::{
|
||||
EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX, generate_access_token, generate_refresh_token,
|
||||
refresh_token_cookie,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -68,9 +70,11 @@ pub async fn res(
|
|||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
if !data.config.instance.registration {
|
||||
return Err(Error::Forbidden("registration is disabled on this instance".to_string()))
|
||||
return Err(Error::Forbidden(
|
||||
"registration is disabled on this instance".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
let uuid = Uuid::now_v7();
|
||||
|
||||
if !EMAIL_REGEX.is_match(&account_information.email) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use actix_multipart::form::{MultipartForm, json::Json as MpJson, tempfile::TempFile};
|
||||
use actix_web::{get, patch, web, HttpRequest, HttpResponse, Scope};
|
||||
use actix_web::{HttpRequest, HttpResponse, Scope, get, patch, web};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
|
@ -9,9 +9,7 @@ use crate::{
|
|||
mod servers;
|
||||
|
||||
pub fn web() -> Scope {
|
||||
web::scope("/me")
|
||||
.service(get)
|
||||
.service(update)
|
||||
web::scope("/me").service(get).service(update)
|
||||
}
|
||||
|
||||
#[get("")]
|
||||
|
@ -76,11 +74,11 @@ pub async fn update(
|
|||
|
||||
if let Some(new_info) = form.json {
|
||||
if let Some(username) = &new_info.username {
|
||||
todo!();
|
||||
me.set_username(&mut conn, username.clone()).await?;
|
||||
}
|
||||
|
||||
if let Some(display_name) = &new_info.display_name {
|
||||
todo!();
|
||||
me.set_display_name(&mut conn, display_name.clone()).await?;
|
||||
}
|
||||
|
||||
if let Some(password) = &new_info.password {
|
||||
|
@ -88,7 +86,7 @@ pub async fn update(
|
|||
}
|
||||
|
||||
if let Some(email) = &new_info.email {
|
||||
todo!();
|
||||
me.set_email(&mut conn, email.to_string()).await?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue