I am trying to build a real-time notification system with laravel 5.8, laravel-echo-server, redis and socket.io
i broadcast the login event, i have tow type of users student and prof
the authenticated function
protected function authenticated(Request $request, $user)
{
$event = new UserLoginEvent($user->id);
broadcast($event)->toOthers();
if ($user->isProf()) {
return redirect('/prof/dashboard');
}
return redirect('/etudiant/dashboard');
}
broadcast event
public function __construct($user)
{
$this->user = $user;
}
public function broadcastOn()
{
return new PresenceChannel('login');
}
channel.php
Broadcast::channel('login', function ($user) {
if(Auth::check()){
return ['id'=>$user->id];
}
});
in the prof controller i use these middelware
public function __construct()
{
$this->middleware(['auth','verified']);
$this->middleware('etudiant')->only(['search','rating']);
$this->middleware('prof')->only(['dashboard']);
$this->middleware('info')->except(['profil']);
}
prof middelware
public function handle($request, Closure $next)
{
if ( Auth::check() && Auth::user()->isProf() )
{
return $next($request);
}
return redirect('/');
}
and in the student controller i use
public function __construct()
{
$this->middleware(['auth','verified','etudiant']);
}
etudiant middelware
public function handle($request, Closure $next)
{
if ( Auth::check() && !Auth::user()->isProf() )
{
return $next($request);
}
return redirect('/');
}
when i am trying to connect with student compte the event broadcast normally, but with the prof compte im got this error
⚠ [2:52:13 PM] - uCUlo9fp3WNtRMgBAAAA could not be authenticated to presence-login
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='http://localhost:8000/login'" />
<title>Redirecting to http://localhost:8000/login</title>
</head>
<body>
Redirecting to http://localhost:8000/login.
</body>
</html>
Client can not be authenticated, got HTTP status 302
Related
I have a custom middleware CheckIfUserHasStore that checks if user has created a store.
public function handle(Request $request, Closure $next)
{
$store = Store::whereUserId(\auth()->user()->id)->first();
if (!$store) {
return redirect()->route('owner.stores.create');
}
return $next($request);
}
and in my web.php with a middleware has_store
...
Route::middleware(['auth', 'role:store_owner', 'has_store'])->group(function () {
...
});
I keep getting this error on browser localhost redirected you too many times.. How can I resolve this?
I am new on laravel. I am actually having a problem with my middleware. I already registered it on Kernel.php file.
public function handle($request, Closure $next){
$user = Auth::user();
if($user->hasAnyRole('school'))
{
return $next($request);
}
return redirect('login');
}
here is my User model
public function roles(){
return $this->belongsToMany('App\Role');
}
public function hasAnyRoles(){
return null !== $this->roles()->whereIn('name', $roles)->first();
}
public function hasAnyRole(){
return null !== $this->roles()->where('name', $role)->first();
}
Try flipping the logic and also checking if logged in.
Do a (Auth::check() :
public function handle($request, Closure $next){
if (Auth::check()) {
$user = Auth::user();
if($user->hasAnyRole('school'))
{
return $next($request);
}
} else {
return redirect('login');
}
}
I am trying to using middleware in laravel but it is giving this error
"The page isn’t redirecting properly"
i have done this
IsAdmin(middleware)
public function handle($request, Closure $next)
{
$user=Auth::user();
if($user->isAdmin())
{
return redirect()->intended('/admin');
}
return $next($request);
}
}
admin controller
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('IsAdmin');
}
public function index()
{
return "You are an administrator because you are seeing this page";
}
}
User Model
public function isAdmin()
{
if($this->role->name=='administrator')
{
return true;
}
return false;
}
web.php
Route::get('/admin','AdminController#index');
"The page isn’t redirecting properly"
Try changing return $next($request); to return redirect()->back();, it might be infinite redirect loop, because you are still trying to access the same route even if middleware fails.
call back route: (I test it with google)
Route::namespace('Auth')->group(function (){
$this->get('login/{provider}/callback', 'LoginController#handleProviderCallback');
...
}
in the LoginController class
public function __construct()
{
$this->middleware('guest')->except('logout');
}
and callback function
public function handleProviderCallback(Request $request, $provider)
{
$social_user = Socialite::driver($provider)->stateless()->user();
$user = User::whereEmail($social_user->getEmail())->first();
auth()->loginUsingId($user->id);
$request->session()->flash('alert', 'hello '.$user->name;);
return back();
}
It always redirect to home page and without alert session.
I found because of this code in
app\Http\Middleware\RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
return redirect('/home');
}
how can do it? I want run this piece of code
$request->session()->flash('alert', 'hello '.$user->name;);
return back();
not this:
return redirect('/home');
You can comment out 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, line in your app/Http/Kernel.php file and you won't be having that redirect
I have Laravel 5.4 Base Controller which should share along children Controllers some common data depending on current Authenticated user.
I was Trying to get it like
public function __construct(ValidationFactory $validation)
{
$this->middleware(array('auth', 'lockscreen'));
var_dump(\Auth::user());
die;
}
this do not works.
private $userId;
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->userId = Auth::user()->id;
return $next($request);
});
}