Add login

This commit is contained in:
Kirigaya Kazuto 2017-11-19 15:36:43 +08:00
parent 36466cec54
commit c9b4cc28f0
3 changed files with 90 additions and 2 deletions

@ -1 +1 @@
Subproject commit 8a274d862c609a734f5c287e70af3e6717e011f9
Subproject commit d6d82ae0d96c0c0c0ef800dbe6aa6871170b6236

@ -1 +1 @@
Subproject commit a5dcc01d2f96a894c358a2e40b6566ac796b0aa2
Subproject commit 5de3e0c2954ad4051e5b794453015457b7f0b459

View File

@ -0,0 +1,88 @@
#include "Request.h"
#include "Response.h"
#include "Session.h"
#include "Util.h"
#include "json.hpp"
using namespace std;
using json=nlohmann::json;
int main()
{
Request req;
Session se(req);
Response res;
json j;
auto jsonfail=[&](int errcode,const std::string& errmsg)
{
j["success"]=0;
j["errcode"]=errcode;
j["errmsg"]=errmsg;
};
if(req.requestMethod=="POST")
{
if(req.post["username"].empty() ||
req.post["password"].empty())
{
jsonfail(2,"Missing Parameter");
}
else
{
DBInfo db;
if(db.readConfig()<0)
{
jsonfail(3,"Failed to read configure");
}
else
{
string uname=req.post["username"];
string upass=req.post["password"];
MySQLConn conn;
if(db.connectProxy(conn)<0)
{
jsonfail(4,"Failed to connect DB");
}
else
{
bool authpass=false;
if(conn.exec(make_str("select count(username) from bs_user where username='",uname,"' and password='",upass,"'"),
[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
if(string(val[0])=="1")
{
authpass=true;
}
});
})<0)
{
jsonfail(5,"Failed to execute SQL.");
}
else
{
/// Try to mark session logged in
if(se.setUser(uname)<0)
{
jsonfail(6,"Failed to set session.");
}
else
{
j["success"]=1;
}
}
}
}
}
}
else
{
jsonfail(1,"Request Method Not Supported");
}
res.content.append(j.dump());
res.show();
return 0;
}