コード例 #1
0
 public function actionLogin()
 {
     $app = Mindy::app();
     if (!$app->user->getIsGuest()) {
         $model = $app->getUser();
         $key = Key::objects()->get(['user' => $model]);
         echo $this->json(['status' => true, 'errors' => [], 'user' => UserHelper::userToJson($model, $key ? $key->key : null), 'message' => UserModule::t('You have successfully logged in to the site')]);
         $this->end();
     }
     $form = new LoginForm();
     $r = $this->getRequest();
     if ($r->getIsPost() && $form->setAttributes($_POST)->isValid() && $form->login()) {
         $model = $form->getUser();
         $this->clearKeys($model);
         $authKey = md5(Password::generateSalt());
         $key = new Key(['user' => $model, 'key' => $authKey]);
         if ($key->save() === false) {
             echo $this->json(['status' => false, 'error' => 'Failed to save token']);
             $this->end();
         }
         $data = ['errors' => [], 'status' => true, 'user' => UserHelper::userToJson($model, $authKey), 'message' => UserModule::t('You have successfully logged in to the site')];
     } else {
         $data = ['errors' => $form->getErrors()];
     }
     echo $this->json($data);
     $this->end();
 }