Skip to content
Snippets Groups Projects
Commit cabce21f authored by HGEpro's avatar HGEpro
Browse files

switched to form instead of body and fixed server error

parent 60661fbe
No related branches found
No related tags found
No related merge requests found
use reqwest;
use serde::{Serialize, Deserialize};
use serde_json;
use std::collections::HashMap;
const APIURL: &str = "https://librehomework-api.herokuapp.com/";
......@@ -22,16 +22,17 @@ impl std::convert::From<reqwest::Error> for ApiResponse {
#[tauri::command]
pub async fn request(url: &str, method: Option<&str>, body: Option<String>) -> Result<(ApiResponse, u16), String> {
pub async fn request(url: &str, method: Option<&str>, form: Option<HashMap<String, String>>) -> Result<(ApiResponse, u16), String> {
let mut _url = url.to_string();
let client = reqwest::Client::new();
if &url.len() > &8 && &url[0..8] != "https://" {
if !url.contains("https://") {
_url = APIURL.to_owned() + &_url;
}
println!("{}", &_url);
let res = match method {
Some(m) => if m == "POST" {
client.post(_url).json(&body).send().await
client.post(_url).form(&form).send().await
} else {
client.get(_url).send().await
},
......
......@@ -53,5 +53,6 @@ String.prototype.capitalize = function() {
let net = new ServerAPI()
console.log(net)
net.getDailyMessage().then((d) => console.log(d))
net.login("username", "password").then((d) => console.log(d))
export default svapp;
......@@ -2,9 +2,11 @@ import { invoke } from "@tauri-apps/api";
class ApiResponse {
constructor(payload) {
this.status = payload[1];
this.data = data[0];
this.data = payload[0].data;
this.error = payload[0].error;
}
}
......@@ -25,39 +27,38 @@ class ServerAPI {
}
async login(username, password) {
return new ApiResponse(await invoke("request", {"url": "login", "method": "POST", "body": JSON.stringify({
return new ApiResponse(await invoke("request", {"url": "login", "method": "POST", "form": {
"username": username,
"password": password
})}));
}}));
}
async signup(username, password, email=null, discord=null, twitter=null, bio=null) {
return new ApiResponse(await invoke("request", {"url": "signup", "method": "POST", "body": JSON.stringify({
return new ApiResponse(await invoke("request", {"url": "signup", "method": "POST", "form": {
"username": username,
"password": password,
"email": email,
"discord": discord,
"twitter": twitter,
"bio": bio
})}));
}}));
}
async deleteAccount(token) {
return new ApiResponse(await invoke("request", {"url": "remove", "method": "POST", "body": JSON.stringify({
return new ApiResponse(await invoke("request", {"url": "remove", "method": "POST", "form": {
"token": token
})}));
}}));
}
//password or username cannot be changed, althought I might add a way to change it in the future
//server should handle the optional fields but check if at least one param is not null
async editProfile(token, email=null, discord=null, twitter=null, bio=null) {
return new ApiResponse(await invoke("request", {"url": "remove", "method": "POST", "body": JSON.stringify({
return new ApiResponse(await invoke("request", {"url": "remove", "method": "POST", "form": {
"token": token
})}));
}}));
}
}
export default ServerAPI;
\ No newline at end of file
......@@ -66,6 +66,8 @@ async def login(db, username, password):
SELECT username FROM users where password = $1 and username = $2
""", utils.hash(password.encode("utf8")), username)
if not db_username: return False, None
if db_username["username"] == username:
return True, authtoken.generate_token(username, 15) #15 minutes
else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment