I am trying to validate apikey , my scenario here is when am as a user enter apikey like xxxxxxxxxxxxxxxx-us17 i am saving this apikey in database as a user_id what i want here when i enter this key this key should check from mailchimp if its valid apikey that a user is enter if its a valid then good to go. if its not there should be a message not valid api key. On my side its allowing to enter any randon apikey which is not allowed in any case.
I am trying to solve this problem from a couple of days: Here is my full controller:
namespace App\Http\Controllers;
use App\APIKEY;
use DrewM\MailChimp\MailChimp;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Mailchimp\MailchimpFacade;
class ApiController extends Controller
{
public function authenticateApiKey(Request $request)
{
$fieldsValidation = [
'api_key' => 'required|unique:apikey,api_key',
];
$validator = Validator::make($request->all(), $fieldsValidation);
if ($validator->fails()) {
$resultArray = [
'status' => 0,
'message' => $validator->errors()->first(),
'dataArray' => []
];
} else {
$api = new APIKEY($request->all());
$mailchimp_ping = new MailChimp($api);
print_r($mailchimp_ping);die;
$mailchimp_ping = $mailchimp_ping->get('/helper/ping');
if (!$mailchimp_ping) {
$errorResponse = [
'message' => 'Not valid api key!',
'error' => '401'
];
return \Illuminate\Support\Facades\Response::json($errorResponse);
}
else
{
$request->user()->apikey()->save($api);
$resultArray = ['status' => 1, 'message' => 'Mailchimp Api key added into system successfully!', 'dataArray' => $api];
return \Illuminate\Support\Facades\Response::json($resultArray, 200);
}
}
}
THe main code where i am trying to validate apikey from maiilchimp is:
$api = new APIKEY($request->all());
$mailchimp_ping = new MailChimp($api);
print_r($mailchimp_ping);die;
$mailchimp_ping = $mailchimp_ping->get('ping');
if (!$mailchimp_ping) {
$errorResponse = [
'message' => 'Not valid api key!',
'error' => '401'
];
return \Illuminate\Support\Facades\Response::json($errorResponse);
Your help will be highly appreciated !