<?php

namespace App\Http\Controllers;

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

class LoginController extends Controller
{
    public $successStatus = 200;

    public function login(){
        if(Auth::attempt(['username' => request('username'), 'password' => request('password')])){
            $user = Auth::user();
            $success['token'] =  $user->createToken('nApp')->accessToken;
            $success['user'] = $user;
            return response()->json(['success' => $success], $this->successStatus);
        }
        else{
            return response()->json(['error'=>'Username/Password Salah'], 401);
        }
    }

    public function getUser(){
        if(Auth::user()){
            return response()->json(['success' => true, 'user'=> Auth::user()], 200);
        }
        else{
            return response()->json(['error'=>'Unauthorized'], 401);
        }
    }
}
