Skip to content

Instantly share code, notes, and snippets.

@programarivm
Created January 3, 2020 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save programarivm/8401b37d3ff7ae2623b6e3ea8deb38f1 to your computer and use it in GitHub Desktop.
Save programarivm/8401b37d3ff7ae2623b6e3ea8deb38f1 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AuthController extends Controller
{
const COOKIE_ACCESS_TOKEN = 'access_token';
const COOKIE_SESSION = 'session';
public function login()
{
$credentials = request(['email', 'password']);
if (!$token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
$session = [
'role' => auth()->user()->getAttributes()['role'],
];
return response(null, 204)
->cookie(self::COOKIE_ACCESS_TOKEN, $token, 480)
->cookie(self::COOKIE_SESSION, json_encode($session), 480, null, null, null, false);
}
public function logout()
{
$accessTokenCookie = \Cookie::forget(self::COOKIE_ACCESS_TOKEN);
$sessionCookie = \Cookie::forget(self::COOKIE_SESSION);
return response(null, 204)
->withCookie($accessTokenCookie)
->withCookie($sessionCookie);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment