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
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
29 changes: 29 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main.java;

/** An item on a todo list. */
public final class Task {

private final long id;
private final String name;
private final String lastNames;
private final String username;
private final long timestamp;

public Task(long id, String names, String lastNames, String username,
long timestamp) {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nit: You can get rid of this empty extra line.

this.id = id;
this.names = names;
this.lastNames = lastNames;
this.username = username;
this.timestamp = timestamp;

}

public String getUsername() {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nit: You can get rid of this empty extra line.

return this.username;

}

}
51 changes: 51 additions & 0 deletions src/main/java/com/ServerMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.google.sps;

import java.net.URL;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;

/**
* Starts up the server, including a DefaultServlet that handles static files, and any servlet
* classes annotated with the @WebServlet annotation.
*/
public class ServerMain {

public static void main(String[] args) throws Exception {

// Create a server that listens on port 8080.
Server server = new Server(8080);
WebAppContext webAppContext = new WebAppContext();
server.setHandler(webAppContext);

// Load static content from inside the jar file.
URL webAppDir = ServerMain.class.getClassLoader().getResource("META-INF/resources");
webAppContext.setResourceBase(webAppDir.toURI().toString());

// Enable annotations so the server sees classes annotated with @WebServlet.
webAppContext.setConfigurations(
new Configuration[] {
new AnnotationConfiguration(), new WebInfConfiguration(),
});

// Look for annotations in the classes directory (dev server) and in the jar file (live server)
webAppContext.setAttribute(
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
".*/target/classes/|.*\\.jar");

// Handle static resources, e.g. html files.
ServletHolder defaultServletHolder = webAppContext.addServlet(DefaultServlet.class, "/");
defaultServletHolder.setInitParameter("cacheControl", "no-store, max-age=0");

// Start the server! 🚀
server.start();
System.out.println("Server started!");

// Keep the main thread alive while the server is running.
server.join();
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/servlets/LoginHandlerServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.google.sps.servlets;

import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.FullEntity;
import com.google.cloud.datastore.KeyFactory;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login-handler")
public class LoginHandlerServlet extends HttpServlet {

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// Get the value entered in the form.
String clientName = request.getParameter("clientName");
String clientPassword = request.getParameter("clientPassword");

// Creating variable to interact with Datastore
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind("Task");

// Print the value so you can see it in the server logs.
System.out.println(clientName + ": " + clientPassword);


Query<Entity> query = Query.newEntityQueryBuilder().setKind("Task").setOrderBy(OrderBY.desc("timestamp")).build();
QueryResults<Entity> results = datastore.run(query);

List<task> tasks = new ArrayList<>();
while(results.hasnext()){
Entity entity = results.next();

long id = entity.getKey().getID();
String name = entity.getString("clientName");
String password = entity.getString("clientPassword");
long timestamp = entity.getLong("timestamp");

Task task = new Task(id, clientName, clientPassword, timestamp);
tasks.add(task);
}

// check if the username exists in the database
boolean existUsername = true;
for (int i = 0; i < tasks.size(); i++){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that you can make this more efficient by storing a set or map of usernames and then checking if the username exists there. It would be O(1) runtime instead of O(n) (please reach out if you have further questions about this!)

if (!tasks.get(i).getUsername().equals(username)){ // sets the variable 'existUsername' to false if the username does not exist in the database
existUsername = false;
}
}

// what happens after the check
if (existUsername){
// true - direct it to their respective page
}
else{
// false - direct to the 'newUser' page which directs to the register page
}


}
}
61 changes: 61 additions & 0 deletions src/main/java/com/servlets/LoginSubmission.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.StructuredQuery.OrderBy;
import com.google.sps.data.Task;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet responsible for listing tasks. */
@WebServlet("/login-handler")
public class LoginSubmissions extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
Query<Entity> query =
Query.newEntityQueryBuilder().setKind("Task").setOrderBy(OrderBy.desc("timestamp")).build();
QueryResults<Entity> results = datastore.run(query);

List<Task> tasks = new ArrayList<>();
while (results.hasNext()) {
Entity entity = results.next();

long id = entity.getKey().getId();
String clientName = request.getParameter("clientName");
String clientPassword = request.getParameter("clientPassword");

Task task = new Task(id, clientName, clientPassword);
tasks.add(sub);
}

Gson gson = new Gson();

response.setContentType("application/json;");
response.getWriter().println(gson.toJson(subs));
}
}
Empty file.
Binary file added src/main/webapp/.DS_Store
Binary file not shown.
Binary file added src/main/webapp/images/.DS_Store
Binary file not shown.
Binary file added src/main/webapp/images/background.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/images/user.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file removed src/main/webapp/index.html
Empty file.
28 changes: 28 additions & 0 deletions src/main/webapp/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Login</title>
</head>
<body>
<div class="bgImg"></div>
<div class="logBox">
<img src="images/user.png" class="avatar">
<h1>Login Here</h1>
<form action="/login-handler" method="POST">
<p>Username</p>
<input type="text" name="clientName" placeholder="Enter Username">
<p>Password</p>
<input type="password" name="clientPassword" placeholder="Enter Password">
</br>
<input type="submit" name="" value="Login">
<a href="#">Forgot password?</a>
</br>
<a href="#"> Dont have an account?</a>
</form>
</div>
</body>
</html>
95 changes: 95 additions & 0 deletions src/main/webapp/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
body, html {
height: 100%;
}


body{
margin: 0;
padding: 0;
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.bgImg{
background: url(images/background.jpeg);
filter: blur(3px);
-webkit-filter: blur(3px);
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
outline: none;
}

.logBox{
width:320px;
height: 420px;
background: rgba(0, 0, 0, 0.9);
color: white;
top:50%;
left:50%;
position:absolute;
transform:translate(-50%, -50%);
box-sizing:border-box;
border: 2px solid rgba(255, 252, 252, 0.7);
border-radius: 10px;
padding: 50px;
box-shadow: 10px;
}
.logBox h1{
text-align: center;
font-size: 27px;
}

.avatar{
width: 100px;
height: 100px;
border: 2px solid white;
border-radius: 50%;
position:absolute;
top: -50px;
left: calc(50% - 50px)
}

.logBox p{
margin:0;
font-weight:bold;
}
.logBox input{
width:100%;
margin-bottom: 20px;
box-align:center;
}
.logBox input[type="text"], input[type="password"]{
border:none;
border-bottom:1px solid white;
background: transparent;
outline:none;
height: 40px;
color: white;
font-size: 15px;
}
.logBox input[type="submit"]{
border:none;
outline: none;
height: 40px;
background: white;
color: black;
font-size: 18px;
border-radius: 10px;
}
.logBox input[type="submit"]:hover{
cursor: pointer;
background: black;
color: white;
border: 1px solid white;
}
.logBox a{
text-decoration: none;
font-size: 13px;
line-height: 20px;
padding:2px;
color:darkgrey;
text-align: center;
}
.logBox a:hover{
text-decoration: underline;
}
Empty file removed src/main/webapp/style.scss
Empty file.