lumen-passport Authentication - $request->user() not setting - passport.js

I am struggling to login a user using lumen-passport. I have followed the instructions in the link I just supplied but cannot seem to set the user to use in the $request variable of via the Auth facade.
As long as I have my guard driver set to passport in auth.php I cannot get into this block inside AuthServiceProvider.php boot() method no matter what I seem to do:
Auth::viaRequest('api', function ($request) {
if ($request->input('api_token')) {
return User::where('api_token', $request->input('api_token'))->first();
}
});
But even if I try manually return a User record before that call the user is never set, and if I try the following:
Auth::setUser(User::where('id', 1);
I get this message:
InvalidArgumentException in AuthManager.php line 99:
Auth guard driver [api] is not defined.
My query above is definitely returning a record from the User table as well. It's just I cannot get around to setting it so I can access it in my application via the $request->user() method.
Any help would be greatly appreciated as this really has me puzzled. If it helps at all this is my auth.php config.
'defaults' => [
//'guard' => env('AUTH_GUARD', 'api'),
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
//'api' => ['driver' => 'api'],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => \App\User::class
]
],
'passwords' => [
//
],
];

Related

Input field function when aggregating data in node and mongoDB

I am trying to develop a data aggregation feature for an application. The user should be able to enter a name and it'll search the database collection for that name and return the collection.
I am trying to implement it using node.js but i am confused as to how to pass a parameter for the name.
Controller:
exports.DAFacility_author_search = (req, res) => {
DAFacility.aggregate([
[
{
'$match': {
'author': author
}
}
]
]).then((DAFacility) => {
res.send(DAFacility);
})
.catch((e) => {
res.send(e);
});
};
Route:
router.get('/DAFacilityAuthor', DAFacilityController.DAFacility_author_search)
For the 'author': author i am trying to use author as the variable name but i dont exactly know how to structure the controller and router to take in a parameter so that i can retrieve a value on postman.
Any help would be great
Thank you
I suppose that your request in postman will be something like:
{
"author": "foo",
...
}
When your request is received by the API, the body is available in your req object in the controller, so, if you want to use author field in your aggregate, you simply access: req.body.author.
Your controller could have the next structure:
export const DAFacilityController = {
DAFacility_author_search: (req, res) => {
DAFacility.aggregate([
{'$match': {'author': req.body.author}}
])
.then((DAFacility) => {
res.send(DAFacility);
}).catch((e) => {
res.send(e);
});
},
DAFacility_another_method: (req, res) => {...},
}
The router is ok:
router.get('/DAFacilityAuthor', DAFacilityController.DAFacility_author_search);
router.get('/DAFacilityAuthor/<something>', DAFacilityController.DAFacility_another_method);
I hope I helped

Stripe how to retrieve a Checkout Session's line items quantity?

Create payment Sessions :
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'], //, 'fpx','alipay'
'line_items' => [[
'price_data' => [
'product_data' => [
'name' => "Topup USDT Wallet",
'images' => ["https://abc-uaha.co/uploads/site_logo/site_logo_20210321130054.png"],
'metadata' => [
'pro_id' => "USD".$_GET['price']/100
]
],
'unit_amount' => $_GET['price'],
'currency' => 'usd',
],
'quantity' => 1,
'description' => "Spartan Capital",
]],
'mode' => 'payment',
'success_url' => STRIPE_SUCCESS_URL.'?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => STRIPE_CANCEL_URL,
]);
Refer to this docs : https://stripe.com/docs/api/checkout/sessions/line_items
I tried retrieve quantity from session:
try {
$checkout_session = \Stripe\Checkout\Session::retrieve([
'id' =>$session_id,
'expand' => ['line_items'],
]);
}catch(Exception $e) {
$api_error = $e->getMessage();
}
$line_items = $session->line_items[0].quantity;
echo $line_items; //it shows nothing, how to make it output "1"?
line_items are no longer included by default when retrieving Checkout Sessions. To get them in your retrieve call, you need to expand the line_item property.
You have two syntax errors :
You're missing a layer and using dot notation instead of PHP arrow syntax. The 2nd error is using $session instead of $checkout_session. So it should be :
$quantity = $checkout_session->line_items->data[0]->quantity;

Laravel Livewire how to validate array data

I try to validate array when form submitted as
<input type="text" class="form-control" wire:model.lazy="data.name" placeholder="name">
//at livewire component class
$data = [
'name' => 'someValue',
'phone' => 'someValue',
'email' => 'someValue'
]
I try this
Validator::make($this->data,[
'name' => 'required',
...
])->validate();
but not working, please help me.
You can validate using the same syntax as you have used in wire:model:
$this->validate([
'data.name' => ['required'],
]);
I'm new using Livewire, Idk if this related to your question. But, since your question is on top in Google. Maybe this will help other people.
public function saveAdd()
{
$rules_state = [
'state_name' => 'required',
'state_email' => 'required',
];
$check_state = $this->inputsFormAddProduct;
foreach ($this->inputsFormAddProduct as $key => $value) {
$rules_state = array_merge($rules_state, [
'state_product.'.$value => 'required',
'state_qty.'.$value => 'required',
]);
}
$validatedData = $this->validate($rules_state,
[
'required' => 'The :attribute cannot be empty.',
],
);
dd($rules_state);
}

Yii2- How to add a search box in grid view

I am new to Yii-2.
I have a grid-view in my index page which some entries are displaying.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'meter_id',
[
'label' => 'Meter MSN',
'value' => function ($d) {
return $d->meter->meter_msn;
},
// 'filter' => Html::activeDropDownList($searchModel, 'meter_id', \app\models\Meters::toArrayList(), ['prompt' => "All Meters", 'class' => 'form-control']),
],
'imsi',
'telecom',
'status',
[
'label' => 'Created By',
'value' => function ($data) {
if (is_object($data))
return $data->created->name;
return ' - ';
},
//'filter' => Html::activeDropDownList($searchModel, 'created_by', \app\models\User::toArrayList(), ['prompt' => "Created By", 'class' => 'form-control']),
],
'comments',
'historic',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Now I want to add a search-box against Meter MSN. In above code the filter is hidden so it was working but I don't want to add a drop-down instead I want a search box.
Below is my search class
public function search($params)
{
$query = MetersInventoryStore::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'meter_id' => $this->meter_id,
'created_by' => $this->created_by,
'updated_by' => $this->updated_by,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'store_id' => $this->store_id,
'meter_serial'=>$this->meter_serial,
// 'historic' => $this->historic,
'status'=>'SIM Installed',
])
// ->orFilterWhere(['status'=>'Communication Failed'])
;
// $query->andFilterWhere(['like', 'meter_serial', $this->meter_serial])
// ->andFilterWhere(['like','meter_id',$this->meter_id]);
$query->orderBy(['id' => SORT_DESC]);
return $dataProvider;
}
How can I place a search-box in it? As the simple search class will set up the search functionality by default. But my MSN value is coming from a function so I have no idea how can I place a search-box.
Any help would be highly appreciated.
for add filter field in a calculated column you should add a pubblic var in
in your search model
public function search($params)
{
public $your_column;
// declare as safe
public function rules()
{
return [
...
[[ 'your_column', ], 'safe'],
];
}
$query = MetersInventoryStore::find();
and then refer to your_column in grid_view
...
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'meter_id',
[
'attribute' => 'your_column',
'label' => 'Meter MSN',
'value' => function ($d) {
return $d->meter->meter_msn;
},
],
And last your searchModel you must expand your filter condition for manage properly your calculated column based on the filter value you passed.
You can find some sample in this tutorial http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

Error when using Auth with ORM driver kohana 3.3.0

today i download a 3.3.0 but have error when working with Auth and ORM :
Fatal error: Class 'Auth_Orm' not found in
/home/vsao4/domains/v4.mclub.vn/public_html/modules/auth/classes/Kohana/Auth.php
on line 37
My Auth config as below :
'driver' => 'orm',
'hash_method' => 'md5',
'hash_key' => 'secret',
'lifetime' => 1209600,
'session_type' => Session::$default,
'session_key' => 'auth_user',
and the code is :
$ars = array(
'username'=>'manhquan',
'password'=>'232323',
'email'=>'manhquan#fifthmediacorp.com'
);
if (Auth::instance()->login($ars['username'], $ars['password']))
{
// sucessfully loged
$this->response->body('login');
} else {
//TODO error
$this->response->body('not login');
}
change
'driver' => 'orm',
to
'driver' => 'ORM',
Also, there will be an error to Database_Mysql class. Also change your config/database.php file:
'type' => 'mysql',
to
'type' => 'MySQL',
This happens due to new Kohana conventions:
http://kohanaframework.org/3.3/guide/kohana/conventions

Resources