Skip to content

Commit 17eb7be

Browse files
author
Yeray Diaz Diaz
committed
Added vendor JS gulp step to include zxcvbn.js
Include it in the register template Added password strength gauge in registration form.
1 parent c196c01 commit 17eb7be

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

Gulpfile.babel.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ gulp.task("dist:images", () => {
150150
.pipe(gulp.dest(path.join(distPath, "images")));
151151
});
152152

153+
gulp.task("dist:vendor", () => {
154+
return gulp.src(path.join(staticPrefix, "js", "vendor", "**", "*"))
155+
.pipe(gulp.dest(path.join(distPath, "js", "vendor")));
156+
});
157+
153158

154159
gulp.task("dist:manifest", () => {
155160
let paths = [
@@ -252,7 +257,7 @@ gulp.task("dist", (cb) => {
252257
// any previously built files.
253258
"clean",
254259
// Build all of our static assets.
255-
["dist:font-awesome", "dist:css", "dist:js"],
260+
["dist:font-awesome", "dist:css", "dist:js", "dist:vendor"],
256261
// We have this here, instead of in the list above even though there is no
257262
// ordering dependency so that all of it's output shows up together which
258263
// makes it easier to read.

warehouse/static/js/vendor/zxcvbn.js

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

warehouse/static/js/warehouse/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ docReady(Analytics);
6262

6363
// Handle the JS based automatic form submission.
6464
docReady(formUtils.submitTriggers);
65+
docReady(formUtils.passwordStrength);
6566

6667
docReady(Statuspage);
6768

warehouse/static/js/warehouse/utils/forms.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,24 @@ export function submitTriggers() {
4949
);
5050
}
5151
}
52+
53+
/* global zxcvbn */
54+
55+
const checkPasswordStrength = (event) => {
56+
let result = document.querySelector(".pw-strength-guage");
57+
if (event.target.value === "") {
58+
result.setAttribute("class", "pw-strength-guage");
59+
} else {
60+
// following recommendations on the zxcvbn JS docs
61+
// the zxcvbn function is available by loading `vendor/zxcvbn.js`
62+
// in the register page template only
63+
let zxcvbnResult = zxcvbn(event.target.value);
64+
result.setAttribute("class", `pw-strength-guage pw-strength-guage__${zxcvbnResult.score}`);
65+
}
66+
};
67+
68+
export function passwordStrength() {
69+
let password = document.querySelector("#password");
70+
if (password === null) return;
71+
password.addEventListener("input", checkPasswordStrength, false);
72+
}

warehouse/static/sass/blocks/_form-group.scss

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,43 @@
4444
max-width: 100%;
4545
font-size: $small-font-size;
4646
font-style: italic;
47+
48+
.pw-strength {
49+
display: inline-block;
50+
width: 100%;
51+
height: 0.8em;
52+
border: 1px solid $border-color;
53+
54+
.pw-strength-guage {
55+
width: 0%;
56+
height: 100%;
57+
display: block;
58+
59+
&__0 {
60+
width: 20%;
61+
background-color: red;
62+
}
63+
64+
&__1 {
65+
width: 40%;
66+
background-color: orange;
67+
}
68+
69+
&__2 {
70+
width: 60%;
71+
background-color: yellow;
72+
}
73+
74+
&__3 {
75+
width: 80%;
76+
background-color: #508000;
77+
}
78+
79+
&__4 {
80+
width: 100%;
81+
background-color: green;
82+
}
83+
}
84+
}
4785
}
4886
}

warehouse/templates/accounts/register.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ <h2>Create an account on PyPI</h2>
7676
<p class="form-group__help-text">
7777
Choose a strong password that contains letters (uppercase and lowercase), numbers and special characters. Avoid common words or repetition.
7878
</p>
79+
<p class="form-group__help-text">
80+
Password strength: <span class="pw-strength"><span class="pw-strength-guage"></span></span>
81+
</p>
7982
{% if form.password.errors %}
8083
<ul class="form-errors">
8184
{% for error in form.password.errors %}
@@ -108,4 +111,7 @@ <h2>Create an account on PyPI</h2>
108111

109112
{% block extra_js %}
110113
{{ recaptcha_src(request) }}
114+
<script async
115+
src="{{ request.static_path('warehouse:static/dist/js/vendor/zxcvbn.js') }}">
116+
</script>
111117
{% endblock %}

0 commit comments

Comments
 (0)