Ap3twe
2 weeks ago
320
17
Laravel

Authetication Redirect and Error Catch

Posted 2 weeks ago by Ap3twe
  1. I want to catch the exception shown below into the login view that is: If the request does not pass return an error message to the user in the form etc. I am returning it to another page on line return view ('authuserspage'); with a custom error message. How do display the error above the form in login view

Routes

Route::get('/', function () {
    return view('welcome');
});
Auth::routes();
Route::get('/home', '[email protected]')->name('home');
Route::get('/logintest', '[email protected]')->name('logintest');
Route::get('/authuserspage', '[email protected]')->name('authuserspage');
 Route::post('/getCustomerVerification', '[email protected]')->name('getCustomerVerification');
Route::post('/xml2multiarray', '[email protected]')->name('xml2multiarray');

Controller

class CustomAuthController extends Controller
{
    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

protected function getCustomerVerification(Request $request){

    /* Initialize webservice with your WSDL */
    $client = new SoapClient("***/Token.asmx?WSDL");
    $params = array(
        'APIUserID' => '****',//ENV
        'APIPassword' => '*****',//ENV
        'webUsername' =>  $request->username,
        'webPassword' => $request->password,
    );


    /* Invoke webservice method with your parameters, in this case: Function1 */

    try {
        $responsetest = $client->getCustomerInfo($params);
     
    }

    catch (\Throwable $e){
  return view ('authuserspage');

    }

    $array = $this->xml2multiarray($responsetest->getCustomerInfoResult->any, $request);
    return view('/home');
}

//convert xml to array
protected function xml2multiarray($xml, Request $request)
{
    
    $xmldata1 = explode("</Table>",$xml);
    $xmldata2 = explode("<Table diffgr:id",$xmldata1[0]);
    $xmldata  = "<Table diffgr:id".$xmldata2[1]."</table>";
    $p = xml_parser_create();
    xml_parse_into_struct($p, $xmldata, $vals, $index);
    xml_parser_free($p);
    $array = array();
    foreach($vals as $val)
    {
        if($val['type'] == 'complete')
        {
            $key = strtolower($val['tag']);
            $value = $val['value'] ?? '';
            $array[$key] = $value;
        }
    }
    // return $array;
    $GLOBALS['docinfoAccountNumber'] = ($array['accountnumber']);
    $GLOBALS['docinfoCustomerID'] = ($array['customerid']);
    $GLOBALS['docinfoEmail'] = ($array['email']);

    $email = $GLOBALS['docinfoEmail'];
    $username = $GLOBALS['docinfoCustomerID'];
    $user = User::where('email', '=', $email)->first();
    $clientId = User::where('username', '=', $username)->first();
    //   validate the fields...
     if ($user === null && $clientId === null){

      $password = Hash::make($request->password);

      $user = new user;
      $user->drname = $GLOBALS['docinfoFirstName'];
      $user->username= $request->username;
      $user->email = $GLOBALS['docinfoEmail'];
      $user->password = $password; //hashed password.
      $user->save();
      Auth::login($user,true);

    }
}

Routes methods

public function logintest()
{
    return view ('/logintest');
}

public function  authuserspage(){

    return view ('/authuserspage');
}

Please sign in or create an account to participate in this conversation.