jeudi 30 mars 2023

Facebook data deletion request error when sending json response

When implementing the Facebook data deletion request, Facebook calls my method from their side and is telling me that the json response im sending back to them is not what they are expecting to receive. If you look at their documentation here Facebook Data Deletion Request. The response back to Facebook should be as follows

{ url: '<url>', confirmation_code: '<code>' }

The code Facebook posts in order to return this json is this

$data = array(
  'url' => $status_url,
  'confirmation_code' => $confirmation_code
);
echo json_encode($data);

Which does not create what Facebook is intending as a response. There literally needs to be no quotes around the name of the property and single quotes around the actual values. So I built that out using this

$return_value = "{ url: '$url', confirmation_code: '$confirmation_code' }";

Yes still no success. IM kind of at a loss here since I've been trying various methods with response()->json() json_encode() when returning either the $data array or $return string I create. That ends up putting double quotes around the entire string or put in escape characters around the url, which is think is one of the reasons why they are telling me the json is not what they are expecting. So if any one has a clue as to exactly how Facebook wants this response to look like. Please let me know. Thank you for your help

public function removeFBDataCallback(Request $request)
    {
        $signed_request = $request->get('signed_request');
        $data = $this->parse_signed_request($signed_request);
        $fb_user_id = $data['user_id'];

        $fb_user = $this->UsersRepository->CheckFacebookUIDExisted($fb_user_id);
  
        if (!$fb_user) {
  
            error_log("No user with facebook id: $fb_user_id found when trying to remove data during facebook deletion callback");
            return response()->json(['code' => 403, 'data' => ['message' => 'Facebook user not found when deleting facebook data']]);
        }
        else {
            

            $fb_delete_confirmation = new FBDeleteConfirmation();
            $fb_delete_confirmation->facebook_id = $fb_user->facebook_user_id;
            $fb_delete_confirmation->token = genToken(20);
            $fb_delete_confirmation->save();


            $fb_user->is_account_facebook = 0;
            $fb_user->facebook_user_id = null;
            $fb_user->save();

            
            $confirmation_code = base64_encode("abc123");
            $url = url('login').'/facebook'.'/dataDeleteConfirmation'.'?confirmationid'.('=').($confirmation_code);


            error_log('------------------');

            $return_value = "{ url: '$url', confirmation_code: '$confirmation_code' }";
            error_log($return_value);
            return json_encode($return_value);

        }
        
    } 


via Chebli Mohamed

How to add support for OPTION http verb to an apiResource?

I have APIs like this. I have a thousand of resources.

Route::apiResource('users', 'UserController')

The problem is I need to have also the OPTION verb enabled for all routes.

I could manually add option route to each route, but I am here asking if there is a way to do it automatically.

Actually the project is stalled on Laravel 5.6



via Chebli Mohamed

mercredi 29 mars 2023

Laravel: Product sorting based on categories

We are facing some issues while displaying the list of products with categories.

If there are 3 categories and each page contains 10 products on each page.

When we have 100 products, we need to show the listing with pagination. But the requirement is as below:

The list of products should be sorted like this: The first product from category 1 (In the list it will be in 1st place) The first product from category 2 (In the list it will be in 2nd place) The first product from category 3 (In the list it will be in 3rd place) The second product from category 1 (In the list it will be in 4th place) The second product from category 2 (In the list it will be in 5th place) The second product from category 3 (In the list it will be in 6th place) and So on...

We have product and category relation one to one relationship



via Chebli Mohamed

image upload from wamp(localhost) server to particular cpanel in laravel framework

I am trying to upload image from wamp server to direct upload in particular Cpanel in laravel framework. Is that possible .?

I want to upload image from any hosting site or localhost (wamp server) image will upload particular cpanel. Is that possible .?



via Chebli Mohamed

lundi 27 mars 2023

clicking on login button and return only to the index page in laravel

i have a problem and it isnt related with validation , im making my own login and register page and im not using start kits like breeze or jet stream ... when i click on the login button it must go and check the database if there is the same password and email then do something . but other to do anything it only return me to the same page like only i refresh the page , even if the email and password correct or not , it only refresh the page. why ?? This is the login form

<form class="login-form" action="" method="post">
    @csrf
    <img
        class="logo"
        src="https://www.logomaker.ckhfZt.png"
        alt="logo"
    />
    <label for="email">Email address:</label>
    <input
        type="text"
        id="email"
        name="email"
        placeholder="Enter email address"
        required
    />

    <label for="password">Password:</label>
    <input
        type="password"
        id="password"
        name="password"
        placeholder="Enter password"
        required
    />
    @error('failed')
    <div class="alert alert-danger" role="alert">
        
    </div>
    @enderror

    <input type="submit" value="Login" />
</form>

and this is the web file

Route::controller(AuthController::class)->group(function () {


    /* Register Route */

    Route::get('/register','register');
    Route::post('/store','store')->name('store');


    /* Login Route */

    Route::get('/','login');
    Route::post('/','loginCheck')->name('login-check');


    /* Home Route */
    Route::get('/home','home');

});

and this is the controller

class AuthController extends Controller
{
    public function login(){
        return view('auth.login');
    }
    public function register(){
        return view('auth.register');
    }

    public function home(){
        return view('home');
    }


    public function store(StoreRegisterRequest $request){

    $existingUser = DB::table('users')->where('email',$request->email)->first();
    if(!$existingUser){
        DB::table('users')->insert([
            'name' => $request->name,
            'email' => $request->email,
            'password'=> Hash::make($request->password),
        ]);
        return redirect('/');
         }
    }


    public function loginCheck(StoreRegisterRequest $request)
    {
        $user = DB::table('users')->where('email', $request->email)->first();
    
        if ($user && Hash::check($request->password, $user->password)) {
            // Credentials are valid, so start the session
          return $user;
        }
    
        // Invalid credentials, so return an error message
        return back()->withErrors(['failed' => 'Invalid login credentials.']);
    }
    
    

}

what is the problem why i cant see the user variable or i cant return to any other path?

im expecting my problem to be solved



via Chebli Mohamed

Call to undefined method App\Http\Controllers\UserController::successResponse() (500 Internal Server Error)

I am so confused by this error because this is my first time in back end subject , our instructor didn't included the CRUD operations on the video he provided so I decided to discover all by myself then I came up with this error on POST operation

HTTP - CONTROLLER - USER CONTROLLER PHP


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
Class UserController extends Controller {
  private $request;
public function __construct(Request $request){
  $this->request = $request;
}
public function getUsers(){ 
  $users = User::all();
  return response()->json($users, 200);
}

public function add(Request $request ){
  $rules = [
  'first_name' => 'required|max:20',
  'last_name' => 'required|max:20',
  // 'gender' => 'required|in:Male,Female',
  ];
  $this->validate($request,$rules);
  $user = User::create($request->all());
  return $this->successResponse($user,
 Response::HTTP_CREATED);
  }
} 

MODEL - USER PHP

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
class User extends Model{
public $timestamps = false;
protected $table = 'students';
// column sa table
protected $fillable = [
'first_name', 'last_name'
];
}

ROUTES - WEB PHP


<?php

/** @var \Laravel\Lumen\Routing\Router $router */

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$router->get('/', function () use ($router) {
    return $router->app->version();
});

$router->get('/users',['uses' => 'UserController@getUsers']);


$router->post('/postUsers', 'UserController@add'); // create new user record

This is the all good I cant identify where the error occured or I have something miss to put

I just want to run the POST in postman to add data on column because I think POST is updating the database

so this is the data that I want to add

{ "first_name": "Lorem", "last_name": "Ipsum" }



via Chebli Mohamed

dimanche 26 mars 2023

How to use refresh token functionality in lcobucci/jwt using laravel 5?

Unable to implement refresh token code

currently im generating token using this vendor in-build functions. Already implemented the create token functionality, but every token has a expiry time once the token reaches its expiry time i needs to use refresh functionality to regenerate new token. dont know to to implement it.

Here's my existing code for create token,

Authcontroller.php
$token = $user->createToken($request->CLIENT_ID, 
                $scope_data_array
                )->accessToken;

HasApiTokens.php (Laravel/passport inbuilt function)
public function createToken($name, array $scopes = [])
    {
        log::debug("--inside createToken");
        return Container::getInstance()->make(PersonalAccessTokenFactory::class)->make(
            $this->getKey(), $name, $scopes
        );
    }


via Chebli Mohamed