So i changed config/auth.php to change default auth table from user to accounts like this :
'defaults' => [
'guard' => 'web',
'passwords' => 'accounts',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'accounts',
],
'api' => [
'driver' => 'token',
'provider' => 'accounts',
'hash' => false,
],
],
'providers' => [
'accounts' => [
'driver' => 'eloquent',
'model' => App\Akun::class,
],
],
i have changed App\Akun Model configuration same as App\User Model :
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Akun extends Authenticatable
{
use HasFactory;
protected $table = 'accounts';
protected $fillable = [
'nip',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
And My Login system is working too. It can return back if the NIP or Password wrong and give error message too. Here my login code:
function checklogin(Request $request){
$this->validate($request,[
'nip' => 'required',
'password' => 'required'
]);
$akun_data = array(
'nip' => $request->get('nip'),
'password' => $request->get('password')
);
if(Auth::attempt($akun_data)){
return redirect('login/successlogin');
}else{
return back()->with('pesan','NIP atau Password salah');
}
}
it was working and return correctly to login/successlogin and run the code in there. But when i tried to add middleware('auth') to the route of login/succeslogin , it'll always return back to login page even when i gave the correct nip and password
Route::get('/login/successlogin',[LoginController::class, 'successlogin'])->middleware('auth');
You can just add a new guard that will be for your accounts table and call it 'accounts' and then in your routes i.e. you can provide route grouping by guard i.e.
Route::middleware('auth:accounts')->group(function () { ...other protected routes... });
I have solved this, My way to change default auth table is correct. But I forgot to add id column there. just add this code in your New Default Auth Table Migration
$table->id();
Related
i have created a second model for authenticating as company, i add the guard and provide
but enable to login!
Company class :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
class Company extends Authenticatable
{
use HasFactory;
protected $guard = 'company';
protected $guarded = [];
}
Config/Auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'company' => [
'driver' => 'session',
'provider' => 'company',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'company' => [
'driver' => 'eloquent',
'model' => App\Models\Company::class,
],
],
CompanyLoginController
use App\Http\Controllers\Controller;
use App\Models\Entreprise;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CompanyLoginController extends Controller
{
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::guard('company')->attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('dashboard');
}
return back()->withErrors([
'email' => 'The provided credentials do not match our records.',
]);
}
}
the $hasher->check() was the one returning false after going deep in te code but for some reason i couldn't find the solution.
as you said you builtin function is not working so you can try manual method like this
public function authenticate(Request $request)
{
$company = Company::where('email', $request->email)->first();
if (!$company) {
return back()->withErrors([
'email' => 'The email doest not exist.',
]);
}
if (Hash::check($request->password, $company->password)) {
auth()->guard('company')->login($company);
return redirect()->intended('dashboard');
}
return back()->withErrors([
'email' => 'The provided credentials do not match our records.',
]);
}
I have multi-authentication set up in my laravel app. I am trying to create multiple authentication using default authentication laravel 5.8. I have two tables one is users and other is admins. I have configured the guards for admin. User login works fine, no issues but when I try to login the admin, it doesn't work even if I login with correct credentials. Password field validation works if I use less then 6 character. Please help me to solve this problem.enter code here
My Admin model is
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable {
use Notifiable;
protected $guard = 'admin';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password', 'title',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Guard setting is
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
],
AdminLoginController is
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;
class AdminLoginController extends Controller {
public function __construct() {
$this->middleware('guest:admin')->except('logout');
}
public function showLoginForm() {
return view('auth.admin-login');
}
protected function guard() {
return Auth::guard('admin');
}
public function login(Request $request) {
//validate the form
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6',
]);
//attemp to login
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
//Authentication passed...
return 'success';
//return redirect()
//->intended(route('admin.dashboardsdsdsd'));
//if login success then redirect to page
// if not success then redirect to back
}
return redirect()->back()->withInput($request->only('email', 'remember'));
}
}
Route is
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('admin', 'AdminController#index')->name('admin.dashboard');
Route::get('admin/login', 'Auth\AdminLoginController#showLoginForm')->name('admin.login');
Route::post('admin/login', 'Auth\AdminLoginController#login')->name('admin.login.submit');
Please help me to resolve this issue, so that admin can login.
There are two main reasons:
First one: your hash password is not correct, so open this website ( which is MD5 Hash Generator) then put any number that you like, take it and create a new admin account directly from database and paste the password then try
Second one: Clear your cache and view :
php artisan view:clear
php artisan cache:clear
In my laravel application, I need to make a login to customers table. So, I made a CustomerLoginController.
My CustomerLoginController looks like:
class CustomerLoginController extends Controller
{
public function login(Request $request)
{
// Validate the data
$this->validate($request,[
'email' => 'required|email',
'password' => 'required'
]);
$credentials=[
'email' => $request->get('email'),
'password' => $request->get('password')
];
// Log The Customer In
if (Auth::guard('customer')->attempt(['email'=> $request['email'],'password' => $request['password']]))
{
// If Authentication passed...
dd($credentials);
return redirect(route('first'));
}
// If Authentication not successful, redirect back to login form with the inputs
return redirect()->back()
->withInput($request->only($this->username()));
}
public function username()
{
return 'email';
}
I have setup the customer guard and providers in the config/Auth.php. My config/Auth.php file:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
],
],
The Problem is that even when the credentials are correct the login attempt is always unsuccessful.
What am I missing?
And what about Customer Model.
it should extend Authenticatable same as User Model.
class User extends Authenticatable
{
/* your code */
}
class Customer extends Authenticatable
/* your code */
{
}
and Don't Forgot to use Authenticatable in Model. :)
use Illuminate\Foundation\Auth\User as Authenticatable;
I set up Laravel Passport and currently I am trying to register user with a Post Route. I did create a RegisterController inside Controllers/Api/Auth.
Thus I created a clients table which looks excatly like a users table.
The client gets created if I call the route, but I do not get an access token nor a refresh token.
The route to my controller looks like this (routes/api):
Route::post('register', ['as' => 'register', 'uses' => 'Api\Auth\RegisterController#register']);
My Controller looks like this:
<?php
namespace App\Http\Controllers\Api\Auth;
use App\Client;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use Laravel\Passport\Client as PClient;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
private $client;
public function __construct() {
$this->client = PClient::find(1);
}
public function register(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6|confirmed'
]);
$client_user = Client::create([
'name' => request('name'),
'email' => request('email'),
'password' => bcrypt(request('password'))
]);
$params = [
'grant_type' => 'password',
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'username' => request('email'),
'password' => request('password'),
'scope' => '*'
];
$request->request->add($params);
$proxy = Request::create('oauth/token', 'POST');
return Route::dispatch($proxy);
}
}
This is my Client Model:
class Client extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, HasApiTokens, Notifiable;
protected $table = 'clients';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
When I am trying to call it with Postman I get this error message:
I may be way off basis here but it looks as if you are creating your client with a password of "password" due to your bcrypt('password') call.
Should it not be bcrypt(request('password'))?
This would explain why your credentials are wrong in your request, because they are ; )
Ok I fixed it, the post route worked if I used the User Model instead of my Client model, so I guessed that there has to be something different.
After some research I have found out that one needs to add the model, in my case the client model to the providers array inside config/auth.php.
So first one needs to change the api guard like this:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'clients',
],
],
This way to api routes login and register only take action with my clients.
Now you need to a a new provider in this case a clients provider like this.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Client::class
],
],
And voila I get an access token + refresh token if I call the route.
I'm currently using the laarvel5.4 authentication in my application; and I want to change the users table name while keeping its role as it is in the authentication logic, all I need is just to change the name.
It seems that Laravel changer the Auth file and code structure in the latest version, so auth.php doesn't really look as in the previous versions of laravel.
I have done the following so far, but it's still not working gy giving me an error saying that the table users doesn't exist:
1- I have changed the migration's up() and down() functions to create and drop staff table instead of users and run the migration successfully.
2- I have changed the validator() function in RegisterController.
3- I have changed all the 'users' to 'staff' in config/auth.php, as shown in the code:
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'staff',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'staff',
],
'api' => [
'driver' => 'token',
'provider' => 'staff',
],
],
'providers' => [
'staff' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// 'staff' => [
// 'driver' => 'database',
// 'table' => 'staff',
// ],
],
'passwords' => [
'staff' => [
'provider' => 'staff',
'table' => 'password_resets',
'expire' => 60,
],
],
];
However, in app/User.php I don't know what to change since in the previous versions there used to be a table variable which u need to change its value from users to the new table name but in my class I don't have such thing
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
You can change the table name in the migration file and then change the table name variable in the User.php model.
Example:
class Flight extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'my_flights';
}
https://laravel.com/docs/5.4/eloquent#eloquent-model-conventions
You need just change in two places
1.add this line after hidden array of app/User.php
protected $hidden = [
'password', 'remember_token',
];
protected $table = 'another_table_name';
2.In the RegisterController change the table name in the validator method:
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:another_table_name',
'password' => 'required|string|min:6|confirmed',
]);
}