Adding API

This commit is contained in:
Chris Smith
2025-02-20 20:09:01 +01:00
parent 0c639ce11e
commit 5f3d1359cf
15 changed files with 292 additions and 133 deletions

View File

@@ -0,0 +1,75 @@
<?php
namespace api\controllers;
use common\models\User;
use Firebase\JWT\JWT;
use Yii;
use yii\rest\Controller;
use yii\web\Response;
class AuthController extends Controller
{
public function verbs()
{
return [
'register' => ['POST'],
'login' => ['POST'],
];
}
private function generateJwt($user)
{
$key = Yii::$app->params['jwtSecret']; // Set in params.php
$payload = [
'iss' => 'calorie-thingy',
'aud' => 'calorie-thingy',
'iat' => time(),
'exp' => time() + (60 * 60 * 24 * 7), // 7 days expiration
'sub' => $user->auth_key,
];
return JWT::encode($payload, $key, 'HS256');
}
public function actionRegister()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$data = Yii::$app->request->post();
if (empty($data['email']) || empty($data['password'])) {
return ['error' => 'Email and password are required'];
}
if (User::findOne(['email' => $data['email']])) {
return ['error' => 'Email already exists'];
}
$user = new User();
$user->email = $data['email'];
$user->password_hash = Yii::$app->security->generatePasswordHash($data['password']);
$user->generateAuthKey();
$user->generateEmailVerificationToken();
if ($user->save()) {
return ['token' => $this->generateJwt($user)];
}
return ['error' => 'Registration failed'];
}
public function actionLogin()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$data = Yii::$app->request->post();
$user = User::findOne(['email' => $data['email'] ?? null]);
if (!$user || !Yii::$app->security->validatePassword($data['password'], $user->password_hash)) {
return ['error' => 'Invalid credentials'];
}
// @todo not sure if it makes sense to generate an auth key each login via the API?
$user->generateAuthKey();
$user->save();
return ['token' => $this->generateJwt($user)];
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace api\controllers;
use api\components\JwtAuth;
use common\models\Meal;
use common\models\MealForm;
use Yii;
use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;
use yii\web\Response;
use yii\web\UploadedFile;
class MealController extends ActiveController
{
public $modelClass = Meal::class;
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => JwtAuth::class,
];
return $behaviors;
}
public function actionCreateMeal()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$model = new MealForm();
$model->picture = UploadedFile::getInstance($model, 'picture');
if ($model->upload()) {
$id = \Yii::$app->gemini->mealInquiry(Yii::getAlias('@frontend/web/'.$model->filepath));
return array_merge(['meal' => Meal::findOne($id)], $this->actionGetDailySummary());
}
return ['error' => 'Failed to save meal'];
}
public function actionGetDailySummary()
{
// @TODO make this a common function of the user maybe?
Yii::$app->response->format = Response::FORMAT_JSON;
$startOfDay = strtotime('today midnight');
$endOfDay = strtotime('tomorrow midnight') - 1;
$today = Meal::find()
->select([
'SUM(calories) AS calories',
'SUM(protein) AS protein',
'SUM(fat) AS fat',
'SUM(carbohydrates) AS carbohydrates',
'SUM(fiber) AS fiber'
])
->where(['user_id' => Yii::$app->user->id])
->andWhere(['between', 'created_at', $startOfDay, $endOfDay])
->asArray()
->one();
return ['summary' => $today];
}
public function actions()
{
$actions = parent::actions();
// Modify the index action
$actions['index']['prepareDataProvider'] = function() {
// Get the user_id from the JWT token
$userId = Yii::$app->user->identity->id;
// Create the query to filter meals by the authenticated user
$query = Meal::find()->where(['user_id' => $userId]);
// Return the data provider with the filtered query
return new ActiveDataProvider([
'query' => $query,
]);
};
return $actions;
}
}

View File

@@ -1,106 +0,0 @@
<?php
namespace api\controllers;
use common\models\LoginForm;
use Yii;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actions()
{
return [
'error' => [
'class' => \yii\web\ErrorAction::class,
],
];
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
/**
* Login action.
*
* @return string|Response
*/
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$this->layout = 'blank';
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
$model->password = '';
return $this->render('login', [
'model' => $model,
]);
}
/**
* Logout action.
*
* @return Response
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
}