package nl.tudelft.ewi.dea.jaxrs.html;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import nl.tudelft.ewi.dea.BuildInfo;
import nl.tudelft.ewi.dea.jaxrs.html.utils.Renderer;

import com.google.inject.persist.Transactional;
import com.google.inject.servlet.RequestScoped;

@RequestScoped
@Path("login")
@Produces(MediaType.TEXT_HTML)
public class LoginPage {

	private final Renderer renderer;
	private final BuildInfo buildInfo;

	@Inject
	public LoginPage(Renderer renderer, BuildInfo buildInfo) {
		this.renderer = renderer;
		this.buildInfo = buildInfo;
	}

	@GET
	@Transactional
	public String servePage() {
		return renderer
				.setValue("buildInfo", buildInfo)
				.render("login.tpl");
	}

}

@@ -33,16 +33,38 @@
 .html;%0A%0A
+import java.net.URI;%0A%0A
 import j
@@ -195,16 +195,50 @@
 diaType;
+%0Aimport javax.ws.rs.core.Response;
 %0A%0Aimport
@@ -320,16 +320,69 @@
 enderer;
+%0Aimport nl.tudelft.ewi.dea.security.SecurityProvider;
 %0A%0Aimport
@@ -633,77 +633,210 @@
 fo;%0A
-%0A%09@Inject%0A%09public LoginPage(Renderer renderer, BuildInfo buildInfo) %7B
+%09private final SecurityProvider securityProvider;%0A%0A%09@Inject%0A%09public LoginPage(SecurityProvider securityProvider,%0A%09%09%09Renderer renderer, BuildInfo buildInfo) %7B%0A%0A%09%09this.securityProvider = securityProvider;
 %0A%09%09t
@@ -928,14 +928,16 @@
 lic 
-String
+Response
  ser
@@ -953,15 +953,152 @@
 %7B%0A%09%09
-return 
+if (securityProvider.getSubject().isAuthenticated()) %7B%0A%09%09%09return Response.seeOther(URI.create(%22/dashboard%22)).build();%0A%09%09%7D%0A%0A%09%09return Response.ok(
 rend
@@ -1164,16 +1164,24 @@
 in.tpl%22)
+).build()
 ;%0A%09%7D%0A
-%0A
 %7D%0A

Fix issue-45: When already logged in, the login page should redirect to the dashboard.