<?php

namespace Modules\Auth\app\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Auth\app\Models\User;

class LoginController extends Controller
{
    protected function respondWithToken()
    {
        return response()->json([
            'access_token' => User::getTokenByAuth(),
            'expires_in' => auth('api')->factory()->getTTL() * 60,
            'user' => auth()->user(),
            'status' => true
        ]);
    }

    /**
     * handle login api
     *
     * @param \Illuminate\Http\Request $request
     * @return void
     */
    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');
        if (auth()->attempt($credentials)) {
            return $this->respondWithToken();
        }
        return response()->json([
            'message' => 'Incorret email or password.',
            'status' => false
        ], 200);
    }

    public function logout(){
        Auth::guard('api')->logout();
        return response()->json([
            'message' => 'Logout',
            'status' => true
        ], 200);
    }
}
