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'];
     }
 }
Example #3
0
 public static function findByToken($token)
 {
     self::$user = User::where('token', $token);
 }