Reputation: 304
have multiple users with totally different post login panels. One for teachers and other for students. I made middlewares to separate their views. Middleware checks if a property ( column in database ) named entity is 'student' or 'company' and restricts or permits views accordingly. And yes Guest isnt permitted to either of the post login panel.
I am using same table to save logins. (entity column differentiates if its a teacher or student). Now I want to redirect the users to different views. If I alter $redirectTo = "/studentPanel"
. Middleware acts and teacher login cannot access this . But if I hard code $redirectTo = "/teacherPanel"
, then student post login panel is not accesible.
How can set $redirectTo
dynamically . I thought of setting in construct method of auth controller.
Tried this:
public function __construct(Request $request ) {
$this->middleware('guest', ['except' => 'getLogout']);
if(Auth::user() && $request->user()->isStudent() )
$this->redirectTo = "/studentPanel";
elseif(Auth::user() && $request->user()->isTeacher() )
$this->redirectTo = "/teacherPanel";
else
$this->redirectTo = "/auth/login";
}
Here isStudent()
and isCompany are functions in App\User which respond with true or false checking the entity column value in Database.
I thought this way and I am getting error "Call to a member function isStudent() on a non-object"
Upvotes: 2
Views: 3471
Reputation: 2710
You need to change $request->user()
to Auth::user()
.
From there, I have faced a very similar issue and the best solution I found at the time was to set $redirectTo
to a static value, and in the route you find whether the user is a student or teacher and redirect appropriately.
The route would look like this:
public function getRedirect()
{
if (Auth::user() && Auth->user()->isStudent()) {
return redirect("/studentPanel");
} else if(Auth::user() && Auth->user()->isTeacher()) {
return redirect("/teacherPanel");
} else {
return redirect("/auth/login");
}
}
Upvotes: 1