lundi 4 janvier 2016

Laravel multiauth md5 and primary key string

currently i have 1 project need to use extend mysql database which has a lot of data and password is md5 encrypted and the MEMBER_ID(primary key) is string like this

5e3bb0bdb70e4f139c132cc32daace0e

the password field name PASSWORD and the primary key name MEMBER_ID

so i set the eloquent model like this

<?php

namespace App\Models;

use Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class Member extends BaseModels implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;

    protected $connection = 'mysql_db2';

    protected $table = 'tb_member';

    protected $primaryKey = 'MEMBER_ID';

    protected $fillable = [];

    public $incrementing = false;

    protected $hidden = ['PASSWORD'];

    public function getAuthIdentifier() {
        return $this->MEMBER_ID;
    }

    public function setPASSWORDAttribute($value) {
        $this->attributes['PASSWORD'] = static::encryptPassword($value);
    }

    public function setPassword($password) {dd('set');
        $this->PASSWORD = static::encryptPassword($password);
    }

    public function getAuthPassword() {dd('get');
        return $this->PASSWORD;
    }

    public static function encryptPassword($password) {
        return md5($password);
    }

    public function checkLogin($username, $password) {
        if ($this->USERNAME == $username && $this->PASSWORD == static::encryptPassword($password)) {
            return true;
        } else {
            return false;
        }
    }
}

i search around and found some post mention to add the getAuthPassword() and setPassword() method, but i die dump there it also pass, so i think is deprecated in laravel 5.2?

and here is my controller

<?php

namespace App\Http\Controllers\Auth;

use App\Models\Member;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Auth;

class MemberController extends Controller {
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    public function login(Request $request) {
        $username = $request->get('username');
        $password = $request->get('password');

        try {
            $member = Member::where('USERNAME', '=', $username)->first();
            dd(Auth::guard('member')->attempt([
                'USERNAME' => $username,
                'PASSWORD' => $password
            ]));
            if ($member) {
                if ($member->checkLogin($username, $password)) {

                    Auth::guard('member')->login($member);
                    if (Auth::guard('member')->check()) {
//                        dd(Auth::guard('member')->check());
                        return redirect()->route('frontend.index');
                    } else {
                        return redirect()->route('frontend.index')->withErrors();
                    }
                } else {
                    return 'Incorrect password';
                }
            } else {
                return 'Username not found';
            }
            $d = new Member();
            $d->PASSWORD = 123456;

            $credentials = [
                'USERNAME' => $username,
                'PASSWORD' => $password
            ];
            if (Auth::guard('member')->attempt($credentials)) {
                return 'successfully login';
            } else {
                return 'unable to login';
            }
            return 'hehe';
//            dd($username, $password);
        } catch (\Exception $e) {
            dd('FILE: ' . $e->getfile() . 'LINE: ' . $e->getLine() . '<br />' . $e->getMessage());
        }
    }
}

the Auth::guard('member')->attempt() always return false

the Auth::guard('member')->login($member) always return null

Auth::guard('member')->check() return true when the page not refresh, but once refresh(or go another route/page) the Auth::guard('member')->check() will then return false

i cannot change the password encrypt method, i cannot add a auto increment field or edit the primary key to the member table because another website maintainer say it might cause his JAVA application error, so any idea without changing any table structure?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1mxJIyh
via IFTTT

Aucun commentaire:

Enregistrer un commentaire