Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Authentication extra features to Comments tab #41

Merged
merged 19 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
Expand All @@ -34,19 +33,13 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
if (userService.isUserLoggedIn()) {
String userEmail = userService.getCurrentUser().getEmail();
String logoutUrl = userService.createLogoutURL(redirectUrl);
Pair userInfo = new Pair(userEmail, logoutUrl);
sendJson(response, userInfo);
UserInfo userInfo = new UserInfo(userEmail, logoutUrl, true);
JsonUtil.sendJson(response, userInfo);
return;
}
String loginUrl = userService.createLoginURL(redirectUrl);
Pair userInfo = new Pair("N/A", loginUrl);
sendJson(response, userInfo);
}

private void sendJson(HttpServletResponse response, Pair userInfo) throws IOException {
Gson gson = new Gson();
String json = gson.toJson(userInfo);
response.getWriter().println(json);
UserInfo userInfo = new UserInfo("", loginUrl, false);
JsonUtil.sendJson(response, userInfo);
}

}
29 changes: 29 additions & 0 deletions portfolio/src/main/java/com/google/sps/servlets/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.gson.Gson;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

public final class JsonUtil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Public classes and methods should have comments.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Added the relevant javadoc-style comments for this class. Will go back to the other classes unrelated to this PR here #59

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RESOLVED


public static void sendJson(HttpServletResponse response, Object object) throws IOException {
Gson gson = new Gson();
String json = gson.toJson(object);
response.getWriter().println(json);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.gson.Gson;
import com.google.appengine.api.datastore.Query;
import java.io.IOException;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
Expand All @@ -39,7 +38,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
PreparedQuery results = prepareQuery();
int max = getMax(request);
List<String> comments = getCommentsToDisplay(results, max);
sendJson(response, comments);
JsonUtil.sendJson(response, comments);
}

private PreparedQuery prepareQuery() {
Expand All @@ -66,11 +65,5 @@ private List<String> getCommentsToDisplay(PreparedQuery results, int max) {
}
return comments;
}

private void sendJson(HttpServletResponse response, List<String> comments) throws IOException {
Gson gson = new Gson();
response.setContentType("application/json;");
response.getWriter().println(gson.toJson(comments));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@

package com.google.sps.servlets;

public class Pair {
public class UserInfo {

private String email;
private String url;
private String loginOrLogoutUrl;
private boolean loggedIn;

Pair(String email, String url) {
UserInfo(String email, String loginOrLogoutUrl, boolean loggedIn) {
this.email = email;
this.url = url;
this.loginOrLogoutUrl = loginOrLogoutUrl;
this.loggedIn = loggedIn;
}

public String getEmail(){
return email;
}

public String getUrl(){
return url;
public String getLoginOrLogoutUrl(){
return loginOrLogoutUrl;
}

public boolean isLoggedIn() {
return loggedIn;
}

}
15 changes: 4 additions & 11 deletions portfolio/src/main/webapp/libraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ function drawCoronavirusChart() {
async function getLoginStatus() {
const response = await fetch('/authentication');
const json = await response.json();
const userEmail = getEmail(json);
const redirectUrl = getUrl(json);
if (userEmail == 'N/A') {
const userEmail = json["email"];
const redirectUrl = json["loginOrLogoutUrl"];
const loggedIn = json["loggedIn"];
if (!loggedIn) {
const html = loginHtml(redirectUrl);
document.getElementById('login-status').innerHTML = html;
return;
Expand All @@ -159,14 +160,6 @@ async function getLoginStatus() {
document.getElementById('comments-form').style.display = "block";
}

function getEmail(json) {
return json["email"];
}

function getUrl(json) {
return json["url"];
}

function loginHtml(loginUrl) {
return `<p>Login <a href=\ ${loginUrl} \>here</a>.`;
}
Expand Down