2018-07-24 14:33:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Customer\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
2018-12-21 12:48:34 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2018-11-22 12:43:03 +00:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Webkul\Customer\Mail\VerificationEmail;
|
2018-07-24 14:33:49 +00:00
|
|
|
use Illuminate\Routing\Controller;
|
2018-08-22 09:46:27 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
2018-11-27 10:55:23 +00:00
|
|
|
use Cookie;
|
2018-07-24 14:33:49 +00:00
|
|
|
|
|
|
|
|
/**
|
2018-08-23 13:11:56 +00:00
|
|
|
* Registration controller
|
2018-07-24 14:33:49 +00:00
|
|
|
*
|
|
|
|
|
* @author Prashant Singh <prashant.singh852@webkul.com>
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class RegistrationController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
protected $_config;
|
2018-08-23 13:11:56 +00:00
|
|
|
protected $customer;
|
2018-07-24 14:33:49 +00:00
|
|
|
|
2018-11-27 10:55:23 +00:00
|
|
|
/**
|
|
|
|
|
* @param CustomerRepository object $customer
|
|
|
|
|
*/
|
2018-12-21 12:48:34 +00:00
|
|
|
public function __construct(CustomerRepository $customer)
|
|
|
|
|
{
|
2018-07-24 14:33:49 +00:00
|
|
|
$this->_config = request('_config');
|
2018-08-23 13:11:56 +00:00
|
|
|
$this->customer = $customer;
|
2018-07-24 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-27 12:07:22 +00:00
|
|
|
* Opens up the user's sign up form.
|
2018-09-22 08:31:18 +00:00
|
|
|
*
|
2018-07-24 14:33:49 +00:00
|
|
|
* @return view
|
|
|
|
|
*/
|
2018-12-21 12:48:34 +00:00
|
|
|
public function show()
|
|
|
|
|
{
|
2018-07-24 14:33:49 +00:00
|
|
|
return view($this->_config['view']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-27 12:07:22 +00:00
|
|
|
* Method to store user's sign up form data to DB.
|
2018-09-22 08:31:18 +00:00
|
|
|
*
|
|
|
|
|
* @return Mixed
|
2018-07-24 14:33:49 +00:00
|
|
|
*/
|
2018-12-21 12:48:34 +00:00
|
|
|
public function create(Request $request)
|
|
|
|
|
{
|
2018-07-24 14:33:49 +00:00
|
|
|
$request->validate([
|
|
|
|
|
'first_name' => 'string|required',
|
|
|
|
|
'last_name' => 'string|required',
|
2018-10-25 07:46:45 +00:00
|
|
|
'email' => 'email|required|unique:customers,email',
|
2018-09-22 08:31:18 +00:00
|
|
|
'password' => 'confirmed|min:6|required',
|
2018-12-27 10:52:44 +00:00
|
|
|
// 'agreement' => 'required'
|
2018-07-24 14:33:49 +00:00
|
|
|
]);
|
2018-08-22 09:46:27 +00:00
|
|
|
|
2018-09-22 08:31:18 +00:00
|
|
|
$data = request()->input();
|
|
|
|
|
|
|
|
|
|
$data['password'] = bcrypt($data['password']);
|
|
|
|
|
|
2018-10-23 11:46:00 +00:00
|
|
|
$data['channel_id'] = core()->getCurrentChannel()->id;
|
|
|
|
|
|
2018-11-22 12:43:03 +00:00
|
|
|
$data['is_verified'] = 0;
|
|
|
|
|
|
2018-12-07 05:25:33 +00:00
|
|
|
$data['customer_group_id'] = 1;
|
|
|
|
|
|
2018-11-27 10:55:23 +00:00
|
|
|
$verificationData['email'] = $data['email'];
|
|
|
|
|
$verificationData['token'] = md5(uniqid(rand(), true));
|
|
|
|
|
$data['token'] = $verificationData['token'];
|
2018-11-22 12:43:03 +00:00
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
Event::fire('customer.registration.before');
|
|
|
|
|
|
|
|
|
|
$customer = $this->customer->create($data);
|
|
|
|
|
|
|
|
|
|
Event::fire('customer.registration.after', $customer);
|
|
|
|
|
|
|
|
|
|
if ($customer) {
|
2018-11-27 10:55:23 +00:00
|
|
|
try {
|
|
|
|
|
session()->flash('success', trans('shop::app.customer.signup-form.success'));
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2018-11-27 10:55:23 +00:00
|
|
|
Mail::send(new VerificationEmail($verificationData));
|
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
|
session()->flash('info', trans('shop::app.customer.signup-form.success-verify-email-not-sent'));
|
2018-08-22 09:46:27 +00:00
|
|
|
|
2018-11-22 12:43:03 +00:00
|
|
|
return redirect()->route($this->_config['redirect']);
|
|
|
|
|
}
|
2018-11-27 10:55:23 +00:00
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
2018-07-24 14:33:49 +00:00
|
|
|
} else {
|
2018-11-27 10:55:23 +00:00
|
|
|
session()->flash('error', trans('shop::app.customer.signup-form.failed'));
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2018-07-24 14:33:49 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 12:43:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method to verify account
|
|
|
|
|
*
|
2018-12-21 12:50:58 +00:00
|
|
|
* @param string $token
|
2018-11-22 12:43:03 +00:00
|
|
|
*/
|
2018-12-21 12:48:34 +00:00
|
|
|
public function verifyAccount($token)
|
|
|
|
|
{
|
2018-11-27 10:55:23 +00:00
|
|
|
$customer = $this->customer->findOneByField('token', $token);
|
2018-11-22 12:43:03 +00:00
|
|
|
|
|
|
|
|
if($customer) {
|
2018-11-27 10:55:23 +00:00
|
|
|
$customer->update(['is_verified' => 1, 'token' => 'NULL']);
|
|
|
|
|
|
|
|
|
|
session()->flash('success', trans('shop::app.customer.signup-form.verified'));
|
2018-11-22 12:43:03 +00:00
|
|
|
} else {
|
2018-11-27 10:55:23 +00:00
|
|
|
session()->flash('warning', trans('shop::app.customer.signup-form.verify-failed'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->route('customer.session.index');
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 12:48:34 +00:00
|
|
|
public function resendVerificationEmail($email)
|
|
|
|
|
{
|
2018-11-27 10:55:23 +00:00
|
|
|
$verificationData['email'] = $email;
|
|
|
|
|
$verificationData['token'] = md5(uniqid(rand(), true));
|
|
|
|
|
|
|
|
|
|
$customer = $this->customer->findOneByField('email', $email);
|
|
|
|
|
|
|
|
|
|
$this->customer->update(['token' => $verificationData['token']], $customer->id);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Mail::send(new VerificationEmail($verificationData));
|
|
|
|
|
|
|
|
|
|
if(Cookie::has('enable-resend')) {
|
|
|
|
|
\Cookie::queue(\Cookie::forget('enable-resend'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(Cookie::has('email-for-resend')) {
|
|
|
|
|
\Cookie::queue(\Cookie::forget('email-for-resend'));
|
|
|
|
|
}
|
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
|
session()->flash('success', trans('shop::app.customer.signup-form.verification-not-sent'));
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-11-22 12:43:03 +00:00
|
|
|
}
|
2018-11-27 10:55:23 +00:00
|
|
|
session()->flash('success', trans('shop::app.customer.signup-form.verification-sent'));
|
2018-11-22 12:43:03 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|