Last active
December 9, 2018 02:47
-
-
Save DanielHe4rt/26eec92ab00f99b8867dbff0fe4b5d32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Entities\Auth\AccessLog; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Auth; | |
use Tymon\JWTAuth\Facades\JWTAuth; | |
class AuthController extends Controller | |
{ | |
// Iremos chamar a autenticação da API em todas | |
// as funções desse Controller exceto no Login. | |
public function __construct() | |
{ | |
$this->middleware('auth:api', ['except' => ['login']]); | |
} | |
// Autenticação padrão, porém com o JWT. | |
public function login(Request $request) | |
{ | |
$credentials = $request->only(['email', 'password']); | |
if (! $token = JWTAuth::attempt($credentials)) { | |
return response()->json(['error' => 'Credenciais inválidas'], 401); | |
} | |
return response()->json([ | |
'status' => 'success', | |
'token' => $token | |
]); | |
} | |
// Renovação de Token | |
public function refresh() | |
{ | |
$token = JWTAuth::getToken(); | |
$newToken = JWTAuth::refresh($token); | |
return response()->json([ | |
'token' => $newToken | |
]); | |
} | |
// Retorna as informações da sessão atual | |
public function me(){ | |
return response()->json(Auth::user()); | |
} | |
// Invalida a sessão atual | |
public function logout(){ | |
$token = JWTAuth::getToken(); | |
JWTAuth::invalidate($token); | |
return response()->json([ | |
'status' => 'success' | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment