Skip to content

Commit

Permalink
login
Browse files Browse the repository at this point in the history
  • Loading branch information
mailgyc-163 committed Jul 7, 2017
1 parent 7d1a698 commit 2b869df
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
4 changes: 2 additions & 2 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ CREATE TABLE IF NOT EXISTS account (
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS session (
CREATE TABLE IF NOT EXISTS record (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
u_id INT NOT NULL REFERENCES account(id),
slug VARCHAR(100) NOT NULL UNIQUE,
round VARCHAR(100) NOT NULL,
markdown MEDIUMTEXT NOT NULL,
published DATETIME NOT NULL,
updated TIMESTAMP NOT NULL,
Expand Down
44 changes: 26 additions & 18 deletions src/net/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ def executor(self) -> ThreadPoolExecutor:
def data_received(self, chunk):
pass

# def on_finish(self):
def on_finish(self):
# self.session.flush()
pass


class WebHandler(BaseHandler):
# @tornado.web.authenticated
def get(self):
if not self.get_cookie("_csrf"):
self.set_cookie("_csrf", self.xsrf_token)
self.render('poker.html')


Expand All @@ -44,8 +47,9 @@ def get(self):


class RegHandler(BaseHandler):
def get(self):
email = self.get_argument('email')

def post(self):
email = self.get_argument('email', self.get_argument('username'))
account = self.db.get('SELECT * FROM account WHERE email="%s"', email)
if account:
raise tornado.web.HTTPError(400, "username already taken")
Expand All @@ -57,24 +61,28 @@ def get(self):
uid = self.db.insert('INSERT INTO account (email, username, password) VALUES ("%s", "%s", "%s")',
email, username, password)

self.head('content-type', 'application/json')
self.set_secure_cookie("uid", str(account.get('id')))
self.write('ok')


def auth_login(self):
account = self.db.get('SELECT * FROM account WHERE email="%s"', self.get_argument('email'))
password = self.get_argument("password")
password = bcrypt.hashpw(password.encode('utf8'), account.get('password'))
class LoginHandler(BaseHandler):

if password == account.get('password'):
self.set_secure_cookie("uid", str(account.get('id')))
self.redirect(self.get_argument("next", "/"))
return True
return False
def post(self):
username = self.get_argument('email')
password = self.get_argument("password")
account = self.db.get('SELECT * FROM account WHERE email="%s"', self.get_argument('email'))
password = bcrypt.hashpw(password.encode('utf8'), account.get('password'))

self.head('content-type', 'application/json')
if password == account.get('password'):
self.set_secure_cookie("uid", str(account.get('id')))
self.redirect(self.get_argument("next", "/"))


def auth_logout(self):
uid = self.get_secure_cookie("uid")
self.clear_cookie("uid")
self.session.remove(int(uid))
self.redirect(self.get_argument("next", "/"))
class LoginoutHandler(BaseHandler):

def post(self):
uid = self.get_secure_cookie("uid")
self.clear_cookie("uid")
self.session.remove(int(uid))
self.redirect(self.get_argument("next", "/"))
41 changes: 23 additions & 18 deletions src/static/js/boot.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
PG = {
PG = {
score: 0,
music: null,
playerInfo: {},
orientated: false
};


PG.getCookie = function(name) {
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
return r ? r[1] : undefined;
}

PG.PW = 90;
PG.PH = 120;

Expand Down Expand Up @@ -172,6 +178,7 @@ PG.Login = function (game) {
this.username = null;
this.password = null;
this.passwordAgain = null;
this.error = null;
};

PG.Login.prototype = {
Expand All @@ -188,26 +195,27 @@ PG.Login.prototype = {
// type: PhaserInput.InputType.password
};
this.game.add.plugin(PhaserInput.Plugin);

this.username = this.add.inputField((this.world.width-300)/2, this.world.height/2 - 130, style);

style.placeHolder = '密码';
this.password = this.add.inputField((this.world.width-300)/2, this.world.height/2 - 65, style);

style.placeHolder = '再次输入密码';
this.passwordAgain = this.add.inputField((this.world.width-300)/2, this.world.height/2, style);

var style = {font: "22px Arial", fill: "#f00", align: "center"};
this.error = this.add.text(this.world.width/2, this.world.height/2 + 20, '', style);

var login = this.add.button(this.world.width/2, this.world.height * 3/4, 'btn', this.onLogin, this, 'register.png', 'register.png', 'register.png');
login.anchor.set(0.5);
},

onLogin: function () {
var req = {
email: this.username.value,
username: this.username.value,
password: this.password.value,
password_again: this.passwordAgain.value
};
if (!req['username']) { alert('请输入用户名'); }
if (!req['password']) { alert('请输入密码'); }
if (!req['password_again']) { alert('请再次输入密码'); }
if (!this.username.value) { this.username.startFocus(); return; }
if (!this.password.value) { this.password.startFocus(); return; }
if (!this.passwordAgain.value) { this.passwordAgain.startFocus(); return; }
if (this.password.value != this.passwordAgain.value) { this.error.text="两次输入的密码不一致"; return; }

var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function(){
Expand All @@ -217,18 +225,15 @@ PG.Login.prototype = {
console.log(httpRequest.responseText);
} else {
console.log('Error:' + httpRequest.status);
alert(httpRequest.responseText);
this.error.text = httpRequest.responseText;
}
}
};
httpRequest.open('POST', '/reg', true);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.send(JSON.stringify(req) + '&_xsrf=' + this.getCookie("_xsrf"));
},

getCookie: function(name) {
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
return r ? r[1] : undefined;
}
httpRequest.setRequestHeader('X-Csrftoken', PG.getCookie("_xsrf"))

var req = 'username=' + encodeURIComponent(this.username.value) + '&password=' + encodeURIComponent(this.password.value);
httpRequest.send(req);
}
};
1 change: 1 addition & 0 deletions src/static/poker.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<body>
<div id="game"></div>
<div id="orientation"></div>
{% module xsrf_form_html() %}
<script>
(function () {
var h = window.innerHeight * 960/window.innerWidth;
Expand Down

0 comments on commit 2b869df

Please sign in to comment.