I have two views, Student and Teacher, I have implemented a Socialite login with specific domain of organization(student@abc.edu.pk) WHen the student login it redirect to Home Page/View. And now i create Teacher which also go through same login system, Teacher also have same domain name account teacher@abc.edu.pk , I want when teacher Login it should redirect to another View, ( TeacharHome ) How can i do that, I try my hard but could not do that... Here
Route::get('/home', 'HomeController@index')->name('home');
Route::get('auth/{provider}', 'Auth\LoginController@redirectToProvider');
Route::get('auth/{provider}/callback', 'Auth\LoginController@handleProviderCallback');
Here My LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
use Auth;
use App\User;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo1 = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
public function handleProviderCallback($provider)
{
$user = Socialite::driver($provider)->stateless()->user();
$domain = explode("@", $user->email)[1];
if( $domain != 'abc.edu.pk')
{
return redirect($this->redirectTo1);
}
else
{
$authUser=$this->findOrCreateUser($user, $provider);
Auth::login($authUser, true);
return redirect($this->redirectTo);
//return $user->token;
}
}
public function findOrCreateUser($user, $provider)
{
$authUser = User::where('provider_id', $user->id)->first();
if($authUser)
{
return $authUser;
}
return User::create([
'name'=> $user->name,
'email'=> $user->email,
'provider'=> strtoupper($provider),
'provider_id'=>$user->id
]);
}
}
Try requesting upfront for the type. You can have a required radio button asking the type
<form method="post" action="auth/facebook">
<label>
Teacher
<input type="radio" required value="teacher" name="type" />
</label>
<label>
Student
<input type="radio" required value="student" name="type" />
</label><br>
<button type="submit">Login with Facebook</button>
</form>
You can then include the type param on your callback and use it to redirect the user to the correct page
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect(['type' => request()->input('type')]);
}
and finally, you redirect accordingly
public function handleProviderCallback($provider)
{
//... Make sure you also use role and Role Base Access for security
if (request()->query('type') === 'teacher') {
return redirect()->route('home.teacher')
}
return redirect()->route('home.student')
}
hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.