Search code examples
phplaraveloracleauthenticationlaravel-6.2

Using custom authentication on Laravel 6


I would like to manually authenticate the users in my company. The issue is that, I have 2 tables, called Student and Staff in the Oracle database.

As for the Student table, I get the idea of overriding the built in Auth method provided through the auth scaffolding command as the username and password are stored right into the table.

As for the Staff table, the password is stored a different column/table and encrypted using a stored procedure/package so the only way to get the user validation is by calling the package which only returns 0 or 1 only.

What I have done,

I wrote my own Routes, and added my own functions in LoginController.

public function loginStaff(Request $req){
    $username = Str::upper($req->input('username'));
    $password = $req->input('password');

    $users = PortalUser::where('ID', $username)->firstOrFail();

    if ($users->user_type == 'STAFF'){

       $queryResult = DB::select('select PACKAGE.validStaff(?,?) from dual',[$username, $password]);

       if($queryResult == 1){

              //this is where I would like to auth the user.
              //using Auth::Attempt and Auth::Login will only run the default query
       }

}

I have successfully returned value of 1 and 0 in the controller.

So is there anything that I am missing? Or should I manually set the session by myself using the session() method?

Thank you.


Solution

  • If you want to manually authenticate users, you can easily use sessions. Have the following code as reference:

    //this is where I would like to auth the user.
    //using Auth::Attempt and Auth::Login will only run the default query
    
    // typically you store it using the user ID, but you can modify the session using other values.     
    session()->put('user_id', user id from database here);
    

    And if you want to check whether user is authenticated, modify RedirectIfAuthenticated middleware to this:

    <?php
    
    namespace App\Http\Middleware;
    
    use App\Providers\RouteServiceProvider;
    use Closure;
    use Illuminate\Support\Facades\Auth;
    
    class RedirectIfAuthenticated
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @param  string|null  $guard
         * @return mixed
         */
        public function handle($request, Closure $next, $guard = null)
        {
            if (session()->has('user_id')) {
                return redirect(  custom path here );
            }
    
            return $next($request);
        }
    }
    

    When you want to logout the user, simply destroy the session key

    session()->forget('user_id');
    

    **Note: ** many broadcasting and addons use Laravel's authentication system (Guards) and you may need to hook into their code if you want to use them with your custom auth system