Fixed session expiring and added logout page

This commit is contained in:
Norbi Peti 2016-08-01 12:25:41 +02:00
parent 3efa16fe0a
commit a3d5f290a6
4 changed files with 32 additions and 5 deletions

View file

@ -14,8 +14,10 @@
</div>
<div id="sidebar">
<div id="userbox">
Logged in as
<username/>
<p>Logged in as
<username/>
</p>
<a href="/logout">Logout</a>
</div>
</div>
<div id="loginregisterbox">

View file

@ -120,8 +120,8 @@ public class IOHelper {
ZonedDateTime expiretime = ZonedDateTime.now(ZoneId.of("GMT")).plus(Period.of(2, 0, 0));
exchange.getResponseHeaders().add("Set-Cookie",
"user_id=" + user.getId() + "; expires=" + expiretime.format(DateTimeFormatter.RFC_1123_DATE_TIME));
exchange.getResponseHeaders().add("Set-Cookie",
"session_id=" + user.getSessionid() + "; expires=" + expiretime);
exchange.getResponseHeaders().add("Set-Cookie", "session_id=" + user.getSessionid() + "; expires="
+ expiretime.format(DateTimeFormatter.RFC_1123_DATE_TIME));
}
public static void LogoutUser(HttpExchange exchange, User user) {

View file

@ -70,7 +70,7 @@ public class Main {
provider.save(user2);
User loggedinuser = new User();
loggedinuser.setName("NorbiPeti");
loggedinuser.setSessionid("8b148304-5dd6-48dd-a1a3-c8e47bcfc44b");
loggedinuser.setSessionid("093b1395-8c31-4f3b-ba67-828a755af92e");
loggedinuser.setEmail("sznp@asd.com");
convo.getUsers().add(loggedinuser);
loggedinuser.getConversations().add(convo);

View file

@ -0,0 +1,25 @@
package io.github.norbipeti.chat.server.page;
import java.io.IOException;
import com.sun.net.httpserver.HttpExchange;
import io.github.norbipeti.chat.server.IOHelper;
import io.github.norbipeti.chat.server.db.domain.User;
public class LogoutPage extends Page {
@Override
public String GetName() {
return "logout";
}
@Override
public void handlePage(HttpExchange exchange) throws IOException {
User user = IOHelper.GetLoggedInUser(exchange);
if (user != null)
IOHelper.LogoutUser(exchange, user);
IOHelper.Redirect("/", exchange);
}
}