public function login() { $response = $this->app->response(); $response->header("Content-Type", "application/json"); $username = $this->app->request()->params('username'); $password = $this->app->request()->params('password'); if (!isset($username)) { return Auth::deny_access("Username is null"); } if (!isset($password)) { return Auth::deny_access("Password is null"); } $username = htmlentities(trim($username)); $password = htmlentities(trim($password)); $database_user = User::where('username', $username); $database_user = json_decode($database_user, true); if (empty($database_user)) { return ['status' => 400, 'message' => 'username doesn\'t']; } $database_user = $database_user[0]; if ($database_user['password'] == md5($password)) { $key = $this->config->jwt_key(); $token = ["iss" => $this->config->jwt_issuer(), "iat" => $this->config->jwt_issuer_at(), "nbf" => $this->config->jwt_not_before(), "exp" => $this->config->jwt_expiration_time(), "data" => ["username" => $database_user['username']]]; $encode_jwt = JWT::encode($token, $key, 'HS512'); $responseArray = ["token" => $encode_jwt, "status" => 200]; $response->status(200); $response->body(json_encode($responseArray)); return $response; } else { return Auth::deny_access("Incorrect Authentication Details"); } }
public function authenticate() { $app = $this->app; $request = $app->request(); $response = $app->response(); $response->header("Content-Type", "application/json"); if (!$request->headers->get('Authorization')) { return Auth::deny_access("Authorization Token is not set. Please login"); } else { $key = "example_key"; $this->token = $request->headers->get('Authorization'); $decoded_jwt = JWT::decode($this->token, $key, array('HS512')); $decoded_jwt = (object) $decoded_jwt; $this->expiry = $decoded_jwt->exp; $this->auth_user = User::where('username', $decoded_jwt->data->username); $this->auth_user = json_decode($this->auth_user, true); $this->auth_user = $this->auth_user[0]; return $this->auth_user['username']; } }
public function updateEmoji($id) { $app = $this->app; $request = $app->request(); $response = $app->response(); $response->header("Content-Type", "application/json"); $tag = $request->params('tag'); $title = $request->params('title'); $image = $request->params('image'); try { $emoji = new Emoji(); $emoji->id = $id; if (isset($tag)) { $emoji->tag = $tag; } if (isset($image)) { $emoji->image = $image; } if (!isset($title)) { $emoji->title = $title; } if (count($this->auth->authenticate()) < 0) { return Auth::deny_access("Invalid User"); } $emoji::save(); $responseArray['status'] = 200; $responseArray['message'] = "Emoji has been successfully updated"; $response->status(200); $response->body(json_encode($responseArray)); } catch (ModelNotFoundException $e) { $response->body(json_encode(['error' => 'Emoji not found for the given id'])); $response->status(404); } return $response; }