/**
  * Update the specified resource in storage.
  *
  * @param  ApiKey $apiKey
  * @return Response
  */
 public function update(ApiKey $apiKey)
 {
     $input = \Input::all();
     $name = $input['name'];
     $value = $input['value'];
     $apiKey->fill(['name' => $name, 'value' => $value]);
     $apiKey->save();
     return redirect()->route('apiKeys.show', $apiKey->id)->with('message', 'API key udpated.')->with('message-class', 'success');
 }
Example #2
0
 /**
  * Used to create an api key.
  * Users are only allowed to have one api key.
  * 
  * @return json  Empty response on successful or error on failure
  */
 public function create()
 {
     //must be ajax
     if (Request::ajax()) {
         $user = Auth::user();
         $userId = $user->id;
         //only allowed one key
         $numberOfKeys = ApiKey::where('user_id', $userId)->count();
         if ($numberOfKeys !== 0) {
             return response()->json(array('error' => 'Requesting too many keys'), 403);
         }
         $apiKeyModel = new ApiKey();
         $key = md5(microtime());
         $apiKeyModel->user_id = $userId;
         $apiKeyModel->api_key = $key;
         $saved = $apiKeyModel->save();
         if ($saved) {
             return response()->json(array('key' => $key));
             // return redirect('/apikey');
         } else {
             return response()->json(array('error' => 'Unable to generate key'), 500);
         }
     }
 }