S
S
stainer2022-01-26 11:47:11
PHP
stainer, 2022-01-26 11:47:11

How to do HTTP Basic Authorization in Yii2 application without DB?

Good afternoon!
I need to do basic authorization in yii2 for a separate /api folder, without using a model (db), i.e. just enter the username and password in the code.
Tried via htaccess but no response when adding .htaccess to /api/web. As if the project does not see it.

I tried to do this, but it is through the database:

public function behaviors()
    {
        return [
            'basicAuth' => [
                'class' => \yii\filters\auth\HttpBasicAuth::className(),
                'auth' => function ($username, $password) {
                    if ($username == 'admin' && $password == 'admin') {
                        return true;
                    } else {
                        return null;
                    }
                },
            ],
        ];
    }


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2022-01-26
@reaferon

I will assume that the server does not run on Apache, but on nginx. In this case, you need to register in its config.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question