111 lines
2.6 KiB
PHP
111 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Webkul\API\Http\Controllers\Customer;
|
|
|
|
use Webkul\API\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Webkul\Customer\Http\Listeners\CustomerEventsHandler;
|
|
use Auth;
|
|
use Cart;
|
|
|
|
/**
|
|
* Session controller for the APIs of user customer
|
|
*
|
|
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
*/
|
|
class AuthController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
protected $_config;
|
|
|
|
public function __construct()
|
|
{
|
|
|
|
$this->middleware('customer')->except(['show','create']);
|
|
$this->_config = request('_config');
|
|
|
|
$subscriber = new CustomerEventsHandler;
|
|
|
|
Event::subscribe($subscriber);
|
|
}
|
|
|
|
public function create(Request $request)
|
|
{
|
|
$request->validate([
|
|
'email' => 'required|email',
|
|
'password' => 'required'
|
|
]);
|
|
|
|
$credentials['email'] = $request->input('email');
|
|
$credentials['password'] = $request->input('password');
|
|
|
|
if ($token = $this->guard()->attempt(request(['email', 'password']))) {
|
|
return $this->respondWithToken($token);
|
|
}
|
|
|
|
return response()->json(['error' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
/**
|
|
* Get the token array structure.
|
|
*
|
|
* @param string $token
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
protected function respondWithToken($token)
|
|
{
|
|
return response()->json([
|
|
'access_token' => $token,
|
|
'token_type' => 'bearer',
|
|
'expires_in' => auth('api')->factory()->getTTL() * 60,
|
|
'customer_id' => auth()->guard('customer')->user()->id,
|
|
'customer_email' => auth()->guard('customer')->user()->email
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the guard to be used during authentication.
|
|
*
|
|
* @return \Illuminate\Contracts\Auth\Guard
|
|
*/
|
|
public function guard()
|
|
{
|
|
return Auth::guard('api');
|
|
}
|
|
|
|
/**
|
|
* Refresh a token.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function refresh()
|
|
{
|
|
return $this->respondWithToken($this->guard()->refresh());
|
|
}
|
|
|
|
/**
|
|
* Get the authenticated User
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function me()
|
|
{
|
|
return response()->json($this->guard()->user());
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$this->guard()->logout();
|
|
|
|
return response()->json(['message' => 'Successfully logged out']);
|
|
}
|
|
}
|