简体   繁体   中英

Laravel authentication with username is case insensitive

I am using Laravel mannual authentication and I need case sensitive username check, but laravel have by default case insensitive checking, I don't find in documentation how to change it. Is there some easy way or I need to write my own authentication?

Here is my Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {
        $credentials = $request->only('username', 'password');

        if (Auth::attempt($credentials)) {

            return redirect()->intended('dashboard');
        }
        return redirect()->intended('login');
    }

The case insensitive match is not coming from Laravel itself but rather from your database which is (in most cases) using a case insensitive collation to store the username. You can change your migration to something like eg:

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique()->collation('utf8_bin');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

This will collate the email column with the utf8_bin collation which is not case sensitive. The collection will affect the sorting of columns however so any queries with ORDER BY email might return a different ordering if you're using UTF8 characters of ambiguous order. If this is an email or a username that can only be using ASCII characters this is not an issue though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM