Y
Y
yurygolikov2017-02-20 21:18:33
Yii
yurygolikov, 2017-02-20 21:18:33

YII2 - HttpBearerAuth. Why does it throw an error without specifying the optional property?

I do authentication through HttpBearerAuth on YII2.
If I specify WITHOUT 'optional' - throws an error. Your request was made with invalid credentials.

public function behaviors()
    {
        return [
            'authenticator' => [
                'class' => HttpBearerAuth::className(),
            ],

If I specify C 'optional' - then everything is ok.
public function behaviors()
    {
        return [
            'authenticator' => [
                'class' => HttpBearerAuth::className(),
                'optional' => ['*'],
            ],

Question:
Why does the default value - take all actions - not work?
As far as I understand, the doc says that if this property is not set,
then the filter is applied to all actions. Or have I misunderstood something?
/**
     * @var array list of action IDs that this filter will be applied to, but auth failure will not lead to error.
     * It may be used for actions, that are allowed for public, but return some additional data for authenticated users.
     * Defaults to empty, meaning authentication is not optional for any action.
     * Since version 2.0.10 action IDs can be specified as wildcards, e.g. `site/*`.
     * @see isOptional()
     * @since 2.0.7
     */
    public $optional = [];

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
padlyuck, 2017-02-20
@yurygolikov

because the default value is an empty array, not all actions. in the piece you cited, it says that by default the list is empty, which means that authorization is NOT optional for any action (if verbatim). If in Russian, then it is mandatory for everyone

D
Dmitry Luzanov, 2017-10-25
@ZZiliST

var el = 'rate-55345'
var numEl = parseInt(el.match(/\d+/))

alert(numEl)

E
Egor Zhivagin, 2017-10-25
@Krasnodar_etc

var el = 'rate-5356';
// numEl изначально строка, чтобы числа не складывались друг с другом, а приписывались
var numEl = '';
// Перебираем каждый символ. Если символ можно распарсить как номер, приписываем к строке
for (var index in el) {
  if ( parseInt(el[index]) ) {
    numEl += el[index]
  }
}
parseInt(numEl); // 5356

UPD: added comments just in case

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question