<?php

namespace rest\controllers;


use common\models\User;
use yii\filters\auth\HttpBasicAuth;
use yii\rest\Controller;

class RestController extends Controller
{

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => HttpBasicAuth::className(),
            'auth' => [$this, 'auth']
        ];
        return $behaviors;
    }

    public function auth($username, $password)
    {
        try {
            $user = User::findOne(['username' => $username]);
            if ($user->validatePassword($password)) {
                return $user;
            }
        } catch (\Exception $e) {
            \Yii::error("Failed to load user: " . $e->getMessage());
        }
        return null;
    }


}
