Commit 0c68d600 authored by Viral Solani's avatar Viral Solani

Refactor Register Method for api

Add UUID for activation
Refactor misc thigs
parent 0f52f655
...@@ -5,6 +5,17 @@ use App\Http\Utilities\SendEmail; ...@@ -5,6 +5,17 @@ use App\Http\Utilities\SendEmail;
use App\Models\Notification\Notification; use App\Models\Notification\Notification;
use App\Models\Settings\Setting; use App\Models\Settings\Setting;
use Carbon\Carbon as Carbon; use Carbon\Carbon as Carbon;
use App\Helpers\uuid;
/**
* Henerate UUID
*
* @return uuid
*/
function generateUuid()
{
return uuid::uuid4();
}
/* /*
* Global helpers file with misc functions. * Global helpers file with misc functions.
......
<?php
/**
* Represents a universally unique identifier (UUID), according to RFC 4122.
*
* This class provides the static methods `uuid3()`, `uuid4()`, and
* `uuid5()` for generating version 3, 4, and 5 UUIDs as specified in RFC 4122.
*
* If all you want is a unique ID, you should call `uuid4()`.
*
* @link http://tools.ietf.org/html/rfc4122
* @link http://en.wikipedia.org/wiki/Universally_unique_identifier
* @link http://www.php.net/manual/en/function.uniqid.php#94959
*/
namespace App\Helpers;
class uuid
{
/**
* When this namespace is specified, the name string is a fully-qualified domain name.
*
* @link http://tools.ietf.org/html/rfc4122#appendix-C
*/
const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is a URL.
*
* @link http://tools.ietf.org/html/rfc4122#appendix-C
*/
const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is an ISO OID.
*
* @link http://tools.ietf.org/html/rfc4122#appendix-C
*/
const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is an X.500 DN in DER or a text output format.
*
* @link http://tools.ietf.org/html/rfc4122#appendix-C
*/
const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
/**
* The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.
*
* @link http://tools.ietf.org/html/rfc4122#section-4.1.7
*/
const NIL = '00000000-0000-0000-0000-000000000000';
private static function getBytes($uuid)
{
if (!self::isValid($uuid)) {
throw new InvalidArgumentException('Invalid UUID string: '.$uuid);
}
// Get hexadecimal components of UUID
$uhex = str_replace([
'urn:',
'uuid:',
'-',
'{',
'}',
], '', $uuid);
// Binary Value
$ustr = '';
// Convert UUID to bits
for ($i = 0; $i < strlen($uhex); $i += 2) {
$ustr .= chr(hexdec($uhex[$i].$uhex[$i + 1]));
}
return $ustr;
}
private static function uuidFromHash($hash, $version)
{
return sprintf('%08s-%04s-%04x-%04x-%12s',
// 32 bits for "time_low"
substr($hash, 0, 8),
// 16 bits for "time_mid"
substr($hash, 8, 4),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number
(hexdec(substr($hash, 12, 4)) & 0x0fff) | $version << 12,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
// 48 bits for "node"
substr($hash, 20, 12));
}
/**
* Generate a version 3 UUID based on the MD5 hash of a namespace identifier
* (which is a UUID) and a name (which is a string).
*
* @param string $namespace The UUID namespace in which to create the named UUID
* @param string $name The name to create a UUID for
*
* @return string
*/
public static function uuid3($namespace, $name)
{
$nbytes = self::getBytes($namespace);
// Calculate hash value
$hash = md5($nbytes.$name);
return self::uuidFromHash($hash, 3);
}
/**
* Generate a version 4 (random) UUID.
*
* @return string
*/
public static function uuid4()
{
$bytes = function_exists('random_bytes') ? random_bytes(16) : openssl_random_pseudo_bytes(16);
$hash = bin2hex($bytes);
return self::uuidFromHash($hash, 4);
}
/**
* Generate a version 5 UUID based on the SHA-1 hash of a namespace
* identifier (which is a UUID) and a name (which is a string).
*
* @param string $namespace The UUID namespace in which to create the named UUID
* @param string $name The name to create a UUID for
*
* @return string
*/
public static function uuid5($namespace, $name)
{
$nbytes = self::getBytes($namespace);
// Calculate hash value
$hash = sha1($nbytes.$name);
return self::uuidFromHash($hash, 5);
}
/**
* Check if a string is a valid UUID.
*
* @param string $uuid The string UUID to test
*
* @return bool
*/
public static function isValid($uuid)
{
return preg_match('/^(urn:)?(uuid:)?(\{)?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}(?(3)\}|)$/i', $uuid) === 1;
}
/**
* Check if two UUIDs are equal.
*
* @param string $uuid1 The first UUID to test
* @param string $uuid2 The second UUID to test
*
* @return bool
*/
public static function equals($uuid1, $uuid2)
{
return self::getBytes($uuid1) === self::getBytes($uuid2);
}
}
...@@ -21,8 +21,7 @@ class AuthController extends APIController ...@@ -21,8 +21,7 @@ class AuthController extends APIController
* Authenticate User. * Authenticate User.
* *
* @param Request $request * @param Request $request
* * @return \Illuminate\Http\JsonResponse
* @return {mix}
*/ */
public function authenticate(Request $request) public function authenticate(Request $request)
{ {
...@@ -62,7 +61,7 @@ class AuthController extends APIController ...@@ -62,7 +61,7 @@ class AuthController extends APIController
/** /**
* Log Out. * Log Out.
* *
* @return JSON Response * @return \Illuminate\Http\JsonResponse
*/ */
public function logout() public function logout()
{ {
...@@ -81,6 +80,12 @@ class AuthController extends APIController ...@@ -81,6 +80,12 @@ class AuthController extends APIController
]); ]);
} }
/**
* Register User
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function register(Request $request) public function register(Request $request)
{ {
$validation = Validator::make($request->all(), [ $validation = Validator::make($request->all(), [
...@@ -92,25 +97,31 @@ class AuthController extends APIController ...@@ -92,25 +97,31 @@ class AuthController extends APIController
]); ]);
if ($validation->fails()) { if ($validation->fails()) {
return response()->json(['message' => $validation->messages()->first()], 422); return $this->throwValidation($validation->messages()->first());
} }
$user = User::create([ $user = User::create([
'email' => request('email'), 'first_name' => request('first_name'),
'status' => 'pending_activation', 'last_name' => request('last_name'),
'password' => bcrypt(request('password')), 'email' => request('email'),
'status' => '0',
'password' => bcrypt(request('password')),
'country_id' => 1,
'state_id' => 1,
'city_id' => 1,
'zip_code' => 1,
'ssn' => 123456789,
'created_by' => 1,
]); ]);
$user->activation_token = generateUuid(); $user->confirmation_code = generateUuid();
$user->save(); $user->save();
/* $profile = new Profile();
$profile->first_name = request('first_name');
$profile->last_name = request('last_name');
$user->profile()->save($profile);*/
$user->notify(new Activation($user)); $user->notify(new Activation($user));
return response()->json(['message' => 'You have registered successfully. Please check your email for activation!']); return $this->respondCreated([
'You have registered successfully. Please check your email for activation!'
]);
} }
public function activate($activation_token) public function activate($activation_token)
......
...@@ -35,7 +35,22 @@ class User extends Authenticatable ...@@ -35,7 +35,22 @@ class User extends Authenticatable
* *
* @var array * @var array
*/ */
protected $fillable = ['first_name', 'last_name', 'email', 'password', 'address', 'country_id', 'state_id', 'city_id', 'zip_code', 'ssn', 'status', 'confirmation_code', 'confirmed', 'created_by']; protected $fillable = [
'first_name',
'last_name',
'email',
'password',
'address',
'country_id',
'state_id',
'city_id',
'zip_code',
'ssn',
'status',
'confirmation_code',
'confirmed',
'created_by'
];
/** /**
* The attributes that should be hidden for arrays. * The attributes that should be hidden for arrays.
......
...@@ -43,7 +43,7 @@ class Activation extends Notification ...@@ -43,7 +43,7 @@ class Activation extends Notification
*/ */
public function toMail($notifiable) public function toMail($notifiable)
{ {
$url = url('/auth/'.$this->user->activation_token.'/activate'); $url = url('/auth/'.$this->user->confirmation_code.'/activate');
return (new MailMessage()) return (new MailMessage())
->greeting('Hello!') ->greeting('Hello!')
......
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
"App\\": "app/" "App\\": "app/"
}, },
"files": [ "files": [
"app/helpers.php" "app/Helpers/helpers.php"
] ]
}, },
"autoload-dev": { "autoload-dev": {
......
...@@ -56,7 +56,7 @@ return [ ...@@ -56,7 +56,7 @@ return [
*/ */
'from' => [ 'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'cygnet.dnjoshi1997@gmail.com'), 'address' => env('MAIL_USERNAME', 'viral.solani@gmail.com'),
'name' => env('MAIL_FROM_NAME', 'Admin'), 'name' => env('MAIL_FROM_NAME', 'Admin'),
], ],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment