elektronika_bagisto/packages/Webkul/API/Http/Controllers/Admin/AuthController.php

103 lines
2.5 KiB
PHP

<?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;
use Webkul\User\Repositories\AdminRepository;
/**
* 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
{
/**
* 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');
}
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,
'admin_id' => auth()->guard('admin-api')->user()->id,
'admin_email' => auth()->guard('admin-api')->user()->email
]);
}
/**
* 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']);
}
}