PARAG-57, PARAG-59

This commit is contained in:
s152483 2020-01-13 16:49:05 +00:00
parent 5a5d1a33b8
commit 950ca6605a
5 changed files with 351 additions and 0 deletions

52
LoginController.php Normal file
View File

@ -0,0 +1,52 @@
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//$this->middleware('guest')->except('logout');
}
public function loginAPI(Request $request){
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
if(Auth::guard('api')->attempt([
'email' => $request->email,
'password' => $request->password]))
{
return response()->json(["message " => "Accepted."], 200);
}
else{
return response()->json(["message " => "Record not found."], 202);
}
}
}

110
RegisterController.php Normal file
View File

@ -0,0 +1,110 @@
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Picqer;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
public function makeBarcodeExample(Request $request)
{
$label = $request->barcode;
$barcode_generator = new Picqer\Barcode\BarcodeGeneratorPNG();
$barcode = $barcode_generator->getBarcode($label, $barcode_generator::TYPE_CODE_128);
echo '<img src="data:image/png;base64,' . base64_encode($barcode) . '">';
echo '<p>'.$label;
}
public function makeBarcode(Request $request)
{
$label = $request->barcode;
$barcode_generator = new Picqer\Barcode\BarcodeGeneratorPNG();
$barcode = $barcode_generator->getBarcode($label, $barcode_generator::TYPE_CODE_128);
echo '<img src="data:image/png;base64,' . base64_encode($barcode) . '">';
echo '<p>'.$label;
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'barcode' => ['required', 'string'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function createAPI(Request $request)
{
if($user = User::where('email', $request->email)->exists())
{
return response()->json(["message " => "User exists."], 404);
}
else
{
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
return response()->json($user, 201);
}
}
protected function create(array $data)
{
#self::makeBarcode($data['barcode']);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}

52
StoreLoginController.php Normal file
View File

@ -0,0 +1,52 @@
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class StoreLoginController extends Controller
{
public function __construct(){
//$this->middleware('guest:store');
}
public function showLoginForm(){
return view('auth.store-login');
}
public function login(Request $request){
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
if(Auth::guard('store')->attempt([
'email' => $request->email,
'password' => $request->password], $request->remember))
{
return redirect()->intended(route('store.dashboard'));
}
return redirect()->back();
}
public function loginAPI(Request $request){
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);
if(Auth::guard('store')->attempt([
'email' => $request->email,
'password' => $request->password]))
{
return response()->json(["message " => "Accepted."], 200);
}
else{
return response()->json(["message " => "Record not found."], 404);
}
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Models\Store;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class StoreRegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:stores'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function createAPI(Request $request)
{
if($store = Store::where('email', $request->email)->exists())
{
return response()->json(["message " => "Store exists."], 404);
}
else
{
$store = Store::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
return response()->json($store, 201);
}
}
protected function create(Request $request)
{
$store = Store::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
if(Auth::guard('store')->attempt([
'email' => $request->email,
'password' => $request->password]))
{
return redirect()->intended(route('store.dashboard'));
}
}
public function showRegisterForm(){
return view('auth.store-register');
}
}

41
api.php Normal file
View File

@ -0,0 +1,41 @@
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::prefix('user')->group(function(){
Route::post('/login', 'Auth\LoginController@loginAPI');
Route::post('/register', 'Auth\RegisterController@createAPI');
Route::get('/receipt', 'ReceiptController@index');
Route::get('/receipt/{id}', 'ReceiptController@show');
Route::get('/servicesByCategory/{category}', 'ServiceController@showByCategory');
});
Route::prefix('store')->group(function(){
Route::post('login', 'Auth\StoreLoginController@loginAPI');
Route::post('register', 'Auth\StoreRegisterController@createAPI');
Route::post('/receipt', 'ReceiptController@store');
Route::put('/receipt/{id}', 'ReceiptController@update');
Route::delete('/receipt/{id}', 'ReceiptController@destroy');
});
Route::prefix('service')->group(function(){
Route::post('login', 'Auth\ServiceLoginController@loginAPI');
Route::post('register', 'Auth\ServiceRegisterController@createAPI');
});
Route::apiResource('receiptItem', 'ReceiptItemController');