2018-11-17 13:56:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\API\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use Webkul\API\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
2018-11-19 11:34:13 +00:00
|
|
|
use Webkul\User\Repositories\AdminRepository;
|
2018-11-17 13:56:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Session controller for the APIs of user admins
|
|
|
|
|
*
|
|
|
|
|
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class AuthController extends Controller
|
|
|
|
|
{
|
2018-11-19 11:34:13 +00:00
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
protected $_config;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->middleware('admin')->except(['show','create']);
|
|
|
|
|
$this->_config = request('_config');
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-17 13:56:48 +00:00
|
|
|
public function create(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'email' => 'required|email',
|
|
|
|
|
'password' => 'required'
|
|
|
|
|
]);
|
|
|
|
|
|
2018-11-19 11:34:13 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
2018-11-19 11:34:13 +00:00
|
|
|
return response()->json(['error' => 'Unauthorized'], 401);
|
|
|
|
|
}
|
2018-11-17 13:56:48 +00:00
|
|
|
|
2018-11-19 11:34:13 +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,
|
|
|
|
|
'admin_id' => auth()->guard('admin-api')->user()->id,
|
|
|
|
|
'admin_email' => auth()->guard('admin-api')->user()->email
|
|
|
|
|
]);
|
|
|
|
|
}
|
2018-11-17 13:56:48 +00:00
|
|
|
|
2018-11-19 11:34:13 +00:00
|
|
|
/**
|
|
|
|
|
* Get the guard to be used during authentication.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\Auth\Guard
|
|
|
|
|
*/
|
|
|
|
|
public function guard()
|
|
|
|
|
{
|
|
|
|
|
return auth()->guard('admin-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']);
|
2018-11-17 13:56:48 +00:00
|
|
|
}
|
|
|
|
|
}
|