How to authenticate and logging in user in laravel using guard? - laravel

I'm trying to make authentication using guard in Laravel 5.8. Authentication is passed but somehow it's not logging in the user.
public function login(Request $request)
{
$email = $request->email;
$password = $request->password;
$credentials = $request->only('email','password');
if (Auth::guard('owner')->attempt($credentials,$request->remember)){
//echo "Authentication is passed";
return redirect()->intended('/owner/dashboard');
}
return redirect('/owner')->with('error','Login failed.');
}
When redirected to route /owner/dashboard which is filtered with $this->middleware('owner'), the user will be redirected to login form and get notification that login failed. Is Auth::guard('owner')->attempt($credentials) only authenticating without logging in user?

in config/auth.php add this
add new guard and the provider for this guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'owner' => [
'driver' => 'session',
'provider' => 'owner',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
```
```
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'owner' => [
'driver' => 'eloquent',
'model' => App\Owner::class,
],
],
```
```
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'owner' => [
'provider' => 'owner',
'table' => 'password_resets',
'expire' => 60,
],
],
```
Follow this article
https://medium.com/#sagarmaheshwary31/laravel-multiple-guards-authentication-setup-and-login-2761564da986

Related

Is there way to fix argument 2 passed to Tymon\\JWTAuth\\JWTGuard::__construct() using JWT and Laravel?

I have problem on my jwt authentication. When I hit the api for the login of admin it shows error on my postman says that "message": "Argument 2 passed to Tymon\JWTAuth\JWTGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given.
I read some threads regarding on this, they said that there is problem on auth.php of laravel I guess it is providers.
Here is what is inside on my auth.php and my controller.
Controller
public function __construct()
{
Config::set('jwt.user', Admin::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Admin::class,
]]);
}
Auth.php
'guards' => [
// 'web' => [
// 'driver' => 'session',
// 'provider' => 'users',
// ],
'api' => [
'driver' => 'jwt',
'provider' => 'assureds',
'hash' => false,
],
'admins' => [
'driver' => 'jwt',
'provider' => 'admins',
],
],
Provider:
'providers' => [
'assureds' => [
'driver' => 'eloquent',
'model' => App\Assured::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],

how to solve Laravel 6.0 error upload file

I'm trying to input some data to database
i cant input some data to database because i got that error
but i can login with multi auth in my project
and i got some error
how i can solve this error?
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Jurusan;
use Auth;
class AdminActionsController extends Controller
{
public function addjurusan(Request $request)
{
$jurusan = new Jurusan();
$jurusan->nama_jurusan=$request->nama;
$file=$request->file('fotohimpunan');
if (!$file) {
return redirect()->route('in.jurusan')->with('alert','foto harus diisi!');
}
$file_name=$file->getClientOriginalName();
$path=public_path('/img');
$file->move($path,$file_name);
$jurusan->fotohimpunan='public/img/'.$file_name;
$jurusan->status='disable';
// dd($jurusan);
$jurusan->save();
return redirect()->route('in.jurusan');
}
}
guard
<?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',
],
'panitia' => [
'driver' => 'session',
'provider' => 'panitia',
],
'panitia-api' => [
'driver' => 'token',
'provider' => 'panitia',
],
'mahasiswa' => [
'driver' => 'session',
'provider' => 'mahasiswa',
],
'mahasiswa-api' => [
'driver' => 'token',
'provider' => 'mahasiswa',
],
],
// Providers
'providers' => [
'panitia' => [
'driver' => 'eloquent',
'model' => App\Panitia::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'mahasiswa' => [
'driver' => 'eloquent',
'model' => App\Mahasiswa::class,
],
],
// Password
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
how i can solve my problems please help me
i can login with multi auth but if i try to input some data i got that error
You don't have a users provider:
'providers' => [
'panitia' => [
'driver' => 'eloquent',
'model' => App\Panitia::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'mahasiswa' => [
'driver' => 'eloquent',
'model' => App\Mahasiswa::class,
],
],
The web guard is set to use a provider named users:
'web' => [
'driver' => 'session',
'provider' => 'users',
],
You need to adjust that to use a provider you have registered, or add a users provider.
Are you intending to be using the web guard for which ever route you are hitting and getting the error? It is possible you intend to use a different guard completely.

Laravel Login to Admin dashboard redirect to 404 page after enable AuthenticateSession middleware

Im working in simple blog site using laravel which has 2 parts , blog site for users and admin dashboard
It is all works good before but after uncomment below line in Http/Kernal.php
\Illuminate\Session\Middleware\AuthenticateSession::class,
failed to login and redirect to admin dashboard and it is redirect to 404 page.
I just want to use AuthenticateSession middleware because , i want to use
logoutOtherDevices function for website users
Auth::logoutOtherDevices($request->input('new-password'));
when i comment out this line \Illuminate\Session\Middleware\AuthenticateSession::class, again it is working normal .
please help me to solve this issue .
I have users table used for login users and admin table for login admin to dashboard
here is table for admin :
here is config/auth.php :
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Model\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Model\Admin::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
]
],
];
here is admin login function :
public function login(Request $request)
{
// Validate the form data
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password,'status'=>1], $request->remember)) {
// if successful, then redirect to their intended location
return redirect()->intended(route('admin.dashboard'));
}else{
// if unsuccessful, then redirect back to the login with the form data
return redirect()->back()->withInput($request->only('email', 'remember'));
}
}

laravel call to undefined method Auth guard

i want to prevent the login after register in Laravel 5.5, I already did this by commenting a line:
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
// $this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
i override it in RegisterController.php
i got this error:
Call to undefined method Illuminate\Auth\AuthenticationException::guard()
$guard = array_get($exception->guard(),0);
switch ($guard) {
case 'admin':
return redirect()->guest(route('admin.login'));
break;
default:
return redirect()->guest(route('login'));
break;
}
Here is the content of my config/auth:
<?php
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' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
],
];
i have enabled multi-auth system which is i have an admin login and a user login, what i wan't is to disable the login after register in my user page.
To check guard in the exception can do something like this:
return redirect(route(auth()->guard('admin')->check() ? 'admin.login' : 'login'));
Also, use auth() helper or Auth:: facade in the RegisterController#register if you're trying to override the method:
auth()->guard('admin')->login($user);

Laravel 5.2 Multi Table Authentication ErrorException in AuthManager.php

I use Laravel 5.2 and I need to use multi table authentication. I read from this link Can anyone explain Laravel 5.2 Multi Auth with example
I modified config/auth.php
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
//User Providers
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
//Resetting Password
'passwords' => [
'user' => [
'provider' => 'user',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admin' => [
'provider' => 'admin',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
Here is part of the controller for login (post method)
$admindata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($admindata)) {
echo 'SUCCESS!';
} else {
$admin = Auth::admin();
return Redirect::to('/b');
}
But I got this error
ErrorException in AuthManager.php line 288: call_user_func_array()
expects parameter 1 to be a valid callback, class
'Illuminate\Auth\SessionGuard' does not have a method 'admin'
It looks like the error is on Auth::attempt(). How to solve this error?
I believe the error is not in attempt method but here:
$admin = Auth::admin();
You try to run here admin method and obviously there is no such method in Illuminate\Auth\SessionGuard class.

Resources