You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Laravel Nova登录页添加密码显示/隐藏功能的实现方案咨询

Laravel Nova登录页添加密码显示/隐藏功能的实现方案咨询

我太懂这种心情了——明明Nova默认登录页已经够用,就差个显示/隐藏密码的小功能,完全重写整个视图又觉得没必要。刚好我之前也折腾过这个需求,给你两个实用的方案,不用大动干戈:

方案一:轻量注入JS/CSS(推荐,无需修改视图)

Nova本身确实没有内置开关来开启这个功能,但我们可以利用Nova的服务钩子,在登录页注入一小段自定义代码,完全不用碰视图文件:

  1. 在Nova服务提供者中注册资源
    打开app/Providers/NovaServiceProvider.php,在boot()方法里添加这段代码,用来加载我们的自定义JS和CSS:
public function boot()
{
    parent::boot();

    Nova::serving(function (ServingNova $event) {
        // 注册自定义JS和CSS
        Nova::script('password-toggle', public_path('js/nova-password-toggle.js'));
        Nova::style('password-toggle', public_path('css/nova-password-toggle.css'));
    });
}
  1. 编写切换密码可见性的JS
    在项目的public/js目录下创建nova-password-toggle.js,写入以下代码(专门针对Nova登录页,不会影响其他页面):
document.addEventListener('DOMContentLoaded', function() {
    // 只在Nova登录页执行
    if (window.location.pathname !== '/nova/login') return;

    const passwordInput = document.querySelector('input[name="password"]');
    if (!passwordInput) return;

    // 创建眼睛图标容器
    const toggleBtn = document.createElement('button');
    toggleBtn.type = 'button';
    toggleBtn.className = 'nova-password-toggle-btn';
    // 默认显示睁眼图标
    toggleBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>';

    // 给密码输入框的父元素设置相对定位,方便图标定位
    passwordInput.parentElement.style.position = 'relative';
    passwordInput.parentElement.appendChild(toggleBtn);

    // 绑定切换事件
    toggleBtn.addEventListener('click', function() {
        const isPassword = passwordInput.type === 'password';
        // 切换输入框类型
        passwordInput.type = isPassword ? 'text' : 'password';
        // 切换图标
        toggleBtn.innerHTML = isPassword 
            ? '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c7 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3 7 10 7a9.74 9.74 0 0 0 5.37-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>'
            : '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>';
    });
});
  1. 美化图标样式
    public/css目录下创建nova-password-toggle.css,让图标位置更贴合Nova的输入框风格:
.nova-password-toggle-btn {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    cursor: pointer;
    color: #6b7280;
    padding: 0;
}

.nova-password-toggle-btn:hover {
    color: #374151;
}

这样一来,访问/nova/login时,密码输入框右侧就会出现眼睛图标,点击就能切换密码可见性了,完全不用修改Nova的核心视图。

方案二:部分重写登录视图(更贴合原生结构)

如果你觉得注入JS不够“原生”,也可以只重写登录视图的密码输入框部分,Nova会自动识别自定义的视图文件:

  1. 复制Nova登录视图到项目目录
    Nova的登录视图文件在vendor/laravel/nova/resources/views/auth/login.blade.php,把这个文件复制到resources/views/vendor/nova/auth/login.blade.php(注意要创建对应的目录结构),Nova会优先加载这个自定义视图,而不是vendor里的默认文件。

  2. 修改密码输入框部分
    打开复制后的login.blade.php,找到密码输入框的代码块,大概是这样的:

<div class="mb-4">
    <label class="block font-medium text-sm text-gray-700" for="password">
        {{ __('Password') }}
    </label>
    <input class="form-input rounded-md shadow-sm mt-1 block w-full"
                type="password"
                name="password"
                id="password"
                required autocomplete="current-password" />
</div>

把它改成带图标的结构,让图标和输入框更融合:

<div class="mb-4">
    <label class="block font-medium text-sm text-gray-700" for="password">
        {{ __('Password') }}
    </label>
    <div class="relative">
        <input class="form-input rounded-md shadow-sm mt-1 block w-full pr-10"
                    type="password"
                    name="password"
                    id="password"
                    required autocomplete="current-password" />
        <button type="button" id="password-toggle" class="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-500 hover:text-gray-700 bg-transparent border-none cursor-pointer">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
        </button>
    </div>
</div>
  1. 添加切换逻辑
    可以在视图的底部(@endsection之前)添加一段内嵌的JS,或者继续用方案一的全局JS注入,效果是一样的。

总结

  • 优先选方案一,侵入性最小,不用修改任何视图文件,后续Nova升级也不会受影响;
  • 如果想要完全贴合Nova的原生视图结构,再用方案二,只需要复制一个视图文件,修改一小部分代码就行。

内容来源于stack exchange

火山引擎 最新活动