daster/source/requests/authorization.d

48 lines
1.3 KiB
D
Raw Normal View History

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)) {
log.i(req.clientAddress.toAddressString() ~ ": Данные авторизации не верны");
res.send(
true,
"Данные авторизации не верны"
);
return;
}
auto user = UserData(true);
req.session = res.startSession();
req.session.set!UserData("user", user);
log.i(req.clientAddress.toAddressString() ~ ": Вход в систему");
res.send();
}
void logout(HTTPServerRequest req, HTTPServerResponse res) {
req.session.set!UserData("user", UserData.init);
res.terminateSession();
log.i(req.clientAddress.toAddressString() ~ ": Выход из системы");
res.send();
}
bool checkAuth(HTTPServerRequest req) {
if (req.session)
return req.session.get!UserData("user").login;
log.d(req.clientAddress.toAddressString() ~ ": Отсутствует авторизация");
return false;
}