Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uswanto's avatar

sendLoginResponse didn't work and turns to blank page after login with External API

I just override a function login in my LoginController and change the attemptLogin (auth login through database) with my own function attempt with external API which i tested before with POSTMAN and works. But, when i put a decision where if status "success" then its return $this->sendLoginResponse (same with attemptLogin before) it turns to blank page.. Is it because the csrf token invalid or something else? I can't figure it out.

0 likes
4 replies
uswanto's avatar

This is login function inside LoginController that override login function inside trait AuthenticatesUsers.php

public function login(Request $request) { //validate the login request $this->validateLogin($request);

    //checking if username is exist (if not exist that means user don't have access to logbook system).
    $check['username'] = $request->username;
    $rules = array('username' => 'unique:users');
    $validator = Validator::make($check, $rules);

    if($validator->fails()){
      
        // if ($this->attemptLogin($request)) {
        //     return $this->sendLoginResponse($request);
        // }

        $this->attemptApiLogin($request);

        return $this->sendFailedLoginResponse($request);
    }

    return $this->sendNotAuthorizedLoginResponse($request);      
}

Then, below is attemptApiLogin function that i created to consuming the API

protected function attemptApiLogin(Request $request) { $postdata = http_build_query( array( 'LoginForm[username]' => $request->username, 'LoginForm[password]' => $request->password ) );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-Type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );
    
    $context = stream_context_create($opts);
    $resultapi = file_get_contents('https://abcdef.com/api/auth/login', false, $context);
    
    //echo $resultapi;
    
    $jsondec = json_decode($resultapi, true);
    // echo $jsondec['status'];

    if($jsondec['status'] == 'success'){ 
        //return $this->sendLoginResponse($request);
        //echo $resultapi; //IT WORKS (Fetch the data) and contain 'status' which i use for this decision
        echo "Login success"; //IT WORKS TESTED
    }else{
        echo "Login failed"; 
    }
}
Sti3bas's avatar

@uswanto I have no idea why you're trying to do, because:

  1. unique validation rule is for validating that value doesn't exist in the database. (I guess it should be opposite? (exists rule))
  2. You are calling attemptApiLogin method when validation fails.
  3. You are always returning failed response after attempting to login.
uswanto's avatar

Yeah first i want to validate if the username is registered in my users table then it means the user have access to my logbook system. It's because the API is contain all student credentials while my system only used by few student which is a volunteer. (for reduce API request too). When validation fails it means username exist in my user table so the next process is login by attemptApiLogin..

that return failed response when attemptApiLogin failed (username and password doesnt match) and i already disabled it to check attemptApiLogin response... it still doesn't return sendLoginResponse which contain

protected function sendLoginResponse(Request $request) { $request->session()->regenerate();

    $this->clearLoginAttempts($request);

    return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
}

is it because csrf token or something else? i'm new with laravel..

Please or to participate in this conversation.