简体   繁体   中英

Finding User with Auth - Laravel

I am trying to find the logged in user in my application using Auth but i get trying to get property of non-object which i understand clearly that it is returning null.

In my code below, an event triggers my webhook and post is sent to the address below. The function orderCreateWebhook triggers but that is where the error comes from..

The line $get_template = Order::where('id', Auth::user()->id); . Why is Auth returning null please? I am logged as well because i use auth in this same controller for another function which works fine.

Is it because it a webhook ?

Controller

public function registerOrderCreateWebhook(Request $request)
    {

            $shop = "feas.myshopify.com";
            $token = "8f43d89a64e922d7d343c1173f6d";
            $shopify = Shopify::setShopUrl($shop)->setAccessToken($token);
            Shopify::setShopUrl($shop)->setAccessToken($token)->post("admin/webhooks.json", ['webhook' => 
             ['topic' => 'orders/create',
             'address' => 'https://larashop.domain.com/order-create-webhook',
             'format' => 'json'
             ]
            ]);
    }




     public function orderCreateWebhook(Request $request)
    {

         $get_template = Order::where('id', Auth::user()->id);

        $baseurl = "https://apps.domain.net/smsapi";
        $query = "?key=7e3e4d4a6cfebc08eadc&to=number&msg=message&sender_id=Shopify";
        $final_uri = $baseurl.$query;
        $response = file_get_contents($final_uri);
        header ("Content-Type:text/xml");

    }

In your function registerOrderCreateWebhook you appear to be making a request to shopify api and providing your webhook as the address which shopify will redirect the user to upon success. If this is correct, that request does not know about the user who generated the original request that made the api request since the request is coming from a completely different origin.

You would need to pass some key along with the url and then obtain the user within orderCreateWebhook . Something like:

Shopify::setShopUrl($shop)->setAccessToken($token)->post("admin/webhooks.json", 
['webhook' => 
    ['topic' => 'orders/create',
     'address' => 'https://larashop.domain.com/order-create-webhook/some-unique-key',
     'format' => 'json'
    ]
 ]);

My suggestion would be to have a unique hash stored somewhere that relates back to the user in your system, perhaps a column in your users table. I wouldn't use the user_id for security reasons. So you would end up with something like:

//route
Route::get('/order-create-webhook/{uniqueKey}', 'YourController@orderCreateWebhook');
//or
Route::post('/order-create-webhook/{uniqueKey}', 'YourController@orderCreateWebhook');
// depending on the request type used by api which calls this endpoint

// controller function
public function orderCreateWebhook($uniqueKey, Request $request)
{

     $user = User::where('unique_key', $uniqueKey)->first();
     $get_template = Order::where('id', Auth::user()->id);

     $baseurl = "https://apps.domain.net/smsapi";
     $query = "?key=7e3e4d4a6cfebc08eadc&to=number&msg=message&sender_id=Shopify";
     $final_uri = $baseurl.$query;
     $response = file_get_contents($final_uri);
     header ("Content-Type:text/xml");

}

Is it because it a webhook ?

Yes, you can't use sessions in a webhook. It's the shopify server which is making the call. You should read the doc, it may exist a way to give an unique identifier in your call to shopify api and get it back in the webhook to find your user associated.

just use this to get authenticated user use the facade in your class/Controller

use Illuminate\Support\Facades\Auth

public function getAuthUser(){
    $user = Auth::user()
    if(!is_null($user)
   {
    //user is authenticated
   }
  else 
   {
    // no user
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM