wahyun
11 months ago
7 changed files with 263 additions and 104 deletions
@ -1,103 +1,104 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
namespace App\Http\Controllers; |
namespace App\Http\Controllers; |
||||||
|
|
||||||
use Illuminate\Support\Facades\Auth; |
use Illuminate\Support\Facades\Auth; |
||||||
use Illuminate\Http\Request; |
use Illuminate\Http\Request; |
||||||
use Illuminate\Support\Facades\Hash; |
use Illuminate\Support\Facades\Hash; |
||||||
|
|
||||||
use App\Models\User; |
use App\Models\User; |
||||||
use App\Models\Role; |
use App\Models\Role; |
||||||
use App\Models\Company; |
use App\Models\Company; |
||||||
use Illuminate\Http\JsonResponse; |
use Illuminate\Http\JsonResponse; |
||||||
use Illuminate\Support\Facades\Password; |
use Illuminate\Support\Facades\Password; |
||||||
use Illuminate\Validation\ValidationException; |
use Illuminate\Validation\ValidationException; |
||||||
|
|
||||||
|
|
||||||
class AuthController extends Controller |
class AuthController extends Controller |
||||||
{ |
{ |
||||||
public function __construct() |
public function __construct() |
||||||
{ |
{ |
||||||
$this->middleware('auth:api', ['except' => ['login']]); |
$this->middleware('auth:api', ['except' => ['login']]); |
||||||
} |
} |
||||||
|
|
||||||
public function login(Request $request) |
public function login(Request $request) |
||||||
{ |
{ |
||||||
$username = $request->username; |
$username = $request->username; |
||||||
$password = $request->password; |
$email = $request->email; |
||||||
$remember = $request->remember; |
$password = $request->password; |
||||||
$is_mobile = $request->is_mobile; |
$remember = $request->remember; |
||||||
|
$is_mobile = $request->is_mobile; |
||||||
if (empty($username) || empty($password)) |
|
||||||
return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400); |
if (empty($email) && empty($username) || empty($password)) |
||||||
|
return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400); |
||||||
$usernameCheck = false; |
|
||||||
$passwordCheck = false; |
$usernameCheck = false; |
||||||
|
$passwordCheck = false; |
||||||
if (User::where('username', $username)->exists()) |
|
||||||
$usernameCheck = true; |
if (User::where('username', $username)->orWhere('email', $email)->exists()) |
||||||
|
$usernameCheck = true; |
||||||
if (User::where('password', md5($password))->exists()) |
|
||||||
$passwordCheck = true; |
if (User::where('password', md5($password))->exists()) |
||||||
|
$passwordCheck = true; |
||||||
if ($usernameCheck & $passwordCheck) { |
|
||||||
$user = User::where('username', $username)->where('password', md5($password))->first(); |
if ($usernameCheck & $passwordCheck) { |
||||||
if ($is_mobile) { |
$user = User::where('username', $username)->where('password', md5($password))->orWhere('email', $email)->first(); |
||||||
$fcm_token = $request->fcm_token; |
if ($is_mobile) { |
||||||
|
$fcm_token = $request->fcm_token; |
||||||
if (!$fcm_token || $fcm_token == "") |
|
||||||
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400); |
if (!$fcm_token || $fcm_token == "") |
||||||
|
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400); |
||||||
$dataUpdateFcm = array( |
|
||||||
"fcm_token" => $fcm_token |
$dataUpdateFcm = array( |
||||||
); |
"fcm_token" => $fcm_token |
||||||
|
); |
||||||
$hr = User::find($user->id); |
|
||||||
|
$hr = User::find($user->id); |
||||||
if ($hr) |
|
||||||
$hr->update($dataUpdateFcm); |
if ($hr) |
||||||
} |
$hr->update($dataUpdateFcm); |
||||||
|
} |
||||||
$dataRole = Role::find($user->role_id); |
|
||||||
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id); |
$dataRole = Role::find($user->role_id); |
||||||
$configApp = Company::where('id', $user->company_id)->first(); |
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id); |
||||||
|
$configApp = Company::where('id', $user->company_id)->first(); |
||||||
if ($configApp) |
|
||||||
$user->configApp = $configApp; |
if ($configApp) |
||||||
|
$user->configApp = $configApp; |
||||||
if ($dataRole) |
|
||||||
$user->role = $dataRole; |
if ($dataRole) |
||||||
|
$user->role = $dataRole; |
||||||
if ($dataHierarchy) |
|
||||||
$user->hierarchy = $dataHierarchy; |
if ($dataHierarchy) |
||||||
|
$user->hierarchy = $dataHierarchy; |
||||||
if (!$token = Auth::login($user)) |
|
||||||
return response()->json(['error' => 'Unauthorized'], 401); |
if (!$token = Auth::login($user)) |
||||||
|
return response()->json(['error' => 'Unauthorized'], 401); |
||||||
$ttl = 60; |
|
||||||
if ($remember) |
$ttl = 60; |
||||||
$ttl = 10080; |
if ($remember) |
||||||
|
$ttl = 10080; |
||||||
// todo : change existing md5 hashed function to laravel's originally bcrypt |
|
||||||
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */ |
// todo : change existing md5 hashed function to laravel's originally bcrypt |
||||||
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ |
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */ |
||||||
|
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ |
||||||
return response()->json([ |
|
||||||
'code' => 200, |
return response()->json([ |
||||||
'data' => array( |
'code' => 200, |
||||||
'data_user' => $user, |
'data' => array( |
||||||
'access_token' => $token, |
'data_user' => $user, |
||||||
'token_type' => 'bearer', |
'access_token' => $token, |
||||||
'expires_in' => auth()->factory()->getTTL() * $ttl, |
'token_type' => 'bearer', |
||||||
), |
'expires_in' => auth()->factory()->getTTL() * $ttl, |
||||||
]); |
), |
||||||
} else { |
]); |
||||||
if (!$usernameCheck && !$passwordCheck) |
} else { |
||||||
return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); |
if (!$usernameCheck && !$passwordCheck) |
||||||
if (!$passwordCheck) |
return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); |
||||||
return response()->json(['code' => 201, 'message' => "password doesn't match"], 201); |
if (!$passwordCheck) |
||||||
if (!$usernameCheck) |
return response()->json(['code' => 201, 'message' => "password doesn't match"], 201); |
||||||
return response()->json(['code' => 201, 'message' => "username doesn't match"], 201); |
if (!$usernameCheck) |
||||||
} |
return response()->json(['code' => 201, 'message' => "username doesn't match"], 201); |
||||||
} |
} |
||||||
} |
} |
||||||
|
} |
||||||
|
@ -0,0 +1,32 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Http\Controllers; |
||||||
|
|
||||||
|
use App\Models\Demo; |
||||||
|
use Illuminate\Http\Request; |
||||||
|
|
||||||
|
class ScheduleDemoController extends Controller |
||||||
|
{ |
||||||
|
public function add(Request $request) |
||||||
|
{ |
||||||
|
$this->validate($request, [ |
||||||
|
'name' => 'required', |
||||||
|
'role' => 'required', |
||||||
|
'email' => 'required', |
||||||
|
'number_phone' => 'required', |
||||||
|
'status' => 'required', |
||||||
|
'message' => 'required' |
||||||
|
]); |
||||||
|
|
||||||
|
$data = $request->all(); |
||||||
|
|
||||||
|
$data['created_by'] = $this->currentName; |
||||||
|
$result = Demo::create($data); |
||||||
|
|
||||||
|
if ($result) { |
||||||
|
return response()->json(['status' => 'success','data' => $result, 'message' => 'add schedule successfully!', 'code' => 200], 200); |
||||||
|
} else { |
||||||
|
return response()->json(['status' => 'failed', 'message' => 'add data schedule failed!', 'code' => 400], 400); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Models; |
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model; |
||||||
|
|
||||||
|
class Demo extends Model |
||||||
|
{ |
||||||
|
protected $table = 'm_demo'; |
||||||
|
|
||||||
|
const CREATED_AT = 'created_at'; |
||||||
|
const UPDATED_AT = 'updated_at'; |
||||||
|
|
||||||
|
protected $fillable = [ |
||||||
|
'name', |
||||||
|
'email', |
||||||
|
'role', |
||||||
|
'number_phone', |
||||||
|
'status', |
||||||
|
'message', |
||||||
|
'created_at', |
||||||
|
'created_by', |
||||||
|
'updated_at', |
||||||
|
'updated_by', |
||||||
|
]; |
||||||
|
} |
Loading…
Reference in new issue