3 table authentication in laravel 5.2 - php

can we do a 3 table authentication in laravel 5.2 like i have role for Admin, Teacher, Student and i want authentication for these 3 tables separately
At this time i have 2 table and these are authenticated separately and working fine
this is my code for auth.php
'defaults' => [
'guard' => 'user',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'user' => [
'driver' => 'session',
'provider' => 'users'
],
'admin' => [
'driver' => 'session',
'provider' => 'admins'
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'email' => 'admin.auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
AdminController.php file is look like
class AdminController extends Controller
{
public function __construct(){
$this->middleware('admin');
}
public function index(){
// return Auth::guard('admin')->user();
return view('admin.dashboard');
}
public function Profile()
{
$adminData = Auth::guard('admin')->user();
return view ('admin.add', compact('adminData'));
}
}
HomeController.php file is here
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('auth.dashboard');
}
}
Route file is look like this
Route::group(['middleware' => ['web']], function(){
Route::auth();
Route::get('/home', 'HomeController#index');
});
Route::group(['middleware' => ['web']], function () {
//Login Routes...
Route::get('/admin/login','AdminAuth\AuthController#showLoginForm');
Route::post('/admin/login','AdminAuth\AuthController#login');
Route::get('/admin/logout','AdminAuth\AuthController#logout');
// Registration Routes...
Route::get('admin/register', 'AdminAuth\AuthController#showRegistrationForm');
Route::post('admin/register', 'AdminAuth\AuthController#register');
Route::get('/admin', 'AdminController#index');
Route::get('/profile', 'AdminController#Profile');
});
Now i want to add another table and do builtin authentication for this new table

Related

Error Multy Auth Laravel 6, i can login, but i can't access my home

I use laravel 6, I make multiple logins, my multi logins are running, but after logging out, I get an error like this on my welcome page
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct()
must implement interface Illuminate\Contracts\Auth\UserProvider, null
given, called in
D:\xampp2\htdocs\alkit\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php
on line 125
auth.php
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
// Guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admin',
],
'camp' => [
'driver' => 'session',
'provider' => 'camp',
],
'camp-api' => [
'driver' => 'token',
'provider' => 'camp',
],
'user' => [
'driver' => 'session',
'provider' => 'user',
],
'user-api' => [
'driver' => 'token',
'provider' => 'user',
],
],
// Providers
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'camp' => [
'driver' => 'eloquent',
'model' => App\Camp::class,
],
],
// Password
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
web.php
<?php
Route::get('/home', function () {
return view('index');
});
// hanya untuk tamu yg belum auth
Route::get('/login', 'LoginController#getLogin')->name('login')->middleware('guest');
Route::post('/login', 'LoginController#postLogin');
Route::get('/logout', 'LoginController#logout');;
Route::get('/admin', function() {
return view('admin');
})->middleware('auth:admin');
Route::get('/user', function() {
return view('user');
})->middleware('auth:user');
Route::get('/camp', function() {
return view('camp');
})->middleware('auth:camp');
LoginCobtroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Admin;
use App\User;
use App\Camp;
use Auth;
class LoginController extends Controller
{
public function getLogin()
{
return view('login');
}
public function postLogin(Request $request)
{
// Validate the form data
$this->validate($request, [
'email' => 'required|email',
'password' => 'required'
]);
// Attempt to log the user in
// Passwordnya pake bcrypt
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password])) {
// if successful, then redirect to their intended location
return redirect()->intended('/admin');
} else if (Auth::guard('user')->attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect()->intended('/user');
} else if (Auth::guard('camp')->attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect()->intended('/camp');
}
return redirect()->route('login');
}
public function logout()
{
if (Auth::guard('admin')->check()) {
Auth::guard('admin')->logout();
} else if (Auth::guard('user')->check()) {
Auth::guard('user')->logout();
} else if (Auth::guard('camp')->check()) {
Auth::guard('camp')->logout();
}
return redirect('/home');
}
}
The provider is named wrong. You should use like this
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'camps' => [
'driver' => 'eloquent',
'model' => App\Camp::class,
],
],
Change the web guards configuration to use your adjusted user provider ... by default it wants to use a provider named users which you do not have.
'web' => [
'driver' => 'session',
'provider' => 'user',
],
If you are not going to be using the web guard, you should not have it set as the default. The passwords section is an issue since the only configuration you have there is set to use the users provider, which you do not have.
Similarly: https://stackoverflow.com/a/58896116/2109233
Also:
Route::get('/login', 'LoginController#getLogin')->name('login')->middleware('guest');
Since you have not specified a guard to guest it will use the default guard which is web. So everyone who isn't logged in via the web guard can reach this route.
Having said all of that ... it is probably easier to just change the user provider to be named users and adjust your user and user-api guards to use the users provider.

Auth::guard('admin')->user() return always null

I define a new guard "Admin" to have a multi Auth System User and admin in my project . when i login it is ok and i get the token to the rest of my api routes .When when I use The auth::guard('Admin')->user() i got always null
Any suggestion??
Auth.php :
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
Login method :
public function ALogin(Request $request){
// dd($request);
// dd($rrequest->header);
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {
$this->connectedAdmin = Auth::guard('admin')->user();
// dd( $connectedAdmin);
$success['token'] = $this->connectedAdmin->createToken('MyApp')->accessToken;
$return = new \stdClass();
$return->token = $success['token'];
//dd(self::$connectedAdmin);
return response()->json($return, 200);
}
// dd(Auth::guard('admin')->user());
return response()->json("error", 400);
}
*/
In your route file, to specify a guard for several endpoints you coud do:
/** routes/api.php */
Route::group(['guard' => 'admin'], function () {
Route::get('/a-route', 'AController#method');
// ...
});

Laravel - Getting wrong guard model when I try to use multi authentication

I'm having some problems with Laravel 5.7 multi-auth implementation.
I have created a new table for backend login: "AdminUsers".
So, I modified auth.php file:
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => Ecommerce\User::class,
],
'admins' => [
'driver' => 'eloquent',
'table' => Ecommerce\AdminUser::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
],
];
Then, I create the AdminLoginController class:
<?php
namespace Ecommerce\Http\Controllers\Admin\Auth;
use Illuminate\Http\Request;
use Ecommerce\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class AdminLoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/index';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginForm()
{
return view('admin.auth.login');
}
protected function attemptLogin(Request $request)
{
return $this->guard('admin')->attempt(
$this->credentials($request), $request->filled('remember')
);
}
protected function authenticated(Request $request, $user)
{
dd('authenticated!');
}
public function username()
{
return 'username';
}
}
I setted this routes in routes file:
Route::get ('/login', ['uses'=>'Auth\AdminLoginController#showLoginForm'])->name('login_page');
Route::post('/login', ['uses'=>'Auth\AdminLoginController#login' ])->name('do_login' );
The problem is, that when I do login, Laravel tries to use "users" guard instead of the defined "admin" guard. So that throw a SQL error (because I have not created the "Users" table in the DB).
Even if I do var_dump($this->guard('admin')) inside AdminLoginController::attemptLogin method, the response gives me a SessionGuard object with "Ecommerce\User" instead of "Ecommerce\AdminUser" that is the one defined as "admin" in auth.php guard array.
Anyone knows where else can I look to solve this?
Thanks!
The problem lies in your auth.php providers section.
'table' => Ecommerce\AdminUser::class
'table' should be replaced with 'model'.

Laravel 5.3 admin guard not working

I am trying to login from two different model using same login form. I have defined admin guard in config/Auth.php. But when I define admin guard in Foundation/AuthenticateUsers it checks the database table to validate the user but redirects back to same login form.
config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
Foundation/AuthenticatUsers
protected function guard()
{
return Auth::guard('admin');
}
public function login(Request $request)
{
$credentials = $this->credentials($request);
if (Auth::guard('web')->attempt($credentials, $request- >has('remember'))) {
return $this->sendLoginResponse($request);
}
elseif(Auth::guard('admin')->attempt($credentials, $request->has('remember')))
{
return $this->sendLoginResponse($request);
}
}
Admin guard redirects to login page because of middleware auth, i think you need to do something like this
public function __construct()
{
$this->middleware('auth:admin');
}
Read this Protecting Routes, part "Specifying A Guard"

How to Use two Laravel Default Guard?

I'm trying to login an Admin and a User with the same form, but the config/auth I just only can to set a one default
This is my config/auth
'defaults' => [
'guard' => 'web_users',
'passwords' => 'users',
],
'guards' => [
'web_users' => [
'driver' => 'session',
'provider' => 'users',
],
'web_admins' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
When I log in a User with the default "web_users" I can retrieve his data with the method Auth::user(), but if I log in a Admin with the default "web_admins" can't retrieve anything.
Routes:
Route::resource('log','LogController');
Route::get('logout','LogController#logout');
Log Controller
public function store(Request $request)
{
if(Auth::guard('web_users')->attempt(['email'=>$request['email'],'password'=>$request['password']])) {
return Redirect::to('/');
}
if(Auth::guard('web_admins')->attempt(['email'=>$request['email'],'password'=>$request['password']])) {
return Redirect::to('/');
}
}
public function logout()
{
Auth::logout();
return Redirect::to('log');
}
Note: sorry for my english, I don't speak it very well
try this for Routes file
Route::group(['middleware' => 'auth:web_users,web_admin'], function () {
Route::resource('log','LogController');
Route::get('logout','LogController#logout');
});

Categories