Skip to content
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

Simple login #44

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions .settings/org.eclipse.wst.common.component
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<property name="context-root" value="facitter"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/java"/>
<wb-resource deploy-path="/" source-path="src/main/webapp"/>
<<<<<<< HEAD
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
=======
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
>>>>>>> refs/heads/master
</wb-module>
</project-modules>
91 changes: 60 additions & 31 deletions src/main/java/jp/enpit/facitter/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,63 @@
import javax.xml.bind.annotation.XmlElement;

public class User {
@XmlElement(name="name")
private String name;
@XmlElement(name="password")
private String password;

public User(){
setName("ゲスト");
setPassword("password");
}
public User(String name) {
setName(name);
setPassword("password");
}
public User(String name, String password) {
setName(name);
setPassword(password);
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
}
@XmlElement(name = "name")
private String name;

private String password;
private boolean status;

public User() {
setName("ゲスト");
setPassword("password");
setStatus(true);
}

public User(String name) {
setName(name);
setPassword("password");
setStatus(true);
}

public User(String name, String password) {
setName(name);
setPassword(password);
setStatus(true);
}

public User(String name, String password, boolean status) {
this(name, password);
setStatus(status);
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword() {
return this.password;
}

private void setStatus(boolean status) {
this.status = status;
}

public boolean getStatus() {
return this.status;
}

public void logout() {
setStatus(false);
}
public void login() {
setStatus(true);
}
}
14 changes: 12 additions & 2 deletions src/main/java/jp/enpit/facitter/model/UserModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ public User register(User user) {
return user;
}

// userを更新する機構
public User update(User user) {
users().replaceOne(new BasicDBObject("name",user.getName()), toDocument(user));

return user;
}

// Documentへの変換
private Document toDocument(User user){
return new Document()
.append("name", user.getName())
.append("password", user.getPassword());
.append("password", user.getPassword())
.append("status", user.getStatus());
}

// Userへの変換
Expand All @@ -77,8 +85,10 @@ private User toUser(Document document) {
return null;
return new User(
document.getString("name"),
document.getString("password")
document.getString("password"),
document.getBoolean("status")
);

}

// List形式への変換
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/jp/enpit/facitter/rest/UsersRest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,41 @@ public Response postUser(@FormParam("name") String name, @FormParam("password")
.build();
}
}

@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("logout/{name}")
public Response logout(@PathParam("name") String name) {

try(UserModel model = createModel()) {
User user = model.findUser(name);
//System.out.println("aaaaaaaaaaaaaaa");
user.logout();
model.update(user);
System.out.println("name: "+user.getName()+" status: "+user.getStatus());
return Response.status(201)
.entity(user)
.build();
}

}
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("login/{name}")
public Response login(@PathParam("name") String name) {

try(UserModel model = createModel()) {
User user = model.findUser(name);
//System.out.println("aaaaaaaaaaaaaaa");
user.login();
model.update(user);
System.out.println("name: "+user.getName()+" status: "+user.getStatus());
return Response.status(201)
.entity(user)
.build();
}

}

private UserModel createModel(){
return new UserModel();
Expand Down
136 changes: 95 additions & 41 deletions src/main/webapp/js/simple_login.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var endpoint = 'http://localhost:8080/facitter/api';
var register = function() {

var userName = $('#userName').val();
var email = $('#email').val();
// var email = $('#email').val();
var password = $('#password').val();
var passwordConfirm = $('#passwordConfirm').val();
/*
Expand All @@ -20,20 +20,20 @@ var register = function() {
* document.getElementById('passwordConfirm').value;
*/

if ((userName == '' || email == '') || password == ''
|| passwordConfirm == '') {
console.log("any one is empty\n");
// if ((userName == '' || email == '') || password == ''
if (userName == '' || password == '' || passwordConfirm == '') {
// console.log("any one is empty\n");
return;
}

if (!confirmCheck())
return;
if( !checkContain() ) {
console.log("Contain01");

if (!checkContain()) {
// console.log("Contain01");
return;
}

$.ajax({
type : 'POST',
url : endpoint + '/users',
Expand All @@ -42,14 +42,15 @@ var register = function() {
password : password
},
success : function() {
// flag = true;
// 本番環境へ
// window.location.href =
// "https://team2017-2.spiral.cloud/lama/";
window.location.href = "../facitter/facitter.html";

// flag = true;
// 本番環境へ
// window.location.href =
// "https://team2017-2.spiral.cloud/lama/";
window.location.href = "../facitter/facitter.html";
}
});

}

var confirmCheck = function() {
Expand All @@ -76,57 +77,110 @@ var checkContain = function() {

$.ajax({
type : 'GET',
url : endpoint + '/users/'+userName,
url : endpoint + '/users/' + userName,
data : {
name : userName
},
async: false
async : false
}).fail(function(response) {
// 含まれていない
console.log(response);
console.log("Not contain");
// console.log(response);
// console.log("Not contain");
error = true;
}).done(function(response) {
// 含まれている
console.log(response);
console.log("Contain00");
//return false;
// console.log(response);
// console.log("Contain00");
// return false;
});

return error;
}

// login
var login = function() {
var userName = $('#userName').val();
//var email = $('#email').val();
// var email = $('#email').val();
var password = $('#password').val();
var pass;

$.ajax({
type : 'GET',
url : endpoint + '/users/'+userName,
url : endpoint + '/users/' + userName,
data : {
name : userName
},
async: false
async : false
}).fail(function(response) {
// 含まれていない
console.log(response);
console.log("Not contain");
}).done(function(response) {
// 含まれている
console.log(response);
console.log("Contain00");
pass = response['password'];
});

console.log(password);
console.log(pass);

if(password == pass) {
document.cookie = 'userName='+userName;
// console.log(response);
// console.log("Not contain");
}).done(
function(response) {
// 含まれている
// console.log(response);
// console.log("Contain00");
var logout = function() {
var userName = $('#userName').val();

$.ajax({
type : 'PUT',
url : endpoint + '/users/login/' + userName,
data : $.param({
name : userName
}),
success : function() {
// flag = true;
// 本番環境へ
// window.location.href =
// "https://team2017-2.spiral.cloud/lama/";
// window.location.href = "#";
},
error : function(xhr, ajaxOptions, thrownError) {
alert("msg: " + thrownError.message + " , status: "
+ xhr.status)
}
});
}
pass = response['password'];
});

// console.log(password);
// console.log(pass);

if (password == pass) {
document.cookie = 'userName=' + userName;
window.location.href = "../facitter/facitter.html";
}
else window.location.reload();
// else window.location.reload();
}

/*
* window.onbeforeunload = function() { logout(); return "本当に離れますか?"; };
*/

var logout = function() {
var userName = $('#userName').val();

$.ajax({
type : 'PUT',
url : endpoint + '/users/logout/' + userName,
data : $.param({
name : userName
}),
success : function() {
// flag = true;
// 本番環境へ
// window.location.href =
// "https://team2017-2.spiral.cloud/lama/";
window.location.href = "#";
},
error : function(xhr, ajaxOptions, thrownError) {
alert("msg: " + thrownError.message + " , status: " + xhr.status)
}
});

//console.log(password);
//console.log(pass);

}
Loading