Search code examples
laravellaravel-5oauthlaravel-5.4laravel-passport

Laravel passport with custom login controller


As mention in laravel-passport the login controller is made from php artisan make:auth and in our router web.php would have Auth::routes();

Simple code in our client-site

Route::get('/redirect', function () {

    $query = http_build_query([
        'client_id' => 'client-id',
        'redirect_uri' => 'http://client-site/callback-responses-url',
        'response_type' => 'code',
        'scope' => ''
    ]);

    return redirect('http://server-site/oauth/authorize?'.$query);
});

If the user is not log in the http://server-site/oauth/authorize?'.$query would be redirect to http://server-site/login

In any case of it i would like to redirect the login controller to my custom login controller. Then after i done check the use if is true would be show view about authorization request agreement

Simply i have tried to check oauth/authorize and i didnt find any function or controller handle that link. And if given any chance again. I would to know, more about the laravel-passport how the oauth work, and so do i could make the custom authorization view too after the user has login

Any help would be so helpful thank you (:


Solution

  • After i do a big search in laravel passport, i got the solution how to customize the laravel passport.

    in your app\Exception\Handler.php there is a code

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
    
        //return redirect()->guest(route('login'));
        return redirect()->guest(route('accountLogin')); // change this part to your login router
    }
    

    then in your login controller which is handler the success login add this code

    if(session("url")["intended"] != null){
        $redirect = session("url")["intended"];
    }else{
        $redirect = "/home";
    }
    
    return Redirect($redirect);
    

    That codes are for redirect back to the client page. Surely if you know a bit more about the AuthenticatesUsers in vendor\laravel\framework\src\Illuminate\Foundation\Auth how they works, you could build your own beautiful code.

    The last step i found is here : RouteRegistarar this is for how the route authorization is crossed.

    Untill now, i am not yet customize my authorization page. Currently i was doing a beautiful third party code, so client could be easy to use it.

    Thats all i know about the laravel passport to customize the login page