daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Oct 23rd, 2017 362 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class userAuth {
  2.     // create an empty id variable to hold the user id
  3.     private $id;
  4.     private $email;
  5.     private $key = "16FD8C979FC40CCB97457F4AD79B32A73758771B4D1943C379FB3266EECE0C3E";
  6.  
  7.     // Checks if the user exists in the database
  8.     private function validUser($email, $password) {
  9.         $conn = new mysqli(DBSERVER, DBUSERNAME, DBPASSWORD, DBNAME);
  10.         if ($conn->connect_error) {
  11.             die("Connection failed: " . $conn->connect_error);
  12.         }
  13.         $truepassword = hash('sha256', $password); // password hashing using SHA256
  14.         $query = $conn->query("select * from  users where ( username='$email' OR email = '$email') and password='$truepassword'");
  15.         $count = mysqli_num_rows($query);
  16.         if($count == 1) {
  17.             $row = mysqli_fetch_array($query);
  18.             $this->id = $row['id'];
  19.             $this->email = $row['email'];
  20.             return true;
  21.         }else{
  22.             return false;
  23.         }
  24.     }
  25.     private function genJWT() {
  26.         // Make an array for the JWT Payload
  27.         $payload = array(
  28.             "id" => $this->id,
  29.             "email" => $this->email,
  30.             "exp" => time() + (60 * 60)
  31.         );
  32.  
  33.         // encode the payload using our secretkey and return the token
  34.         return JWT::encode($payload, $this->key);
  35.     }
  36.  
  37.     public function checkUser($email, $password) {
  38.         // check if the user exists
  39.         if ($this->validUser($email, $password)) {
  40.             // generate JSON web token and store as variable
  41.             $token = $this->genJWT();
  42.             $resultJSON = array(
  43.               'email' => $this->email,
  44.               'token' => $token
  45.             );
  46.             return json_encode($resultJSON);
  47.         } else {
  48.             return 'We Couldn't Find You In Our Database. Maybe Wrong Email/Password Combination';
  49.         }
  50.     }
  51.  
  52.     private function validJWT($token) {
  53.         $res = array(false, '');
  54.         // using a try and catch to verify
  55.         try {
  56.             //$decoded = JWT::decode($token, $this->key, array('HS256'));
  57.             $decoded = JWT::decode($token, $this->key, array('HS256'));
  58.         } catch (Exception $e) {
  59.             return $res;
  60.         }
  61.         $res['0'] = true;
  62.         $res['1'] = (array) $decoded;
  63.  
  64.         return $res;
  65.     }
  66.  
  67.  
  68.     public function validLogin($token) {
  69.         // checks if an email is valid
  70.         $tokenVal = $this->validJWT($token);
  71.  
  72.         // check if the first array value is true
  73.         if ($tokenVal['0']) {
  74.             // create user session and all that good stuff
  75.             return "Everything went well, time to serve you what you need.";
  76.         } else {
  77.             return "There was an error validating your email. Send another link";
  78.         }
  79.     }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top