Skip to content

Commit 7d5b7f3

Browse files
creating 1st api
1 parent d01de53 commit 7d5b7f3

File tree

10 files changed

+238
-134
lines changed

10 files changed

+238
-134
lines changed

classes/v1/Auth.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,29 @@ public function getIdFromToken($token) {
4545
}
4646
$t = (new Parser())->parse((string) $token);
4747
$data = new ValidationData();
48-
$data->setIssuer(TOKEN_ISSURE)
49-
->setAudience(TOKEN_AUDIENCE)
50-
->setId(TOKEN_ID, true);
48+
$data->setIssuer(TOKEN_ISSURE);
49+
$data->setAudience(TOKEN_AUDIENCE);
50+
$data->setId(TOKEN_ID, true);
5151
if ($t->validate($data) === false) {
5252
throw new Exception('token is not valid');
5353
}
5454
return $t->getClaim('id');
5555
}
56-
56+
5757
public function getUserFromToken($token) {
5858
if (empty($token)) {
5959
throw new Exception('token must be required to extract id');
6060
}
6161
$t = (new Parser())->parse((string) $token);
6262
$data = new ValidationData();
63-
$data->setIssuer(TOKEN_ISSURE)
64-
->setAudience(TOKEN_AUDIENCE)
65-
->setId(TOKEN_ID, true);
63+
$data->setIssuer(TOKEN_ISSURE);
64+
$data->setAudience(TOKEN_AUDIENCE);
65+
$data->setId(TOKEN_ID, true);
6666
if ($t->validate($data) === false) {
6767
throw new Exception('token is not valid');
6868
}
6969
return $t->getClaim('user');
7070
}
71-
7271

7372
/**
7473
*
@@ -103,25 +102,37 @@ public function validateUser() {
103102
public function login() {
104103
echo 'Login';
105104
}
106-
105+
107106
public function signUp() {
108-
107+
109108
$first_name = isset($this->bodyParams['first_name']) ? $this->bodyParams['first_name'] : '';
110109
$last_name = isset($this->bodyParams['last_name']) ? $this->bodyParams['last_name'] : '';
111110
$email = isset($this->bodyParams['email']) ? $this->bodyParams['email'] : '';
112111
$password = isset($this->bodyParams['password']) ? $this->bodyParams['password'] : '';
113-
112+
114113
try {
115114
$this->validation->email($email);
116115
$this->validation->firstName($first_name);
117116
$this->validation->lastName($last_name);
118117
$this->validation->password($password);
118+
119+
$this->db->query('IF EXISTS (select 1 from ' . TBL_USERS . ' where email = ? ) THEN'
120+
. ' SIGNAL SQLSTATE "23000" SET MYSQL_ERRNO = "1452", MESSAGE_TEXT = "EMAIL_ALREADY_EXIST";'
121+
. 'ELSE insert into ' . TBL_USERS . ' (email, first_name, last_name, password, created_at, updated_at) values(?, ?, ?, ?, ?, ?);'
122+
. 'END IF;', array($email, $email, $first_name, $last_name, $password, CURRENT_MILISECOND, CURRENT_MILISECOND));
123+
$user = $this->db->query("select * from " . TBL_USERS . " where email = ? LIMIT 1", array($email))->getFirst();
124+
if ($user == null) {
125+
return $this->response->jsonResponse($this->error->UNKNOWN_ERROR, 500);
126+
}
127+
128+
$token = $this->createToken($user, $user->id);
129+
$u = $this->getUserFromToken($token);
130+
return $this->response->jsonResponse(array('token' => $token), 200);
119131
} catch (ValidationException $ex) {
120-
return $this->response->jsonResponse($ex->getError(), 409);
132+
return $this->response->jsonResponse($ex->getError(), 400);
121133
} catch (Exception $ex) {
122-
return $this->response->jsonResponse($ex->getError(), 409);
134+
return $this->response->jsonResponse($ex->getError(), 500);
123135
}
124-
125136
}
126137

127138
}

db/users.sql

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 4.8.0
3+
-- https://www.phpmyadmin.net/
4+
--
5+
-- Host: localhost
6+
-- Generation Time: Oct 04, 2018 at 07:16 PM
7+
-- Server version: 10.1.31-MariaDB
8+
-- PHP Version: 7.2.4
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
SET AUTOCOMMIT = 0;
12+
START TRANSACTION;
13+
SET time_zone = "+00:00";
14+
15+
16+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
17+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
18+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
19+
/*!40101 SET NAMES utf8mb4 */;
20+
21+
--
22+
-- Database: `core_php_rest_api_starter`
23+
--
24+
25+
-- --------------------------------------------------------
26+
27+
--
28+
-- Table structure for table `users`
29+
--
30+
31+
CREATE TABLE `users` (
32+
`id` bigint(20) NOT NULL,
33+
`first_name` varchar(255) NOT NULL,
34+
`last_name` varchar(255) NOT NULL,
35+
`email` varchar(255) NOT NULL,
36+
`password` varchar(255) NOT NULL,
37+
`created_at` bigint(20) NOT NULL,
38+
`updated_at` bigint(20) NOT NULL
39+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
40+
41+
--
42+
-- Indexes for dumped tables
43+
--
44+
45+
--
46+
-- Indexes for table `users`
47+
--
48+
ALTER TABLE `users`
49+
ADD PRIMARY KEY (`id`);
50+
51+
--
52+
-- AUTO_INCREMENT for dumped tables
53+
--
54+
55+
--
56+
-- AUTO_INCREMENT for table `users`
57+
--
58+
ALTER TABLE `users`
59+
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=24;
60+
COMMIT;
61+
62+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
63+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
64+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

env.php

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
//editable dev, prod or testing
33

44
define('ENV', 'dev');
5+
define('CURRENT_MILISECOND', round(microtime(true) * 1000));
56
?>

exceptions/QueryException.php

+34-21
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,49 @@ public function getError() {
1010
//echo $this->getMessage();
1111
global $error;
1212
if ($this->getCode() != 0) {
13-
switch ($this->getCode()) {
14-
15-
default:
16-
if (ENV === 'dev') {
13+
14+
global $error;
15+
if ($error->{$this->getMessage()} != null) {
16+
return $error->{$this->getMessage()};
17+
} else {
18+
if (ENV === 'dev') {
1719
return $this->getMessage();
1820
} else if (ENV === 'test') {
1921
return $this->getMessage();
2022
} else {
2123
return $error->UNKNOWN_QUERY_ERROR;
2224
}
2325
}
26+
// switch ($this->getCode()) {
27+
//
28+
// default:
29+
// if (ENV === 'dev') {
30+
// return $this->getMessage();
31+
// } else if (ENV === 'test') {
32+
// return $this->getMessage();
33+
// } else {
34+
// return $error->UNKNOWN_QUERY_ERROR;
35+
// }
36+
// }
2437
}
2538

26-
switch ($this->getMessage()) {
27-
28-
case 'REGISTRATION_FAILED':
29-
return $error->REGISTRATION_FAILED;
30-
break;
31-
case 'FAILED_TO_UPDATE_PROFILE_PIC':
32-
return $error->FAILED_TO_UPDATE_PROFILE_PIC;
33-
break;
34-
default:
35-
if (ENV === 'dev') {
36-
return $this->getMessage();
37-
} else if (ENV === 'test') {
38-
return $this->getMessage();
39-
} else {
40-
return $error->UNKNOWN_QUERY_ERROR;
41-
}
42-
}
39+
// switch ($this->getMessage()) {
40+
//
41+
// case 'REGISTRATION_FAILED':
42+
// return $error->REGISTRATION_FAILED;
43+
// break;
44+
// case 'FAILED_TO_UPDATE_PROFILE_PIC':
45+
// return $error->FAILED_TO_UPDATE_PROFILE_PIC;
46+
// break;
47+
// default:
48+
// if (ENV === 'dev') {
49+
// return $this->getMessage();
50+
// } else if (ENV === 'test') {
51+
// return $this->getMessage();
52+
// } else {
53+
// return $error->UNKNOWN_QUERY_ERROR;
54+
// }
55+
// }
4356
}
4457

4558
}

init.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22

3-
require('env.php');
4-
require 'config.php';
5-
require "db.config.php";
3+
require_once 'env.php';
4+
require_once 'config.php';
5+
require_once 'db.config.php';
6+
require_once 'tables.php';
67

7-
require DIR_PATH . "/lib/Response.php";
8-
require DIR_PATH . '/lib/Utils.php';
9-
require_once DIR_PATH . "/lib/ErrorMessage.php";
8+
require_once DIR_PATH . '/lib/Response.php';
9+
require_once DIR_PATH . '/lib/Utils.php';
10+
require_once DIR_PATH . '/lib/ErrorMessage.php';
1011

1112
if (ENV === 'prod') {
1213
error_reporting(0);

lib/DB.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class DB {
44

5-
private $_pdo, $_query, $_count = 0, $_result = array(), $_lastID = NULL, $_errors = array(), $dbuser, $dbname, $dbpass, $dbhost;
5+
private $_pdo, $_query, $_count = 0, $_results = array(), $_lastID = NULL, $_errors = array(), $dbuser, $dbname, $dbpass, $dbhost;
66

77
public function __construct($con_data = array()) {
88
$req_keys = array('dbname', 'dbuser', 'dbpass', 'dbhost');
@@ -36,6 +36,7 @@ public function query($query, $params = array()) {
3636
$this->_count = 0;
3737
$this->_lastID = null;
3838
try {
39+
3940
$this->_query = $this->_pdo->prepare($query);
4041
if (count($params)) {
4142
$i = 1;
@@ -71,7 +72,7 @@ private function action($action, $table, $conditions) {
7172
}
7273

7374
public function lastInsertedId() {
74-
return $this->_lastId;
75+
return $this->_lastID;
7576
}
7677

7778
public function get($table, $conditions = NULL, $limit = NULL) {
@@ -82,6 +83,10 @@ public function get($table, $conditions = NULL, $limit = NULL) {
8283
}
8384
return $this;
8485
}
86+
87+
public function getResult() {
88+
return $this->_results;
89+
}
8590

8691
public function getFirst() {
8792
if ($this->rowCount() > 0) {

lib/ErrorMessage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ErrorMessage {
3535
public $EMAIL_INVALID = array('message' => 'email address is invalid', 'code' => 2002);
3636
public $PASSWORD_EMPTY = array('error' => 'Password missing', 'code' => 2003);
3737
public $FIRST_NAME_EMPTY = array('error' => 'First name missing', 'code' => 2004);
38-
38+
public $EMAIL_ALREADY_EXIST = array('error' => 'Email id already exist', 'code' => 2005);
3939
/**
4040
* Image upload error
4141
* Start with 5100

0 commit comments

Comments
 (0)