<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\Users;


class UsersController extends Controller
{
  public function __construct()
   {
     // $this->middleware('auth');
   }
   /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
   public function authenticate(Request $request)
   {
       $this->validate($request, [
           'username' => 'required',
           'password' => 'required'
        ]);

      $user = Users::where('username', $request->input('username'))->first();

     // if(Hash::check($request->input('password'), $user->password)){
      if($user != "" && $request->input('password') == $user->password){

          $apikey = base64_encode(str_random(40));
          
          Users::where('username', $request->input('username'))->update(['api_key' => $apikey]);;

          return response()->json(['status' => 'success','api_key' => $apikey, 'user' => $request->input('username')]);

      }else{
        return response()->json(['status' => 'error', 'msg' => "Incorrect Username or password."]);
      }




   }
}    
