Jul 2nd - Optimized code
This commit is contained in:
@@ -3,26 +3,7 @@
|
||||
{% block page_title %}Edit User{% endblock %}
|
||||
|
||||
{% block head %}
|
||||
<style>
|
||||
/* ── Password rule checklist ── */
|
||||
.pw-rules{
|
||||
margin-top:8px;display:grid;grid-template-columns:1fr 1fr;gap:3px 12px;
|
||||
background:var(--bg);border:1px solid var(--border);border-radius:8px;
|
||||
padding:10px 12px;
|
||||
}
|
||||
.pw-rule{
|
||||
display:flex;align-items:center;gap:5px;
|
||||
font-size:11.5px;color:var(--muted);
|
||||
transition:color .2s;
|
||||
}
|
||||
.pw-rule .ri{
|
||||
font-size:13px;width:14px;text-align:center;
|
||||
transition:color .2s, transform .15s;
|
||||
}
|
||||
.pw-rule.met{color:var(--success);}
|
||||
.pw-rule.met .ri{color:var(--success);transform:scale(1.1);}
|
||||
.pw-rule.unmet .ri{color:var(--border2);}
|
||||
</style>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/password-strength.css') }}"/>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
@@ -67,18 +48,38 @@
|
||||
<div class="col-12"><hr style="border-color:var(--border);"/></div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">New Password <span style="color:var(--muted);font-weight:400;">(optional)</span></label>
|
||||
<input type="password" class="form-control" id="new-password" name="new_password" placeholder="Leave blank to keep current"/>
|
||||
<div class="pw-wrap">
|
||||
<input type="password" class="form-control pw-input" id="new-password" name="new_password"
|
||||
maxlength="128" placeholder="Leave blank to keep current"
|
||||
autocomplete="new-password"/>
|
||||
<button type="button" class="pw-toggle" id="new-password-toggle"
|
||||
title="Show / hide password" tabindex="-1">
|
||||
<i class="bi bi-eye"></i>
|
||||
</button>
|
||||
</div>
|
||||
<div style="font-size:11px;color:var(--muted);margin-top:5px;">Leave both fields blank to keep the current password.</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Confirm Password</label>
|
||||
<input type="password" class="form-control" id="confirm-password" name="confirm_password" placeholder="Repeat new password"/>
|
||||
<div class="pw-wrap">
|
||||
<input type="password" class="form-control pw-input" id="confirm-password" name="confirm_password"
|
||||
maxlength="128" placeholder="Repeat new password"
|
||||
autocomplete="new-password"/>
|
||||
<button type="button" class="pw-toggle" id="confirm-password-toggle"
|
||||
title="Show / hide password" tabindex="-1">
|
||||
<i class="bi bi-eye"></i>
|
||||
</button>
|
||||
</div>
|
||||
<div class="pw-match" id="pw-match-msg"></div>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<!-- Password strength bar -->
|
||||
<div style="height:4px;background:var(--border);border-radius:2px;margin-top:4px;" id="pw-strength-track">
|
||||
<div id="pw-strength-bar" style="height:100%;border-radius:2px;width:0%;transition:width .3s,background .3s;"></div>
|
||||
<!-- Strength bar -->
|
||||
<div class="pw-strength-wrap" id="pw-strength-wrap" style="display:none;">
|
||||
<div class="pw-strength-track">
|
||||
<div class="pw-strength-fill" id="pw-strength-bar"></div>
|
||||
</div>
|
||||
<div class="pw-strength-label" id="pw-strength-label"></div>
|
||||
</div>
|
||||
<div id="pw-strength-label" style="font-size:11px;color:var(--muted);margin-top:4px;"></div>
|
||||
|
||||
<!-- Rule checklist — mirrors server-side validate_password() rules -->
|
||||
<div class="pw-rules" id="pw-rules-box" style="display:none;">
|
||||
@@ -113,87 +114,31 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
<script src="{{ url_for('static', filename='js/password-strength.js') }}"></script>
|
||||
<script>
|
||||
// ── Password strength + rule checklist ────────────────────────────────────────
|
||||
// Mirrors server-side validate_password() rules in validation_service.py.
|
||||
// Any change to the server rules must be reflected here too.
|
||||
const PW_RULES = {
|
||||
len : pw => pw.length >= 8,
|
||||
upper : pw => /[A-Z]/.test(pw),
|
||||
digit : pw => /\d/.test(pw),
|
||||
special: pw => /[!@#$%^&*()\-_=+\[\]{};:'",.<>?/\\|`~]/.test(pw),
|
||||
};
|
||||
|
||||
function setPwRule(id, met) {
|
||||
const el = document.getElementById('pw-rule-' + id);
|
||||
if (!el) return;
|
||||
el.className = 'pw-rule ' + (met ? 'met' : 'unmet');
|
||||
el.querySelector('.ri').innerHTML = met
|
||||
? '<i class="bi bi-check-circle-fill"></i>'
|
||||
: '<i class="bi bi-circle"></i>';
|
||||
}
|
||||
|
||||
document.getElementById('new-password').addEventListener('input', function () {
|
||||
const val = this.value;
|
||||
|
||||
const bar = document.getElementById('pw-strength-bar');
|
||||
const label = document.getElementById('pw-strength-label');
|
||||
const rules = document.getElementById('pw-rules-box');
|
||||
|
||||
if (val.length === 0) {
|
||||
bar.style.width = '0%';
|
||||
label.textContent = '';
|
||||
rules.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
|
||||
rules.style.display = 'grid';
|
||||
setPwRule('len', PW_RULES.len(val));
|
||||
setPwRule('upper', PW_RULES.upper(val));
|
||||
setPwRule('digit', PW_RULES.digit(val));
|
||||
setPwRule('special', PW_RULES.special(val));
|
||||
|
||||
let score = 0;
|
||||
if (PW_RULES.len(val)) score++;
|
||||
if (PW_RULES.upper(val)) score++;
|
||||
if (PW_RULES.digit(val)) score++;
|
||||
if (PW_RULES.special(val)) score++;
|
||||
if (val.length >= 16) score++; // bonus for extra length
|
||||
|
||||
const levels = [
|
||||
{ pct: '20%', bg: '#dc2626', text: 'Very weak' },
|
||||
{ pct: '40%', bg: '#d97706', text: 'Weak' },
|
||||
{ pct: '60%', bg: '#ca8a04', text: 'Fair' },
|
||||
{ pct: '80%', bg: '#16a34a', text: 'Strong' },
|
||||
{ pct: '100%', bg: '#059669', text: 'Very strong' },
|
||||
];
|
||||
const lvl = levels[Math.min(score - 1, 4)] || levels[0];
|
||||
bar.style.width = lvl.pct;
|
||||
bar.style.background = lvl.bg;
|
||||
label.textContent = lvl.text;
|
||||
label.style.color = lvl.bg;
|
||||
const pwStrength = PasswordStrength.init({
|
||||
password : 'new-password',
|
||||
confirm : 'confirm-password',
|
||||
strengthBar : 'pw-strength-bar',
|
||||
strengthLabel: 'pw-strength-label',
|
||||
strengthWrap : 'pw-strength-wrap',
|
||||
rulesWrap : 'pw-rules-box',
|
||||
rules: { len: 'pw-rule-len', upper: 'pw-rule-upper', digit: 'pw-rule-digit', special: 'pw-rule-special' },
|
||||
matchMsg : 'pw-match-msg',
|
||||
toggles: [
|
||||
{ input: 'new-password', button: 'new-password-toggle' },
|
||||
{ input: 'confirm-password', button: 'confirm-password-toggle' },
|
||||
],
|
||||
});
|
||||
|
||||
// ── Client-side password match check before submit ────────────────────────────
|
||||
// ── Block submit on a real mismatch (server re-validates regardless) ──────────
|
||||
document.querySelector('form').addEventListener('submit', function (e) {
|
||||
const pw = document.getElementById('new-password').value;
|
||||
const pw2 = document.getElementById('confirm-password').value;
|
||||
if (pw !== pw2) {
|
||||
e.preventDefault();
|
||||
document.getElementById('confirm-password').style.borderColor = 'var(--danger)';
|
||||
const msg = document.createElement('div');
|
||||
msg.style.cssText = 'font-size:12px;color:var(--danger);margin-top:4px;';
|
||||
msg.textContent = 'Passwords do not match.';
|
||||
const existing = document.getElementById('pw-match-msg');
|
||||
if (existing) existing.remove();
|
||||
msg.id = 'pw-match-msg';
|
||||
document.getElementById('confirm-password').insertAdjacentElement('afterend', msg);
|
||||
pwStrength.renderMatch();
|
||||
}
|
||||
});
|
||||
document.getElementById('confirm-password').addEventListener('input', function () {
|
||||
this.style.borderColor = '';
|
||||
const msg = document.getElementById('pw-match-msg');
|
||||
if (msg) msg.remove();
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user