<?php

namespace App\Http\Middleware;

use App\Models\Member;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class CheckSwaggerAuth
{
    public function handle(Request $request, Closure $next)
    {
        $token = Session::get('user_token');

        if (! $token) {
            // 원래 URL 저장 후 로그인 페이지로 이동
            Session::put('redirect_to', $request->fullUrl());

            return redirect()->route('login');
        }

        $user = Member::where('remember_token', $token)->first();

        if (! $user) {
            Session::forget('user_token');
            Session::forget('user_id');

            Session::put('redirect_to', $request->fullUrl());

            return redirect()->route('login');
        }

        return $next($request);
    }
}
