Skip to content

Commit

Permalink
Merge pull request #40 from tttzach/LibrariesAuthentication
Browse files Browse the repository at this point in the history
Add Authentication feature to Comments tab
  • Loading branch information
tttzach authored Jun 17, 2020
2 parents 24f5452 + 81554fb commit 950bf44
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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("/authentication")
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 logoutUrl = userService.createLogoutURL(redirectUrl);
response.getWriter().println("<p>Logout <a href=\"" + logoutUrl + "\">here</a>.");
} else {
String loginUrl = userService.createLoginURL(redirectUrl);
response.getWriter().println("<p>Login <a href=\"" + loginUrl + "\">here</a>.");
}
}

}
27 changes: 15 additions & 12 deletions portfolio/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="script.js"></script>
<script src="libraries.js"></script>
<body onload="getGreeting(); createMap();">
<body onload="getGreeting(); createMap(); getLoginStatus();">
<!-- Two-column layout -->
<div class="row">
<div class="column-left">
Expand All @@ -30,17 +30,20 @@ <h1>definitely not zachary's website.</h1>
<div class="row">
<div id="comments" class="containerTab">
<span onclick="this.parentElement.style.display='none'" class="closebtn">x</span>
<form action="/new-comment" method="POST">
<p>Type a comment here: <input type="text" name="comment"><input type="submit"/>
</form>
<p>Load how many comments: <input type="int" id="max" name="max" value="" onchange="loadComments(this.value)">
<br/><br/>
<div id="comments-list"></div>
<br/><br/>
<form action="/delete-comment" method="POST">
<input type="submit" name="delete" value="Delete" />
</form>
<br/><br/>
<div id="login-status"></div>
<div id="comments-form">
<form action="/new-comment" method="POST">
<p>Type a comment here: <input type="text" name="comment"><input type="submit"/>
</form>
<p>Load how many comments: <input type="int" id="max" name="max" value="" onchange="loadComments(this.value)">
<br/><br/>
<div id="comments-list"></div>
<br/><br/>
<form action="/delete-comment" method="POST">
<input type="submit" name="delete" value="Delete" />
</form>
<br/><br/>
</div>
</div>
<div id="about-me" class="containerTab">
<span onclick="this.parentElement.style.display='none'" class="closebtn">x</span>
Expand Down
4 changes: 4 additions & 0 deletions portfolio/src/main/webapp/libraries.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
width: 700px;
display: inline-block;
}

#comments-form {
display: none;
}
9 changes: 9 additions & 0 deletions portfolio/src/main/webapp/libraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,12 @@ function drawCoronavirusChart() {
chart.draw(data, options);
});
}

async function getLoginStatus() {
const response = await fetch('/authentication');
const responseHtml = await response.text();
document.getElementById('login-status').innerHTML = responseHtml;
if (responseHtml.includes('Logout')) {
document.getElementById('comments-form').style.display = 'block';
}
}

0 comments on commit 950bf44

Please sign in to comment.