sarga/packages/Webkul/API/Http/Controllers/Shop/SessionController.php

139 lines
3.5 KiB
PHP
Raw Normal View History

2019-05-07 11:36:21 +00:00
<?php
namespace Webkul\API\Http\Controllers\Shop;
use Illuminate\Support\Facades\Event;
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\API\Http\Resources\Customer\Customer as CustomerResource;
class SessionController extends Controller
{
/**
* Contains current guard
*
2020-03-05 05:34:57 +00:00
* @var string
2019-05-07 11:36:21 +00:00
*/
protected $guard;
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* Controller instance
*
2020-03-05 05:34:57 +00:00
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
2019-05-07 11:36:21 +00:00
*/
public function __construct(CustomerRepository $customerRepository)
{
$this->guard = request()->has('token') ? 'api' : 'customer';
auth()->setDefaultDriver($this->guard);
$this->middleware('auth:' . $this->guard, ['only' => ['get', 'update', 'destroy']]);
2019-10-18 13:13:38 +00:00
2019-05-07 11:36:21 +00:00
$this->_config = request('_config');
$this->customerRepository = $customerRepository;
}
/**
* Method to store user's sign up form data to DB.
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Http\Response
2019-05-07 11:36:21 +00:00
*/
public function create()
{
request()->validate([
2020-03-04 06:32:53 +00:00
'email' => 'required|email',
'password' => 'required',
2019-05-07 11:36:21 +00:00
]);
$jwtToken = null;
if (! $jwtToken = auth()->guard($this->guard)->attempt(request()->only('email', 'password'))) {
return response()->json([
'error' => 'Invalid Email or Password',
], 401);
}
2019-12-24 14:01:13 +00:00
Event::dispatch('customer.after.login', request('email'));
2019-05-07 11:36:21 +00:00
$customer = auth($this->guard)->user();
2019-10-18 13:13:38 +00:00
2019-05-07 11:36:21 +00:00
return response()->json([
2020-03-05 05:34:57 +00:00
'token' => $jwtToken,
'message' => 'Logged in successfully.',
'data' => new CustomerResource($customer),
]);
2019-05-07 11:36:21 +00:00
}
/**
* Get details for current logged in customer
*
* @return \Illuminate\Http\Response
*/
public function get()
{
$customer = auth($this->guard)->user();
return response()->json([
2020-03-04 06:32:53 +00:00
'data' => new CustomerResource($customer),
2019-05-07 11:36:21 +00:00
]);
}
/**
* Update the specified resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function update()
{
$customer = auth($this->guard)->user();
$this->validate(request(), [
2020-02-19 11:24:04 +00:00
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
2019-05-07 11:36:21 +00:00
'date_of_birth' => 'nullable|date|before:today',
2020-02-19 11:24:04 +00:00
'email' => 'email|unique:customers,email,' . $customer->id,
2020-03-04 06:32:53 +00:00
'password' => 'confirmed|min:6',
2019-05-07 11:36:21 +00:00
]);
$data = request()->all();
if (! $data['date_of_birth']) {
unset($data['date_of_birth']);
}
if (!isset($data['password']) || ! $data['password']) {
unset($data['password']);
} else {
$data['password'] = bcrypt($data['password']);
}
$this->customerRepository->update($data, $customer->id);
return response()->json([
2020-02-27 08:03:03 +00:00
'message' => 'Your account has been created successfully.',
2020-03-04 06:32:53 +00:00
'data' => new CustomerResource($this->customerRepository->find($customer->id)),
2020-02-27 08:03:03 +00:00
]);
2019-05-07 11:36:21 +00:00
}
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\Response
*/
public function destroy()
{
auth()->guard($this->guard)->logout();
return response()->json([
'message' => 'Logged out successfully.',
]);
}
}