<?php

namespace  App\Http\Controllers;

//use
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller{


    public function login(Request $request){
        $username = $request->username;
//        $credentials['name'] = $username;
        $user=User::where('name','=',$username)->first();
        if (!$user){
            return $this->response->array([
                'code' => 100,
                'message' => '账号或密码错误'
            ]);
        }
        $password = md5($request->password);
        if (!hash_equals($password,$user->password)) {
            //返回401
            return $this->response->array([
                'code' => 100,
                'message' => '账号或密码错误'
            ]);
        }

        $token=Auth::guard('api')->fromUser($user);
        return $this->response->array([
            'code'=>200,
            'data'=>[
                'token'=>$token
            ]
        ])->setStatusCode(201);

    }

    public function get_info(){
        return $this->response()->array([
            'code'=>200,
            'data'=>[
                'roles'=>['root'],
                'introduction'=>'路遥知马力',
                'avatar'=>'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                'name'=>'root'
            ]
        ]);
    }
}