/join 'ing for new users code steramlined a bit
*mostly a coding qol change
This commit is contained in:
parent
04ca53dc79
commit
5652388f4c
@ -1,6 +1,7 @@
|
|||||||
// Handlers for the base auth routes
|
// Handlers for the base auth routes
|
||||||
use crate::users;
|
use crate::users{self, Member};
|
||||||
use std::{error, fmt};
|
use std::{error, fmt};
|
||||||
|
use crate::rand_utils::new_key;
|
||||||
|
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
@ -39,15 +40,19 @@ impl<'r> Responder<'r> for AuthErr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#[post("/login")]
|
#[post("/login")]
|
||||||
pub fn login() {
|
pub fn login() {
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
#[post("/join", data="<data>")]
|
|
||||||
pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<>, AuthErr>{
|
#[post("/join", data="<params>")]
|
||||||
|
pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<User>, AuthErr>{
|
||||||
/*
|
/*
|
||||||
* Requires <code:int> -> body
|
* Requires <code:int> -> body
|
||||||
* Requires <name:string> -> body
|
* Requires <name:string> -> body
|
||||||
|
* Struct JoinParams enforces this for us so if something is missing then rocket should 404
|
||||||
*/
|
*/
|
||||||
const expired: &'static str = "Invite expired";
|
const expired: &'static str = "Invite expired";
|
||||||
const negate: &'static str = "Malformed request";
|
const negate: &'static str = "Malformed request";
|
||||||
@ -58,13 +63,13 @@ pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<>, AuthErr>{
|
|||||||
if let Ok(data) = diesel_result {
|
if let Ok(data) = diesel_result {
|
||||||
match data.uses {
|
match data.uses {
|
||||||
1 ... std::i32::MAX => {
|
1 ... std::i32::MAX => {
|
||||||
let user = users::new_user();
|
let new_user = users::new_member(conn):
|
||||||
// update the uses counter
|
// update the uses counter
|
||||||
diesel::update(users.filter(userid.eq(user.userid)))
|
diesel::update(users.filter(userid.eq(user.userid)))
|
||||||
.set(uses.eq(data.uses - 1))
|
.set(uses.eq(data.uses - 1))
|
||||||
.execute(&conn.0)
|
.execute(&conn.0)
|
||||||
|
|
||||||
AuthResult(Json(user))
|
AuthResult(Json(new_user))
|
||||||
}
|
}
|
||||||
// The invite has been used up and thus should be removed
|
// The invite has been used up and thus should be removed
|
||||||
std::i32::MIN ... 0 => {
|
std::i32::MIN ... 0 => {
|
||||||
@ -81,6 +86,8 @@ pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<>, AuthErr>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
#[post("/leave", data = "<>")]
|
#[post("/leave", data = "<>")]
|
||||||
pub fn leave() {
|
pub fn leave() {
|
||||||
}
|
}
|
||||||
@ -89,4 +96,4 @@ pub fn leave() {
|
|||||||
pub fn close() {
|
pub fn close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
@ -9,9 +9,9 @@ pub fn new_user_id() -> u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_key() -> String { // Returns a random string which we later hash with bcrypt
|
pub fn new_key() -> String {
|
||||||
let mut raw_slice = [0u8; 32];
|
let mut raw_slice = [0u8; 64];
|
||||||
if let Ok(_ignored) = getrandom(&mut raw_slice) {
|
let _ignored = getrandom(&mut raw_slice).unwrap();
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
for i in raw_slice.iter() {
|
for i in raw_slice.iter() {
|
||||||
let mut cv: u8 = *i;
|
let mut cv: u8 = *i;
|
||||||
@ -20,8 +20,4 @@ pub fn new_key() -> String { // Returns a random string which we later hash w
|
|||||||
buf.push(cv as char);
|
buf.push(cv as char);
|
||||||
}
|
}
|
||||||
buf
|
buf
|
||||||
}
|
|
||||||
else {
|
|
||||||
"".into() // like with new_user_id if this really fires we're screwed anyway
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -8,6 +8,9 @@ use diesel::{self, prelude::*};
|
|||||||
use diesel::result::Error;
|
use diesel::result::Error;
|
||||||
|
|
||||||
pub fn create_new_user(conn: DBConn, name: String) -> User {
|
pub fn create_new_user(conn: DBConn, name: String) -> User {
|
||||||
|
/*
|
||||||
|
* Should only eveer be called under good circumstances
|
||||||
|
*/
|
||||||
use schema::users::dsl::*;
|
use schema::users::dsl::*;
|
||||||
use diesel::result::Error;
|
use diesel::result::Error;
|
||||||
use crate::models::InsertableUser;
|
use crate::models::InsertableUser;
|
||||||
|
Loading…
Reference in New Issue
Block a user