Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.0 KiB

3 years ago
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Role;
class AuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
3 years ago
public function login(Request $request)
{
$username = $request->username;
$password = $request->password;
$is_mobile = $request->is_mobile;
if(empty($username) || empty($password)){
return response()->json(['status'=>'error','message'=>'You must fill all the fields'], 400);
}
$user = User::where('username', $username)->where('password', md5($password))->first();
3 years ago
if($is_mobile){
$fcm_token = $request->fcm_token;
if(!$fcm_token || $fcm_token==""){
return response()->json(['status'=>'error','message'=>'FCM Token is required'], 400);
die();
}
$dataUpdateFcm = array(
"fcm_token"=>$fcm_token
);
$hr = User::find($user->id);
if($hr){
$hr->update($dataUpdateFcm);
3 years ago
}
}
$dataRole = Role::find($user->role_id);
if($dataRole){
$user->role = $dataRole;
}
3 years ago
if (! $token =Auth::login($user)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token, $user);
}
protected function respondWithToken($token, $user)
{
return response()->json([
'code' => 200,
3 years ago
'data' => array(
'data_user' => $user,
'access_token' => $token,
3 years ago
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 360
3 years ago
),
]);
}
}