Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
116 changes: 116 additions & 0 deletions src/main/java/com/tritoncubed/chatapp/Login.java
Original file line number Diff line number Diff line change
@@ -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<String> parameterNames = new ArrayList<String>(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;
}

}
4 changes: 4 additions & 0 deletions src/main/webapp/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Expand All @@ -6,5 +7,8 @@
</head>
<body>
<!-- This is where the main page will be implemented -->
<p>Index.html</p>


</body>
</html>
94 changes: 94 additions & 0 deletions src/main/webapp/login.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<!-- Login page that interacts with Login.java (Login servlet) -->

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>

<title>Log in</title>

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Tailwindcss package import -->
<link href="https://unpkg.com/tailwindcss@%5E2/dist/tailwind.min.css" rel="stylesheet">
</head>

<body>

<!-- Display the error message pop-up if there was a failed authentication -->

<% if(null != request.getAttribute("errorMessage")) { %>
<div class="pl-5">
<div class=" absolute inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl border border-red-500 transform transition-all sm:my-8 sm:align-left sm:max-w-lg sm:w-full">
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<div class="sm:flex sm:items-start">
<div class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10">
<!-- Heroicon name: outline/exclamation -->
<svg class="h-6 w-6 text-red-600"
xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
</svg>
</div>
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left">
<h3 class="text-lg leading-6 font-medium text-gray-900"
id="modal-title"><%= request.getAttribute("errorMessage") %></h3>
<div class="mt-2">
<p class="text-sm text-gray-500">Please try signing in again or create a new account.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<%}%>

<div class="flex h-screen justify-center items-center">
<div class="w-full max-w-xs">
<form class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4"
action="Login">
<div
class="text-center text-gray-700 text-lg font-bold mb-2 cursor-text">
<h1>Login</h1>
</div>

<div class="mb-4">
<label
class="block text-gray-700 text-sm font-bold mb-2 cursor-text"
for="username"> Username </label> <input
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
name="username" type="text" placeholder="Username" required>
</div>

<div class="mb-6">
<label class="block text-gray-700 text-sm font-bold mb-2"
for="password"> Password </label> <input
class="shadow appearance-none border border-blue-500 rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline"
name="password" type="password" placeholder="******************"
required>
</div>

<div class="flex items-center justify-between">
<input class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="submit" value="Sign In">
<label class="flex items-center">
<input type="checkbox" class="form-checkbox cursor-pointer" name="isNewAccount" value="1">
<span class="ml-2">New Account</span>
</label>
</div>

<div
class="text-center pt-6 font-bold text-sm text-blue-500 hover:text-blue-800 cursor-pointer">
<a href="#">Forgot Password?</a>
</div>

</form>
<!-- Copyright symbol under the form. Needs to be updated -->
<p class="text-center text-gray-500 text-xs">
&copy;Placeholder. All rights reserved.
</p>
</div>
</div>

</body>
</html>