Add hCaptcha support.
This commit is contained in:
@@ -2,6 +2,7 @@ use super::Response;
|
||||
use crate::database;
|
||||
use crate::email;
|
||||
use crate::util::gen_token;
|
||||
use crate::util::captcha;
|
||||
|
||||
use bcrypt::{hash, verify};
|
||||
use chrono::prelude::*;
|
||||
@@ -17,6 +18,7 @@ pub struct Create {
|
||||
username: String,
|
||||
password: String,
|
||||
email: String,
|
||||
captcha: Option<String>,
|
||||
}
|
||||
|
||||
/// create a new Revolt account
|
||||
@@ -28,6 +30,12 @@ pub struct Create {
|
||||
/// (3) add user and send email verification
|
||||
#[post("/create", data = "<info>")]
|
||||
pub fn create(info: Json<Create>) -> Response {
|
||||
if let Err(error) = captcha::verify(&info.captcha) {
|
||||
return Response::BadRequest(
|
||||
json!({ "error": error })
|
||||
);
|
||||
}
|
||||
|
||||
let col = database::get_collection("users");
|
||||
|
||||
if info.username.len() < 2 || info.username.len() > 32 {
|
||||
@@ -150,6 +158,7 @@ pub fn verify_email(code: String) -> Response {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Resend {
|
||||
email: String,
|
||||
captcha: Option<String>,
|
||||
}
|
||||
|
||||
/// resend a verification email
|
||||
@@ -158,6 +167,12 @@ pub struct Resend {
|
||||
/// (3) resend the email
|
||||
#[post("/resend", data = "<info>")]
|
||||
pub fn resend_email(info: Json<Resend>) -> Response {
|
||||
if let Err(error) = captcha::verify(&info.captcha) {
|
||||
return Response::BadRequest(
|
||||
json!({ "error": error })
|
||||
);
|
||||
}
|
||||
|
||||
let col = database::get_collection("users");
|
||||
|
||||
if let Some(u) = col
|
||||
@@ -218,6 +233,7 @@ pub fn resend_email(info: Json<Resend>) -> Response {
|
||||
pub struct Login {
|
||||
email: String,
|
||||
password: String,
|
||||
captcha: Option<String>,
|
||||
}
|
||||
|
||||
/// login to a Revolt account
|
||||
@@ -226,6 +242,12 @@ pub struct Login {
|
||||
/// (3) return access token
|
||||
#[post("/login", data = "<info>")]
|
||||
pub fn login(info: Json<Login>) -> Response {
|
||||
if let Err(error) = captcha::verify(&info.captcha) {
|
||||
return Response::BadRequest(
|
||||
json!({ "error": error })
|
||||
);
|
||||
}
|
||||
|
||||
let col = database::get_collection("users");
|
||||
|
||||
if let Some(u) = col
|
||||
|
||||
@@ -2,7 +2,7 @@ use super::channel::ChannelType;
|
||||
use super::Response;
|
||||
use crate::database::guild::{fetch_member as get_member, get_invite, Guild, MemberKey};
|
||||
use crate::database::{
|
||||
self, channel::fetch_channel, guild::fetch_guilds, guild::serialise_guilds_with_channels, channel::Channel, Permission, PermissionCalculator, user::User
|
||||
self, channel::fetch_channel, guild::serialise_guilds_with_channels, channel::Channel, Permission, PermissionCalculator, user::User
|
||||
};
|
||||
use crate::notifications::{
|
||||
self,
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
use super::Response;
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use std::env;
|
||||
|
||||
/// root
|
||||
#[get("/")]
|
||||
pub fn root() -> Response {
|
||||
Response::Success(json!({
|
||||
"revolt": "0.2.8",
|
||||
"revolt": "0.2.9",
|
||||
"version": {
|
||||
"major": 0,
|
||||
"minor": 2,
|
||||
"patch": 8
|
||||
"patch": 9
|
||||
},
|
||||
"features": {
|
||||
"captcha": env::var("HCAPTCHA_KEY").is_ok()
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user