Customize Authentication in Laravel [closed]

2019-06-14 17:28发布

I'm using Laravel 5.1, and I need to use existing user table that has its own password algorithm. After hours and hours of research, I've found solution and here are the steps. Hope this helps Laravelers.

1条回答
劳资没心,怎么记你
2楼-- · 2019-06-14 17:51

in config/auth.php file, set driver value to custom. like this.

...
'driver' => 'custom',
...

create a file 'CustomUserProvider.php in app/Auth directory.

<?php

namespace App\Auth;

use App\Model\User;
use Carbon\Carbon;
use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;

class CustomUserProvider implements UserProvider{

    /**
     * Retrieve a user by their unique identifier.
     *
     * @param  mixed $identifier
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */

    public function retrieveById($identifier){
        $qry = User::where('id', '=', $identifier);
        if ($qry->count() > 0){
            $user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
                , 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
                , 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
            return $user;
        }
        return NULL;
    }


    /**
     * Retrieve a user by by their unique identifier and "remember me" token.
     *
     * @param  mixed $identifier
     * @param  string $token
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */

    public function retrieveByToken($identifier, $token){
        $qry = User::where('id', '=', $identifier)
            ->where('remember_token', '=', $token);
        if ($qry->count() > 0){
            $user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
                , 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
                , 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
            return $user;
        }
        return NULL;
    }


    /**
     * Update the "remember me" token for the given user in storage.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     * @param  string $token
     * @return void
     */

    public function updateRememberToken(Authenticatable $user, $token){
        $user->setRememberToken($token);
        $user->save();
    }


    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array $credentials
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */

    public function retrieveByCredentials(array $credentials){
        $qry = User::where('email', 'like', $credentials['email']);

        if ($qry->count() > 0){
            $user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
                , 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
                , 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
            return $user;
        }
        return NULL;
    }


    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     * @param  array $credentials
     * @return bool
     */

    public function validateCredentials(Authenticatable $user, array $credentials){
        $salt = base64_decode($user->passwordSalt);
        $password = $credentials['password'];
        $utf16Password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
        $calculatedPassword = base64_encode(sha1($salt . $utf16Password, true));
        if ($user->email == $credentials['email'] && $user->getAuthPassword() == $calculatedPassword){
            return true;
        }
        return false;
    }
}

?>

next, create a file 'CustomAuthProvider.php' in app/Providers directory.

<?php

namespace App\Providers;

use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;

class CustomAuthProvider extends ServiceProvider{

    /**
     * Bootstrap the application services.
     *
     * @return void
    */

    public function boot(){
        $this->app['auth']->extend('custom', function(){
            return new CustomUserProvider();
        });
    }


    /**
     * Register the application services.
     * 
     * @return void
    */

    public function register(){
        //
    }


}

?>

That's it.

查看更多
登录 后发表回答