From 2c553b551c5b864ecf550c0cd6102ae83b7bf44c Mon Sep 17 00:00:00 2001 From: tttzach Date: Thu, 11 Jun 2020 12:41:35 -0400 Subject: [PATCH] Add basic functioning login and logout feature --- .../sps/servlets/AuthenticationServlet.java | 26 +++++-------------- portfolio/src/main/webapp/index.html | 2 +- portfolio/src/main/webapp/libraries.js | 10 +++---- portfolio/src/main/webapp/script.js | 6 ----- 4 files changed, 11 insertions(+), 33 deletions(-) diff --git a/portfolio/src/main/java/com/google/sps/servlets/AuthenticationServlet.java b/portfolio/src/main/java/com/google/sps/servlets/AuthenticationServlet.java index c1a2e79..fd7b30e 100644 --- a/portfolio/src/main/java/com/google/sps/servlets/AuthenticationServlet.java +++ b/portfolio/src/main/java/com/google/sps/servlets/AuthenticationServlet.java @@ -28,29 +28,15 @@ public class AuthenticationServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); - UserService userService = UserServiceFactory.getUserService(); + String redirectUrl = "/index.html"; if (userService.isUserLoggedIn()) { - /* - String userEmail = userService.getCurrentUser().getEmail(); - String urlToRedirectToAfterUserLogsOut = "/"; - String logoutUrl = userService.createLogoutURL(urlToRedirectToAfterUserLogsOut); - - response.getWriter().println("

Hello " + userEmail + "!

"); - response.getWriter().println("

Logout here.

"); - */ - // Create list of podmates - - response.getWriter().println("true"); + String logoutUrl = userService.createLogoutURL(redirectUrl); + response.getWriter().println("

Logout here."); } else { - /* - String urlToRedirectToAfterUserLogsIn = "/"; - String loginUrl = userService.createLoginURL(urlToRedirectToAfterUserLogsIn); - - response.getWriter().println("

Hello stranger.

"); - response.getWriter().println("

Login here.

"); - */ - response.getWriter().println("false"); + String loginUrl = userService.createLoginURL(redirectUrl); + response.getWriter().println("

Login here."); } } + } diff --git a/portfolio/src/main/webapp/index.html b/portfolio/src/main/webapp/index.html index 024ac26..8938dec 100644 --- a/portfolio/src/main/webapp/index.html +++ b/portfolio/src/main/webapp/index.html @@ -19,7 +19,6 @@

definitely not zachary's website.

-

about me

technical

personal @@ -31,6 +30,7 @@

definitely not zachary's website.

x +

Type a comment here: diff --git a/portfolio/src/main/webapp/libraries.js b/portfolio/src/main/webapp/libraries.js index c8dc5cd..db95264 100644 --- a/portfolio/src/main/webapp/libraries.js +++ b/portfolio/src/main/webapp/libraries.js @@ -139,11 +139,9 @@ function drawCoronavirusChart() { async function getLoginStatus() { const response = await fetch('/authentication'); - const responseText = await response.text(); - const loggedIn = (responseText == 'true'); - if (loggedIn) { - document.getElementById('login-status').innerHTML = "

You are logged in."; - } else { - document.getElementById('login-status').innerHTML = "

You are not logged in."; + const responseHtml = await response.text(); + document.getElementById('login-status').innerHTML = responseHtml; + if (responseHtml.includes("Logout")) { + document.getElementById('comments-form').style.display = "block"; } } diff --git a/portfolio/src/main/webapp/script.js b/portfolio/src/main/webapp/script.js index 870e0c1..48a5da5 100644 --- a/portfolio/src/main/webapp/script.js +++ b/portfolio/src/main/webapp/script.js @@ -69,12 +69,6 @@ function getCommentForm() { // Fetches comments from the datastore and adds them to the DOM function loadComments(maxInt) { - /* - fetch('/authentication') - .then(response => (response.text() == 'true')) - .then(loggedIn => { - - })*/ const max = maxInt.toString(); fetch('/list-comments?max=' + max) .then(response => response.json())