summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/db/users.rs21
-rw-r--r--src/routes/home.rs3
-rw-r--r--src/routes/login.rs3
-rw-r--r--src/users.rs4
4 files changed, 14 insertions, 17 deletions
diff --git a/src/db/users.rs b/src/db/users.rs
index 0fc7c64..a5759fd 100644
--- a/src/db/users.rs
+++ b/src/db/users.rs
@@ -1,11 +1,12 @@
use sqlx::{Pool, Postgres};
+use uuid::Uuid;
use crate::users::User;
use crate::Result;
#[derive(Clone)]
pub struct Users(Pool<Postgres>);
-// code code code code code code code code code code code code code code
+
impl Users {
pub fn new(pool: Pool<Postgres>) -> Self {
Self(pool)
@@ -13,7 +14,8 @@ impl Users {
pub async fn create(&self, user: User) -> Result<()> {
sqlx::query!(
- r#"insert into users (username, password, email, bio, site, privacy, admin) values ($1, $2, $3, $4, $5, $6, $7)"#,
+ r#"insert into users (id, username, password, email, bio, site, privacy, admin) values ($1, $2, $3, $4, $5, $6, $7, $8)"#,
+ user.id,
user.username,
user.password,
user.email,
@@ -27,11 +29,11 @@ impl Users {
Ok(())
}
- pub async fn read(&self, user_id: i32) -> Result<User> {
+ pub async fn read(&self, user_id: Uuid) -> Result<User> {
Ok(
sqlx::query_as!(
User,
- "select username, password, email, bio, site, privacy as \"privacy: _\", admin from users where id = $1",
+ "select id, username, password, email, bio, site, privacy as \"privacy: _\", admin from users where id = $1",
user_id
)
.fetch_one(&self.0)
@@ -43,7 +45,7 @@ impl Users {
Ok(
sqlx::query_as!(
User,
- "select username, password, email, bio, site, privacy as \"privacy: _\", admin from users where username = $1",
+ "select id, username, password, email, bio, site, privacy as \"privacy: _\", admin from users where username = $1",
username
)
.fetch_one(&self.0)
@@ -51,15 +53,6 @@ impl Users {
)
}
- pub async fn get_id(&self, username: &str) -> Result<i32> {
- Ok(
- sqlx::query!("select id from users where username = $1", username)
- .fetch_one(&self.0)
- .await?
- .id,
- )
- }
-
pub async fn read_all(&self) -> Result<Vec<User>> {
Ok(sqlx::query_as("select * from users")
.fetch_all(&self.0)
diff --git a/src/routes/home.rs b/src/routes/home.rs
index a43eabc..f2642a5 100644
--- a/src/routes/home.rs
+++ b/src/routes/home.rs
@@ -1,5 +1,6 @@
use actix_session::Session;
use actix_web::{get, web, HttpResponse};
+use uuid::Uuid;
use crate::templates;
use crate::{Pinussy, Result};
@@ -7,7 +8,7 @@ use crate::{Pinussy, Result};
#[get("/")]
async fn get(session: Session, state: web::Data<Pinussy>) -> Result<HttpResponse> {
let username: Option<String>;
- if let Some(user_id) = session.get::<i32>("user_id")? {
+ if let Some(user_id) = session.get::<Uuid>("user_id")? {
username = Some(state.db.users().read(user_id).await?.username)
} else {
username = None
diff --git a/src/routes/login.rs b/src/routes/login.rs
index c6cf077..bd7eaf1 100644
--- a/src/routes/login.rs
+++ b/src/routes/login.rs
@@ -30,8 +30,7 @@ async fn post(
match state.db.users().read_username(&form.username).await {
Ok(user) => {
if user.verify_password(&form.password)? {
- let user_id = state.db.users().get_id(&form.username).await?;
- session.insert("user_id", user_id)?;
+ session.insert("user_id", user.id)?;
return Ok(HttpResponse::SeeOther()
.insert_header((LOCATION, "/"))
.finish());
diff --git a/src/users.rs b/src/users.rs
index 4cf9310..e8b0b67 100644
--- a/src/users.rs
+++ b/src/users.rs
@@ -1,12 +1,14 @@
use bcrypt::hash;
use bcrypt::verify;
use bcrypt::DEFAULT_COST;
+use uuid::Uuid;
use crate::Privacy;
use crate::Result;
#[derive(sqlx::FromRow)]
pub struct User {
+ pub id: Uuid,
pub username: String,
pub password: String,
pub email: Option<String>,
@@ -19,7 +21,9 @@ pub struct User {
impl User {
pub fn new(username: String, password: String) -> Result<Self> {
let password_hash = hash(password, DEFAULT_COST)?;
+ let id = Uuid::new_v4();
Ok(Self {
+ id,
username,
password: password_hash,
email: None,