Skip to content

Instantly share code, notes, and snippets.

@devhoussam
Last active January 5, 2021 15:59
Show Gist options
  • Save devhoussam/1feef5e279b8acfc039d9d593740bdb6 to your computer and use it in GitHub Desktop.
Save devhoussam/1feef5e279b8acfc039d9d593740bdb6 to your computer and use it in GitHub Desktop.
// app\Http\Controllers\Auth\RegisteredUserController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;
class RegisteredUserController extends Controller
{
/**
* Display the registration view for student.
*
* @return \Illuminate\View\View
*/
public function createStudent()
{
$role = Role::create(['name' => 'student']);
return view('auth.student.student-space', compact('role'));
}
/**
* Display the registration view for instructor.
*
* @return \Illuminate\View\View
*/
public function createInstructor()
{
$role = Role::create(['name' => 'instructor']);
return view('auth.instructor.instructor-space', compact('role'));
}
/**
* Handle an incoming registration request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|confirmed|min:8',
'role' => [Rule::requiredIf($request->role)],
]);
Auth::login($user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]));
$role = $request->get('role')
? Role::findByName('student')
: Role::findByName('instructor');
$user->assignRole($role);
$user->save();
// Check if the "Student" has the correct role
if (auth()->user()->hasRole('student')) {
return redirect()->intended('/student/dashboard');
}
// Check if the "Instructor" has the correct role
if (auth()->user()->hasRole('instructor')) {
return redirect()->intended('/instructor/dashboard');
}
event(new Registered($user));
}
}
// app\Http\Controllers\Auth\AuthenticatedSessionController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthenticatedSessionController extends Controller
{
/**
* Display the Sign In view.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('auth.signin');
}
/**
* Handle an incoming authentication request.
*
* @param \App\Http\Requests\Auth\LoginRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
if(auth()->check() && auth()->user()->hasRole('student')) {
return redirect()->intended('/student/dashboard');
} else {
return redirect()->back();
}
if(auth()->check() && auth()->user()->hasRole('instructor')) {
return redirect()->intended('/instructor/dashboard');
} else {
return redirect()->back();
}
// return redirect(RouteServiceProvider::HOME);
}
/**
* Destroy an authenticated session.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Request $request)
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/signin');
}
}
// app\routes\auth.php
<?php
Route::get('/student-space', [RegisteredUserController::class, 'createStudent'])->middleware('guest')->name('student-space');
Route::post('/student-space', [RegisteredUserController::class, 'store'])->middleware('guest');
Route::get('/instructor-space', [RegisteredUserController::class, 'createInstructor'])->middleware('guest')->name('instructor-space');
Route::post('/instructor-space', [RegisteredUserController::class, 'store'])->middleware('guest');
// resources\views\auth\student-space.blade.php
<form method="POST" action="{{ route('student-space') }}">
@csrf
...
<input type="hidden" name="role" required />
</form>
// resources\views\auth\instructor-space.blade.php
<form method="POST" action="{{ route('instructor-space') }}">
@csrf
...
<input type="hidden" name="role" required />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment