コード例 #1
0
ファイル: index.php プロジェクト: arzynik/hackathon-starter
 public function init($args = null)
 {
     $this->inject(function ($Request) {
         if ($Request->method() == 'POST' && $Request->loc(2)) {
             return http_response_code(403);
         }
         $u = $Request->loc(2) ? $Request->loc(2) : $_SESSION['user'];
         $user = new \App\User($u);
         if ($Request->method() == 'DELETE') {
             if ($Request->loc(2)) {
                 return http_response_code(403);
             }
             $user->delete();
             echo json_encode(['status' => 'success']);
             return;
         }
         if ($user->dbId()) {
             if ($Request->method() == 'POST') {
                 $props = $Request->request();
                 unset($props['id']);
                 $user->serialize($props);
                 try {
                     $user->save();
                 } catch (\Exception $e) {
                     echo json_encode(['status' => false, 'error' => 'Email already exists']);
                     return;
                 }
             }
             echo $user->json();
         } else {
             http_response_code(404);
         }
     });
 }
コード例 #2
0
ファイル: signup.php プロジェクト: arzynik/hackathon-starter
 public function init($args = null)
 {
     $this->inject(function ($Request) {
         if ($Request->method() == 'POST' && $Request->email && $Request->password) {
             $user = \App\User::byEmail($Request->email);
             if ($user->id) {
                 echo json_encode(['error' => 'Email is already in use']);
                 return;
             }
             $user = new \App\User(['email' => $Request->email, 'password' => password_hash($Request->password, PASSWORD_BCRYPT)]);
             $user->save();
             $_SESSION['user'] = $user->id;
             echo $user->json();
             return;
         }
         http_response_code(403);
     });
 }