vendredi 17 mai 2019

different login routes and login views for student and admin

Every laravel newbie struggles with multi auth, I am no exception

I am trying to make student management system. There will two different routs for admin admin/login and for student student/login.

The student can't register itself, but he will be registered by admin.

So a student has only access to student/dashboard, registration of students will be done by the admin on admin/dashboard.

Below is the detail what I have already done:

  1. created migration for both admin and student.

  2. created guard for both admin and student.

  3. modified login controller and added adminLogin and studentLogin methods.

  4. modified RedirectIfAuthenticated middleware

Config/auth.php

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'students' => [
            'driver' => 'session',
            'provider' => 'students',
        ],
        'web-admin'=>[
            'driver'=>'session',
            'provider'=>'admin',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'students',
            'hash' => false,
        ],
    ],
'providers' => [
        'students' => [
            'driver' => 'eloquent',
            'model' => App\Student::class,
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

'passwords' => [
        'students' => [
            'provider' => 'students',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

LoginController.php

lass LoginController extends Controller
{

    use AuthenticatesUsers;

    public function __construct()
    {
        $this->middleware('guest')->except('logout');

        $this->middleware('guest:web-admin')->except('logout');
        $this->middleware('guest:students')->except('logout');
    }

    public function showAdminLoginForm()
    {
        return view('admin.login', ['url' => 'admin']);
    }

    public function adminLogin(Request $request)
    {
        $this->validate($request, [
            'admin_id'   => 'required',
            'password' => 'required|min:8'
        ]);

        if (Auth::guard('admin')->attempt(['admin_id' => $request->adminid, 'password' => $request->password], $request->get('remember'))) {

            return redirect()->intended('/admin/dashboard');
        }
        return back()->withInput($request->only('admin_id', 'remember'));
    }

    public function showStudentLoginForm()
    {
        return view('student.login', ['url' => 'student']);
    }

    public function studentLogin(Request $request)
    {
        $this->validate($request, [
            'roll_no'   => 'required',
            'password' => 'required|min:8'
        ]);

        if (Auth::guard('writer')->attempt(['roll_no' => $request->roll_no, 'password' => $request->password], $request->get('remember'))) {

            return redirect()->intended('/student/dashboard');
        }
        return back()->withInput($request->only('roll_no', 'remember'));
    }

}

RedirectAuthenticated.php

class RedirectIfAuthenticated
{
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
           if('web_admin'==='$guard'){
               return redirect('/admin/dashboard');
           }
           return redirect('/admin/login');
        }
        if (Auth::guard($guard)->check()) {
            if('students'==='$guard'){
                return redirect('/student/dashboard');
            }
            return redirect('/student/login');
         }
        return $next($request);
    }
}

I have created two folders in the view, student and admin. They both have two files. login.blade.php and dashboard.blade.php

What laravel does it it shows login, and register under auth folder.

I want to give two routes one for /admin/login which return admin.login view.

Same for student /student/login which return student.login view.

I want to remove /register route and make the link to available on admin dashboard , there will be no admin register link.

Also restrict the user from accessing admin area.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire