register with optional role in the post request
This commit is contained in:
@@ -14,6 +14,7 @@ message RegisterRequest {
|
|||||||
string email = 2;
|
string email = 2;
|
||||||
string password = 3;
|
string password = 3;
|
||||||
string password_confirmation = 4;
|
string password_confirmation = 4;
|
||||||
|
string role = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message AuthResponse {
|
message AuthResponse {
|
||||||
|
|||||||
Binary file not shown.
@@ -9,6 +9,8 @@ pub struct RegisterRequest {
|
|||||||
pub password: ::prost::alloc::string::String,
|
pub password: ::prost::alloc::string::String,
|
||||||
#[prost(string, tag = "4")]
|
#[prost(string, tag = "4")]
|
||||||
pub password_confirmation: ::prost::alloc::string::String,
|
pub password_confirmation: ::prost::alloc::string::String,
|
||||||
|
#[prost(string, tag = "5")]
|
||||||
|
pub role: ::prost::alloc::string::String,
|
||||||
}
|
}
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct AuthResponse {
|
pub struct AuthResponse {
|
||||||
|
|||||||
37
server/src/auth/docs/role_added.txt
Normal file
37
server/src/auth/docs/role_added.txt
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
❯ grpcurl -plaintext -d '{
|
||||||
|
"username": "testuser_default_role",
|
||||||
|
"email": "default@example.com",
|
||||||
|
"password": "password",
|
||||||
|
"password_confirmation": "password"
|
||||||
|
}' localhost:50051 multieko2.auth.AuthService/Register
|
||||||
|
{
|
||||||
|
"id": "73017288-af41-49d8-b4cb-2ac2109b38a1",
|
||||||
|
"username": "testuser_default_role",
|
||||||
|
"email": "default@example.com",
|
||||||
|
"role": "accountant"
|
||||||
|
}
|
||||||
|
❯ grpcurl -plaintext -d '{
|
||||||
|
"username": "testuser_admin_role",
|
||||||
|
"email": "admin@example.com",
|
||||||
|
"password": "password",
|
||||||
|
"password_confirmation": "password",
|
||||||
|
"role": "admin"
|
||||||
|
}' localhost:50051 multieko2.auth.AuthService/Register
|
||||||
|
{
|
||||||
|
"id": "9437e318-818b-4c34-822d-c2d6601b835e",
|
||||||
|
"username": "testuser_admin_role",
|
||||||
|
"email": "admin@example.com",
|
||||||
|
"role": "admin"
|
||||||
|
}
|
||||||
|
❯ grpcurl -plaintext -d '{
|
||||||
|
"username": "testuser_invalid_role",
|
||||||
|
"email": "invalid@example.com",
|
||||||
|
"password": "password",
|
||||||
|
"password_confirmation": "password",
|
||||||
|
"role": "invalid_role_name"
|
||||||
|
}' localhost:50051 multieko2.auth.AuthService/Register
|
||||||
|
ERROR:
|
||||||
|
Code: InvalidArgument
|
||||||
|
Message: Invalid role specified: 'invalid_role_name'
|
||||||
|
╭─ ~/Doc/pr/multieko2/server main +3 !1
|
||||||
|
╰─
|
||||||
@@ -5,6 +5,8 @@ use common::proto::multieko2::auth::{RegisterRequest, AuthResponse};
|
|||||||
use crate::db::PgPool;
|
use crate::db::PgPool;
|
||||||
use crate::auth::models::AuthError;
|
use crate::auth::models::AuthError;
|
||||||
|
|
||||||
|
const DEFAULT_ROLE: &str = "accountant";
|
||||||
|
|
||||||
pub async fn register(
|
pub async fn register(
|
||||||
pool: &PgPool,
|
pool: &PgPool,
|
||||||
payload: RegisterRequest,
|
payload: RegisterRequest,
|
||||||
@@ -18,20 +20,28 @@ pub async fn register(
|
|||||||
let password_hash = hash(payload.password, DEFAULT_COST)
|
let password_hash = hash(payload.password, DEFAULT_COST)
|
||||||
.map_err(|e| Status::internal(AuthError::HashingError(e.to_string()).to_string()))?;
|
.map_err(|e| Status::internal(AuthError::HashingError(e.to_string()).to_string()))?;
|
||||||
|
|
||||||
|
let role_to_insert = if payload.role.is_empty() { DEFAULT_ROLE } else { &payload.role };
|
||||||
|
|
||||||
// Insert user
|
// Insert user
|
||||||
let user = sqlx::query!(
|
let user = sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO users (username, email, password_hash, role)
|
INSERT INTO users (username, email, password_hash, role)
|
||||||
VALUES ($1, $2, $3, 'accountant')
|
VALUES ($1, $2, $3, $4)
|
||||||
RETURNING id, username, email, role
|
RETURNING id, username, email, role
|
||||||
"#,
|
"#,
|
||||||
payload.username,
|
payload.username,
|
||||||
payload.email,
|
payload.email,
|
||||||
password_hash
|
password_hash,
|
||||||
|
role_to_insert
|
||||||
)
|
)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
|
if let Some(db_err) = e.as_database_error() {
|
||||||
|
if db_err.constraint() == Some("valid_roles") {
|
||||||
|
return Status::invalid_argument(format!("Invalid role specified: '{}'", role_to_insert));
|
||||||
|
}
|
||||||
|
}
|
||||||
if e.to_string().contains("duplicate key") {
|
if e.to_string().contains("duplicate key") {
|
||||||
Status::already_exists(AuthError::UserExists.to_string())
|
Status::already_exists(AuthError::UserExists.to_string())
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user