sarga/packages/Webkul/API/Http/Controllers/Customer/AuthController.php

111 lines
2.6 KiB
PHP
Raw Normal View History

2018-11-17 13:56:48 +00:00
<?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;
2018-11-17 13:56:48 +00:00
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);
2018-11-17 13:56:48 +00:00
}
return response()->json(['error' => 'Unauthorized'], 401);
}
2018-11-17 13:56:48 +00:00
/**
* 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
]);
}
2018-11-17 13:56:48 +00:00
/**
* Get the guard to be used during authentication.
*
* @return \Illuminate\Contracts\Auth\Guard
*/
public function guard()
{
return Auth::guard('api');
2018-11-17 13:56:48 +00:00
}
/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
2018-11-17 13:56:48 +00:00
{
return $this->respondWithToken($this->guard()->refresh());
}
2018-11-17 13:56:48 +00:00
/**
* Get the authenticated User
*
* @return \Illuminate\Http\JsonResponse
*/
public function me()
{
return response()->json($this->guard()->user());
}
public function destroy($id)
{
$this->guard()->logout();
2018-11-17 13:56:48 +00:00
return response()->json(['message' => 'Successfully logged out']);
2018-11-17 13:56:48 +00:00
}
}