<?php

namespace App\Http\Controllers;

use App\Models\Usuarios;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function login(Request $request){
    Log::info('hola');
        $credenciales = Usuarios::where('email', $request->email)->first();
        if ($credenciales) {
            if (!Auth::loginUsingId($credenciales->id)) {//cambiar a login
                return response([
                    'message' => 'error en credenciales',
                ], 401);
            }
            $access_token = Auth::user()->createToken('authToken')->accessToken;//llave secreta

            return response([
                'usuario' => Auth::user(),
                'token' => $access_token
            ]);
        } else {
            return response([
                'message' => 'error en credenciales',
            ], 401);
        }
    }
}
