2023-06-05 16:48:33 +00:00
|
|
|
module requests.authorization;
|
|
|
|
|
|
|
|
import vibe.vibe;
|
|
|
|
import response;
|
|
|
|
import structures;
|
|
|
|
import singlog;
|
|
|
|
|
|
|
|
void login(HTTPServerRequest req, HTTPServerResponse res, AuthData serverAuthData) {
|
|
|
|
auto userAuthData = deserializeJson!AuthData(req.json);
|
|
|
|
|
|
|
|
if (!(serverAuthData.login == userAuthData.login &&
|
|
|
|
serverAuthData.password == userAuthData.password)) {
|
2023-06-08 13:56:05 +00:00
|
|
|
log.i(req.clientAddress.toAddressString() ~ ": Данные авторизации не верны");
|
2023-06-05 16:48:33 +00:00
|
|
|
res.send(
|
|
|
|
true,
|
|
|
|
"Данные авторизации не верны"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto user = UserData(true);
|
|
|
|
|
|
|
|
req.session = res.startSession();
|
|
|
|
req.session.set!UserData("user", user);
|
|
|
|
|
2023-06-08 13:56:05 +00:00
|
|
|
log.i(req.clientAddress.toAddressString() ~ ": Вход в систему");
|
2023-06-05 16:48:33 +00:00
|
|
|
|
|
|
|
res.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void logout(HTTPServerRequest req, HTTPServerResponse res) {
|
|
|
|
req.session.set!UserData("user", UserData.init);
|
|
|
|
res.terminateSession();
|
|
|
|
|
2023-06-08 13:56:05 +00:00
|
|
|
log.i(req.clientAddress.toAddressString() ~ ": Выход из системы");
|
2023-06-05 16:48:33 +00:00
|
|
|
|
|
|
|
res.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkAuth(HTTPServerRequest req) {
|
|
|
|
if (req.session)
|
|
|
|
return req.session.get!UserData("user").login;
|
|
|
|
|
2023-06-08 13:56:05 +00:00
|
|
|
log.d(req.clientAddress.toAddressString() ~ ": Отсутствует авторизация");
|
2023-06-05 16:48:33 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|