Customize Authentication in Laravel [closed]

人走茶凉 提交于 2019-12-23 06:10:47

问题


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:


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.



来源:https://stackoverflow.com/questions/32769323/customize-authentication-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!