<?php

namespace App\Http\Controllers\Environment;

use Illuminate\Support\Facades\Auth;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;

class AuthController extends Controller
{
  //* Constructor
    public function __construct() {
        $this->middleware('auth')->only(['logout']);
    }

  //* Special Functions
    public function logout(Request $request) {
        // Extract User from request
        $user = $request->user();

        // Get Device Data
        $device = $request->header('X-Device-Id', 'default');
        $user->tokens()->where('name', $device)->delete(); // Delete device's token

        // Conclude session as Fortify
        Auth::guard('web')->logout();

        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return response()->json(['message' => 'Sesión cerrada']);
    }
}
