package com.google.sps.servlets;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/auth")
public class AuthenticationServlet extends HttpServlet {
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");

    UserService userService = UserServiceFactory.getUserService();
    if (userService.isUserLoggedIn()) {
      String userEmail = userService.getCurrentUser().getEmail();
      String urlToRedirectToAfterUserLogsOut = "/";
      String logoutUrl = userService.createLogoutURL(urlToRedirectToAfterUserLogsOut);

      response.getWriter().println("<p>Hello " + userEmail + "!</p>");
      response.getWriter().println("<p>Logout <a href=\"" + logoutUrl + "\">here</a>.</p>");
    } else {
      String urlToRedirectToAfterUserLogsIn = "/";
      String loginUrl = userService.createLoginURL(urlToRedirectToAfterUserLogsIn);

      response.getWriter().println("<p>Hello stranger.</p>");
      response.getWriter().println("<p>Login <a href=\"" + loginUrl + "\">here</a>.</p>");
    }
  }
}

@@ -341,16 +341,137 @@
 ponse;%0A%0A
+/**%0A*AuthenticationServlet allows users to log in and out using their email, and saves the current status of the user%0A*/%0A
 @WebServ
@@ -692,16 +692,50 @@
 /html%22);
+%0A    String urlToRedirectTo = %22/%22;
 %0A%0A    Us
@@ -905,60 +905,8 @@
 ();%0A
-      String urlToRedirectToAfterUserLogsOut = %22/%22;%0A
     
@@ -973,24 +973,8 @@
 ctTo
-AfterUserLogsOut
 );%0A%0A
@@ -1154,59 +1154,8 @@
 e %7B%0A
-      String urlToRedirectToAfterUserLogsIn = %22/%22;%0A
     
@@ -1220,23 +1220,8 @@
 ctTo
-AfterUserLogsIn
 );%0A%0A

use same login/logout redirect