I try to authenticate using a custom table from database.
Table name is kullanicilar and the fields I use for the authentication are eposta and sifre respectively.
My session controller class is like this:
class OturumDenetcisi extends Controller
{
public function kaydet(Request $request)
{
if (!\Auth::attempt(['eposta' => $request->eposta, 'password' => $request->sifre]))
{
return back();
}
return redirect()->home();
}
// ...
}
Also in user model I have
# In user model
public function getAuthPassword ()
{
return $this->sifre;
}
When I try to authenticate user nothing happens. It takes me back.
Here is the dd ($request->all());
output inside kaydet function which tries to store a new session.
array:3 [▼
"_token" => "KfbftPRMVkm4bBIWo8WoICOHmaDyvRpDhF8Wk4zq"
"eposta" => "elma@elma.com"
"sifre" => "a123"
]
UPDATE : [SOLVED]
After saving the passwords to database using hashing methods now it works as excepted. Here is the complete session controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Kullanici;
use Illuminate\Support\Facades\Hash;
class OturumDenetcisi extends Controller
{
public function __construct()
{
$this->middleware('guest', ['except' => 'cikisYap']);
}
public function olustur()
{
return view('oturumlar.olustur');
}
public function kaydet(Request $request)
{
if (!\Auth::attempt(['eposta' => $request->eposta, 'password' => $request->sifre]))
{
return back();
}
return redirect()->home();
}
public function cikisYap()
{
auth()->logout();
return redirect('/');
}
}
First you have to make sure you are using the proper username column to search for credentials:
<?php
namespace App\Data\Entities;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'eposta';
}
}
Then you also have to check if your user is being found with the credentials you are sending to Laravel, so this is a way to test it:
public function kaydet(Request $request)
{
$user = User::where('eposta', $request->eposta)->first();
dump($user); // does it find the user?
dd(Hash::check($request->sifre, $user->password)); // is the password right?
if (!\Auth::attempt(['eposta' => $request->eposta, 'password' => $request->sifre]))
{
return back();
}
return redirect()->home();
}