Skip to content

Commit e465c6e

Browse files
committed
fix sign in and sign up was broken
1 parent fa79cf5 commit e465c6e

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

firefly/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from flask import Flask, g, request, send_from_directory
66
from flask_security import MongoEngineUserDatastore
77
from flask_social_blueprint.core import SocialBlueprint
8+
from flask_wtf.csrf import CsrfProtect
89

910
from firefly import config as _config
1011
from firefly.ext import (
@@ -34,6 +35,7 @@ def create_app(config):
3435
db.init_app(app)
3536
mail.init_app(app)
3637
redis_store.init_app(app)
38+
CsrfProtect(app)
3739

3840
register_auth(app)
3941
register_hooks(app)

firefly/templates/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes"/>
99
{% block title %}
1010
{% endblock %}
11-
<link rel="icon" type="image/png" href="${url_for('static', filename='icon.png')}"/>
11+
<link rel="icon" type="image/png" href="{{url_for('static', filename='icon.png')}}"/>
1212
<link href="{{url_for('static', filename='stylesheets/hljs.css')}}" rel="stylesheet"/>
1313
<link href="{{url_for('static', filename='stylesheets/themes/zenburn.css')}}" rel="stylesheet"/>
1414
<script src="{{url_for('static', filename='javascripts/libs/require.min.js')}}"></script>
@@ -50,7 +50,7 @@
5050
<div class="panel">
5151
{% if current_user.is_authenticated() %}
5252
Hi {{current_user.username if current_user.username else current_user.cn}}
53-
<a title="退出" class="btn-primary btn-small btn sign-out-button" href="${url_for('security.logout')}">
53+
<a title="退出" class="btn-primary btn-small btn sign-out-button" href="{{url_for('security.logout')}}">
5454
</a>
5555
{% else %}
5656
<button title="注册" class="btn-primary btn-small btn sign-up-button">注册</button>

firefly/templates/login.html

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<h3>登录</h3>
88
<div class="clearfix"></div>
99
</div>
10-
<form id="signinform" class="login-form" role="form" method="post" action="${url_for('home.login')}">
10+
<form id="signinform" class="login-form" role="form" method="post" action="{{url_for('home.login')}}">
1111
<div class="form-group">
1212
<label class="control-label" for="">第三方登录</label>
1313
<div class="thirdparty-login-btns">
@@ -29,14 +29,15 @@ <h3>登录</h3>
2929
</div>
3030
</div>
3131
<div class="form-group">
32+
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
3233
<label class="control-label" for="inputEmail">邮箱</label>
33-
<input id="inputEmail" class="form-control" name="" type="text" value="" type="email" required="" tabindex="1" name="email"/>
34+
<input id="inputEmail" class="form-control" type="text" value="" type="email" required="" tabindex="1" name="email"/>
3435
</div>
3536
<div class="form-group">
3637
<label class="control-label" for="inputPassword">
3738
密码(<a tabindex="4" href="/forgotpass">忘记密码</a>
3839
</label>
39-
<input id="inputPassword" class="form-control" name="" type="text" value="" type="password" required="" tabindex="2" name="password" />
40+
<input id="inputPassword" class="form-control" type="text" value="" type="password" required="" tabindex="2" name="password" />
4041
</div>
4142
<div class="form-group">
4243
<button id="loginBtn" class="login-btn btn-default" tabindex="3" type="submit">
@@ -58,7 +59,7 @@ <h3>注册</h3>
5859
</div>
5960
</div>
6061

61-
<form id="signupform" class="login-form" role="form" method="post" action="${url_for('home.register')}">
62+
<form id="signupform" class="login-form" role="form" method="post" action="{{url_for('home.register')}}">
6263
<div class="form-group">
6364
<label class="control-label" for="">
6465
第三方注册
@@ -83,21 +84,22 @@ <h3>注册</h3>
8384
</div>
8485

8586
<div class="form-group">
87+
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
8688
<label class="control-label" for="inputUsername">
8789
用户名
8890
</label>
8991
<b class="has-required" data-title="必填">Required</b>
90-
<input id="inputUsername" class="form-control" name="" type="text" value="" type="text" name="username" tabindex="1"/>
92+
<input id="inputUsername" class="form-control" type="text" value="" type="text" name="username" tabindex="1"/>
9193
</div>
9294
<div class="form-group">
9395
<label class="control-label" for="inputEmail">邮箱</label>
9496
<b class="has-required" data-title="必填">Required</b>
95-
<input id="inputEmail" class="form-control" name="" type="text" value="" type="email" name="email" tabindex="2"/>
97+
<input id="inputEmail" class="form-control" type="text" value="" type="email" name="email" tabindex="2"/>
9698
</div>
9799
<div class="form-group">
98100
<label class="control-label" for="inputPassword">密码</label>
99101
<b class="has-required" data-title="必填,至少 8 位,同时包含数字、小写字母、大写字母">Required</b>
100-
<input id="inputPassword" class="form-control" name="" type="text" value="" type="password" name="password" tabindex="3" />
102+
<input id="inputPassword" class="form-control" type="text" value="" type="password" name="password" tabindex="3" />
101103
</div>
102104
<div class="form-group">
103105
<button id="signupBtn" class="signup-btn btn btn-default" type="submit" tabindex="4" disabled="disabled" >注册</button>

firefly/views/home.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def get(self):
6868
return redirect(url_for('home.index'))
6969

7070
def post(self):
71-
# TODO 解决在首页登录框中无法获取 csrf_token 的问题
72-
form = LoginForm(csrf_enabled=False)
71+
form = LoginForm()
7372
if form.validate_on_submit():
7473
login_user(form.user)
7574
return redirect(url_for('home.index'))
@@ -81,7 +80,7 @@ def get(self):
8180
return redirect(url_for('home.index'))
8281

8382
def post(self):
84-
form = RegisterForm(csrf_enabled=False)
83+
form = RegisterForm()
8584
if form.validate_on_submit():
8685
user = form.save()
8786
login_user(user)

0 commit comments

Comments
 (0)