From b96ccfcf6348c37473eecf7ef5f6ef88874ea171 Mon Sep 17 00:00:00 2001 From: Sean Boerhout Date: Sun, 14 Nov 2021 13:17:06 -0800 Subject: [PATCH 1/5] Set up login servlet and basic login page. Need to fix new user registration in the login servlet still. --- .../java/com/tritoncubed/chatapp/Login.java | 102 ++++++++++++++++++ src/main/webapp/index.html | 4 + src/main/webapp/login.html | 63 +++++++++++ 3 files changed, 169 insertions(+) create mode 100644 src/main/java/com/tritoncubed/chatapp/Login.java create mode 100644 src/main/webapp/login.html diff --git a/src/main/java/com/tritoncubed/chatapp/Login.java b/src/main/java/com/tritoncubed/chatapp/Login.java new file mode 100644 index 0000000..0ae1599 --- /dev/null +++ b/src/main/java/com/tritoncubed/chatapp/Login.java @@ -0,0 +1,102 @@ +package com.tritoncubed.chatapp; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Collections; + +import javax.servlet.http.*; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Servlet implementation class Login + * + */ +@WebServlet("/Login") +public class Login extends HttpServlet { + private static final long serialVersionUID = 1L; + + /** + * @see HttpServlet#HttpServlet() + */ + public Login() { + super(); + // TODO Auto-generated constructor stub + } + + private boolean auth(String userId, String password) { + /* + Edit this method to check for correct Usernames and Passwords in DynamoDB + */ + return true; + } + + private boolean createAccount(String userId, String password) { + /* + Edit this method to add a new account with given username and password in DynamoDB + */ + return true; // True if successful + } + /** + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse + * response) + */ + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + // response.getWriter().append("Served at: ").append(request.getContextPath()); + + String username = request.getParameter("username"); + String password = request.getParameter("userPassword"); + HttpSession session = request.getSession(); + + // Check to see if it is a new account + boolean newUser = false; + for (String item : Collections.list(request.getParameterNames())) { + if (item == "isNewAccount") + newUser = true; + } + System.out.println(Collections.list(request.getParameterNames())); + + + + // Check for login status! + + // If new user, record a new user + if (newUser) { + System.out.println("You are a new user!"); + // Register/record a new user + + // For now, redirect to the chat page + request.getRequestDispatcher("/index.html").forward(request, response); + } + + // If they aren't a new user, then try to authenticate + // Placeholder method to check whether the person is authenticated (for now, this is always true) + else if (auth(username, password)) { + + System.out.println("Welcome back!"); + + request.getRequestDispatcher("/index.html").forward(request, response); + } + + // Else, you're a stranger! Redirect to login page. + else { + request.getRequestDispatcher("/login.html").forward(request, response); + } + + + + } + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse + * response) + */ + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + assert true; + } + +} \ No newline at end of file diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 18afafe..87d1add 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -1,3 +1,4 @@ + @@ -6,5 +7,8 @@ +

Index.html

+ + \ No newline at end of file diff --git a/src/main/webapp/login.html b/src/main/webapp/login.html new file mode 100644 index 0000000..258d95c --- /dev/null +++ b/src/main/webapp/login.html @@ -0,0 +1,63 @@ + + + +Page Title + + + + + + +
+
+
+
+

Login

+
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + + +
+

+ ©Placeholder. All rights reserved. +

+
+
+ + + + + + + From 6d43804262b492cf273ba5921d391ba09faf9d45 Mon Sep 17 00:00:00 2001 From: Sean Boerhout Date: Sun, 14 Nov 2021 22:11:47 -0800 Subject: [PATCH 2/5] Fixed new user checking and cleanup --- .../java/com/tritoncubed/chatapp/Login.java | 22 +++++++++++++------ src/main/webapp/login.html | 8 +++---- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/tritoncubed/chatapp/Login.java b/src/main/java/com/tritoncubed/chatapp/Login.java index 0ae1599..d5b5f37 100644 --- a/src/main/java/com/tritoncubed/chatapp/Login.java +++ b/src/main/java/com/tritoncubed/chatapp/Login.java @@ -2,7 +2,9 @@ import java.io.IOException; import java.io.PrintWriter; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import javax.servlet.http.*; import javax.servlet.ServletException; @@ -50,25 +52,33 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t String username = request.getParameter("username"); String password = request.getParameter("userPassword"); - HttpSession session = request.getSession(); + List parameterNames = new ArrayList(request.getParameterMap().keySet()); // Check to see if it is a new account boolean newUser = false; - for (String item : Collections.list(request.getParameterNames())) { - if (item == "isNewAccount") + for (String item : parameterNames) { + if (item.equals("isNewAccount")) newUser = true; } System.out.println(Collections.list(request.getParameterNames())); + // Record user credentials for this session + HttpSession session = request.getSession(); + session.setAttribute("username", username); + session.setAttribute("password", password); + // Check for login status! // If new user, record a new user if (newUser) { System.out.println("You are a new user!"); - // Register/record a new user + + // placeholder method - does nothing + createAccount(username, password); + // For now, redirect to the chat page request.getRequestDispatcher("/index.html").forward(request, response); } @@ -79,7 +89,7 @@ else if (auth(username, password)) { System.out.println("Welcome back!"); - request.getRequestDispatcher("/index.html").forward(request, response); + request.getRequestDispatcher("/index.html").forward(request, response); } // Else, you're a stranger! Redirect to login page. @@ -87,8 +97,6 @@ else if (auth(username, password)) { request.getRequestDispatcher("/login.html").forward(request, response); } - - } /** diff --git a/src/main/webapp/login.html b/src/main/webapp/login.html index 258d95c..5151b32 100644 --- a/src/main/webapp/login.html +++ b/src/main/webapp/login.html @@ -11,12 +11,12 @@
-
+

Login

-