Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function login($nick, $email, $password)
- {
- $query = "
- SELECT
- *
- FROM
- " . $this->table_name . "
- WHERE
- email = ?
- OR
- nick = ?
- ";
- // prepare query statement
- $stmt = $this->conn->prepare($query);
- //bind values from user
- $stmt->bindParam(1, $email);
- $stmt->bindParam(2, $nick);
- $stmt->execute();
- if($stmt === true) {
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- $num = $stmt->rowCount();
- if ($num === 1) {
- if (password_verify($password, $row['password'])) {
- $token = array();
- $token['id'] = $row['id'];
- $encoded_token = JWT::encode($token, 'secret_server_key');
- $query = "
- UPDATE
- " . $this->table_name . "
- SET
- token = " . $encoded_token . "
- WHERE
- id = " . $row['id'] . "
- ";
- $stmt_token = $this->conn->prepare($query);
- $stmt_token->execute();
- if ($stmt_token === true)
- {
- http_response_code(200);
- return true;
- }
- }
- else
- {
- http_response_code(402);
- return false;
- }
- }
- else
- {
- http_response_code(401);
- return false;
- }
- }
- else
- {
- http_response_code(400);
- }
- }
- <?php
- if(isset($_POST['submit']))
- {
- if(
- (
- (isset($_POST['nick']) && !empty($_POST['nick']))
- ||
- (isset($_POST['email']) && !empty($_POST['email']))
- ) &&
- (isset($_POST['password']) && !empty($_POST['password']))
- )
- {
- // instantiate database
- require_once '../config/database.php';
- $database = new Database();
- $db = $database->getConnection();
- require_once '../classes/user.php';
- $user = new User($db);
- if(!isset($_POST['nick']))
- {
- $user_nick = null;
- $user->login($user_nick, $_POST['email'], $_POST['password']);
- }
- else if(!isset($_POST['email']))
- {
- $user_email = null;
- $user->login($_POST['nick'], $user_email, $_POST['password']);
- }
- }
- else
- {
- http_response_code(400);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement