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..99e2b9a --- /dev/null +++ b/src/main/java/com/tritoncubed/chatapp/Login.java @@ -0,0 +1,116 @@ +package com.tritoncubed.chatapp; + +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; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Servlet for the Login page. This interacts with login.jsp to authenticate (needs to be added) + * or create accounts (also needs to be added). If there is an error (there currently aren't + * any means of actually getting an error) the servlet responds with a redirect back to the Login + * page and displays a popup with an error message. + */ +@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"); + List parameterNames = new ArrayList(request.getParameterMap().keySet()); + + // Check to see if it is a new account + boolean newUser = false; + for (String item : parameterNames) { + if (item.equals("isNewAccount")) + newUser = true; + } + + // 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!"); + + // placeholder method - does nothing + createAccount(username, password); + + // 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 + // 'auth' is a placeholder method to check whether the person is authenticated + // (for now, this is always true, so the function doesn't do anything) + 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 with an error message. + else { + session.invalidate(); + request.setAttribute("errorMessage", "Account Authentication Failed"); + request.getRequestDispatcher("/login.jsp").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.jsp b/src/main/webapp/login.jsp new file mode 100644 index 0000000..137c70c --- /dev/null +++ b/src/main/webapp/login.jsp @@ -0,0 +1,94 @@ + + +<%@ page language="java" contentType="text/html; charset=utf-8" + pageEncoding="utf-8"%> + +Log in + + + + + + + + + + + + + + <% if(null != request.getAttribute("errorMessage")) { %> +
+
+
+
+
+ + +
+
+ +
+

Please try signing in again or create a new account.

+
+
+
+
+
+
+ <%}%> + +
+
+
+
+

Login

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

+ ©Placeholder. All rights reserved. +

+
+
+ + + +