forked from jmug/stoatchat
Begin work on proper database schema.
This commit is contained in:
@@ -23,3 +23,5 @@ pub fn get_db() -> Database {
|
||||
pub fn get_collection(collection: &str) -> Collection {
|
||||
get_db().collection(collection)
|
||||
}
|
||||
|
||||
pub mod user;
|
||||
29
src/database/user.rs
Normal file
29
src/database/user.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use serde::{ Deserialize, Serialize };
|
||||
use bson::UtcDateTime;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct UserEmailVerification {
|
||||
pub verified: bool,
|
||||
pub target: Option<String>,
|
||||
pub expiry: Option<UtcDateTime>,
|
||||
pub rate_limit: Option<UtcDateTime>,
|
||||
pub code: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct UserRelationship {
|
||||
pub id: String,
|
||||
pub status: u8,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct User {
|
||||
#[serde(rename = "_id")]
|
||||
pub id: String,
|
||||
pub email: String,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub access_token: Option<String>,
|
||||
pub email_verification: UserEmailVerification,
|
||||
pub relations: Option<Vec<UserRelationship>>,
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::database;
|
||||
use crate::email;
|
||||
|
||||
use bson::{ bson, doc, Bson::UtcDatetime, from_bson};
|
||||
use rand::{ Rng, distributions::Alphanumeric };
|
||||
use rocket_contrib::json::{ Json, JsonValue };
|
||||
use bson::{ bson, doc, Bson::UtcDatetime };
|
||||
use serde::{ Serialize, Deserialize };
|
||||
use validator::validate_email;
|
||||
use bcrypt::{ hash, verify };
|
||||
@@ -243,16 +243,18 @@ pub fn login(info: Json<Login>) -> JsonValue {
|
||||
|
||||
if let Some(u) =
|
||||
col.find_one(doc! { "email": info.email.clone() }, None).expect("Failed user lookup") {
|
||||
match verify(info.password.clone(), u.get_str("password").expect("DOC[password]"))
|
||||
let user: database::user::User = from_bson(bson::Bson::Document(u)).expect("Failed to unwrap user.");
|
||||
|
||||
match verify(info.password.clone(), &user.password)
|
||||
.expect("Failed to check hash of password.") {
|
||||
true => {
|
||||
let token =
|
||||
match u.get_str("access_token") {
|
||||
Ok(t) => t.to_string(),
|
||||
Err(_) => {
|
||||
match user.access_token {
|
||||
Some(t) => t.to_string(),
|
||||
None => {
|
||||
let token = gen_token(92);
|
||||
col.update_one(
|
||||
doc! { "_id": u.get_str("_id").expect("DOC[id]") },
|
||||
doc! { "_id": &user.id },
|
||||
doc! { "$set": { "access_token": token.clone() } },
|
||||
None
|
||||
).expect("Failed to update user object");
|
||||
|
||||
@@ -41,9 +41,12 @@ pub fn channel(user: User, target: Channel) -> Option<JsonValue> {
|
||||
return None
|
||||
}
|
||||
|
||||
let Channel ( id, channel_type, doc ) = target;
|
||||
|
||||
Some(
|
||||
json!({
|
||||
"aa": "b"
|
||||
"id": id.to_string(),
|
||||
"type": channel_type as u8
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user