-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 4 commits
1e0f882
7cff3f4
0ffb42b
0f15f74
0207f37
7c6f54b
62c462a
3c02420
7e85c56
3670eb7
daa4204
d1f58d0
b512044
55111cf
316eb68
7b86eaf
b3b113a
84a0c75
c858d4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
|
||
package com.google.sps.servlets; | ||
|
||
import com.google.gson.Gson; | ||
import org.javatuples.Pair; | ||
import com.google.appengine.api.users.UserService; | ||
import com.google.appengine.api.users.UserServiceFactory; | ||
import java.io.IOException; | ||
|
@@ -27,16 +29,25 @@ public class AuthenticationServlet extends HttpServlet { | |
|
||
@Override | ||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
response.setContentType("text/html"); | ||
response.setContentType("application/json"); | ||
UserService userService = UserServiceFactory.getUserService(); | ||
String redirectUrl = "/index.html"; | ||
if (userService.isUserLoggedIn()) { | ||
String userEmail = userService.getCurrentUser().getEmail(); | ||
String logoutUrl = userService.createLogoutURL(redirectUrl); | ||
response.getWriter().println("<p>Logout <a href=\"" + logoutUrl + "\">here</a>."); | ||
Pair<String, String> userInfo = new Pair<>(userEmail, logoutUrl); | ||
sendJson(response, userInfo); | ||
} else { | ||
String loginUrl = userService.createLoginURL(redirectUrl); | ||
response.getWriter().println("<p>Login <a href=\"" + loginUrl + "\">here</a>."); | ||
Pair<String, String> userInfo = new Pair<>("N/A", loginUrl); | ||
sendJson(response, userInfo); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a good candidate for the return early pattern (https://www.itamarweiss.com/personal/2018/02/28/return-early-pattern.html) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an interesting pattern, I've never heard of it. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
private void sendJson(HttpServletResponse response, Pair<String, String> userInfo) throws IOException { | ||
Gson gson = new Gson(); | ||
String json = gson.toJson(userInfo); | ||
response.getWriter().println(json); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: If you are using this in more than one class, you could consider creating a utility class to share this logic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gbuenoandrade I just looked into this, I think this is a great idea. However, one of the sendJson() is defined for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know this, thanks! I've added it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
|
||
package com.google.sps.servlets; | ||
|
||
import com.google.appengine.api.users.UserService; | ||
import com.google.appengine.api.users.UserServiceFactory; | ||
import com.google.appengine.api.datastore.DatastoreService; | ||
import com.google.appengine.api.datastore.DatastoreServiceFactory; | ||
import com.google.appengine.api.datastore.Entity; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: imported names order There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing this out, I just installed a VSCode extension to sort these. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
@@ -29,17 +31,20 @@ public class NewCommentServlet extends HttpServlet { | |
|
||
@Override | ||
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
UserService userService = UserServiceFactory.getUserService(); | ||
String email = userService.getCurrentUser().getEmail(); | ||
String comment = request.getParameter("comment"); | ||
long timestamp = System.currentTimeMillis(); | ||
Entity commentEntity = createEntity(comment, timestamp); | ||
Entity commentEntity = createEntity(comment, timestamp, email); | ||
putEntity(commentEntity); | ||
response.sendRedirect("/index.html"); | ||
} | ||
|
||
private Entity createEntity(String comment, long timestamp) { | ||
private Entity createEntity(String comment, long timestamp, String email) { | ||
Entity entity = new Entity("Comment"); | ||
entity.setProperty("comment", comment); | ||
entity.setProperty("timestamp", timestamp); | ||
entity.setProperty("email", email); | ||
return entity; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,9 +146,30 @@ function drawCoronavirusChart() { | |
|
||
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'; | ||
} | ||
const json = await response.json(); | ||
const userEmail = getEmail(json); | ||
const redirectUrl = getUrl(json); | ||
if (userEmail == 'N/A') { | ||
const html = loginHtml(redirectUrl); | ||
document.getElementById('login-status').innerHTML = html; | ||
} else { | ||
const html = logoutHtml(userEmail, redirectUrl); | ||
document.getElementById('login-status').innerHTML = html; | ||
document.getElementById('comments-form').style.display = "block"; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: Maybe return early There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've implemented this as well, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
function getEmail(json) { | ||
return json.val0; | ||
} | ||
|
||
function getUrl(json) { | ||
return json.val1; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider defining your own type to store user info. That way you will avoid having to reference the generic names defined by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apologies for the delay, I have implemented this! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these helpers add any clarity. You can just access the property directly on the json object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I agree. I've removed them, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
function loginHtml(loginUrl) { | ||
return "<p>Login <a href=\"" + loginUrl + "\">here</a>."; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use a template literal? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
function logoutHtml(userEmail, logoutUrl) { | ||
return "<p>Hi " + userEmail + "! Logout <a href=\"" + logoutUrl + "\">here</a>."; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use a template literal? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Imported names should appear in ASCII sort order (https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out, I just installed a VSCode extension to sort these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RESOLVED