I am trying to implement laravel 5.4 auth. I am using mongoDB database. Collection name is user authenticated field is usrName and usrPassword. Password is not created using bcrypt. I am retrieving password using my own method. Currently I have implemented auth manually but login is not working. Please check and help me. Thanks.
For connecting mongodb and laravel I am using a package "http://ift.tt/2syFbmB"
web.php
Auth::routes();
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
login.blade.php (just added only important things)
<form role="form" method="POST" action="">
<input id="username" type="text" class="form-control" name="username" placeholder="Username" value="" required autofocus>
<input id="password" type="password" class="form-control" name="password" placeholder="Password" required>
<button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
</form>
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use App\User;
use Illuminate\Http\Request;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/admin/dashboard';
public function username()
{
return 'usrName';
}
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginForm()
{
return view('auth.login');
}
public function login(Request $request)
{
$this->validate($request, [
'username' => 'required|max:255',
'password' => 'required|max:255'
]);
$authUser = User::where('usrName', $request->username)->first();
if (isset($authUser)) {
$password = md5('aFGQ475SDsdfsaf2342' . $request->password . $authUser->usrPasswordSalt);
if (Auth::attempt([$this->username() => $request->username, 'password' => $password])) {
return 'logged in successfully : '.$this->username() . ' - ' . $password;
}
else {
return 'oops something happend : '.$this->username() . ' - ' . $password;
}
}
}
auth.php (config)
'defaults' => [
'guard' => 'web',
'passwords' => 'user',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'user',
],
'api' => [
'driver' => 'token',
'provider' => 'user',
],
],
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'passwords' => [
'user' => [
'provider' => 'user',
'table' => 'password_resets',
'expire' => 60,
],
],
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Eloquent implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
use Notifiable;
protected $collection = 'user';
public $timestamps = false;
protected $fillable = [
'usrName', 'usrPassword',
];
protected $hidden = [
'usrPassword', 'remember_token',
];
public function getAuthPassword()
{
return $this->usrPassword;
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire