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.
135 lines
4.6 KiB
135 lines
4.6 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Http\Controllers\Api\BaseAuthApiController as BaseAuthApiController;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class AuthApiController extends BaseAuthApiController
|
|
{
|
|
public $_default_usertype = 'agents';
|
|
//
|
|
public function login(Request $request)
|
|
{
|
|
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
|
|
'username' => 'required|email',
|
|
'password' => 'required'
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $this->sendError('Validation Error.', $validator->errors());
|
|
}
|
|
|
|
if (!Auth::attempt(['email' => $request->username, 'password' => $request->password])) {
|
|
return $this->sendError('Unauthorised.', ['error' => 'Unauthorised']);
|
|
}
|
|
$user = \App\Models\User::where('email', $request->username)->first();
|
|
|
|
if (empty($user)) {
|
|
$this->sendError('Error: ', ['error' => 'Email invalid']);
|
|
}
|
|
$data = [
|
|
'id' => $user->id,
|
|
'address' => $user->address,
|
|
'avatar' => $user->avatar,
|
|
'birthday' => $user->birthday,
|
|
'email' => $user->email,
|
|
'fullname' => $user->name,
|
|
'gender' => $this->getGenderText($user->gender),
|
|
'phone' => $user->phone,
|
|
'last_login' => $user->last_login,
|
|
];
|
|
$data['general_agents_code'] = '';
|
|
$data['general_agents_name'] = '';
|
|
$data['downline_register'] = '';
|
|
$data['promotional_link'] = '';
|
|
$data['role'] = '';
|
|
// Creating a token without scopes...
|
|
$token = $user->createToken($user->id . ' token ' . time(), ['*'])->accessToken;
|
|
|
|
$data['access_token'] = $token;
|
|
$data['token_type'] = 'Bearer';
|
|
$data['expires_in'] = 1296000;
|
|
|
|
$user->update(['last_login' => new \DateTime()]);
|
|
|
|
return response()->json($data, 200);
|
|
}
|
|
public function register(Request $request)
|
|
{
|
|
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
|
|
'email' => 'required|email',
|
|
'fullname' => 'required'
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $this->sendError('Validation Error.', $validator->errors());
|
|
}
|
|
$dataInsert = $this->transformRegisterData($request->all());
|
|
if (!empty($dataInsert)) {
|
|
$user = \App\Models\User::insert($dataInsert);
|
|
return response()->json(['status' => true, 'msg' => $this->getMessageReponse(1)]);
|
|
}
|
|
return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]);
|
|
}
|
|
public function logout(Request $request){
|
|
$token = $request->user()->token();
|
|
// expried token
|
|
$token->revoke();
|
|
|
|
// response
|
|
return response()->json([
|
|
'message' => 'Successfully logged out'
|
|
]);
|
|
}
|
|
function transformRegisterData($data)
|
|
{
|
|
$finalData = [];
|
|
if (!empty($data['email'])) {
|
|
$finalData['email'] = $data['email'];
|
|
}
|
|
if (!empty($data['address'])) {
|
|
$finalData['address'] = $data['address'];
|
|
}
|
|
if (!empty($data['birthday'])) {
|
|
$finalData['birthday'] = $data['birthday'];
|
|
}
|
|
if (!empty($data['fullname'])) {
|
|
$finalData['name'] = $data['fullname'];
|
|
}
|
|
if (!empty($data['phone'])) {
|
|
$finalData['phone'] = $data['phone'];
|
|
}
|
|
$finalData['password'] = !empty($data['password']) ? Hash::make($data['password']) : Hash::make('1qaz2wsxA@');
|
|
|
|
if (!empty($data['gender'])) {
|
|
$finalData['gender'] = !empty($data['gender']) && $data['gender'] == 'male' ? 1 : 0;
|
|
}
|
|
$finalData['user_type'] = !empty($data['role']) ? $data['role'] : $this->_default_usertype;
|
|
|
|
return $finalData;
|
|
}
|
|
function getMessageReponse($key)
|
|
{
|
|
$msg = [
|
|
1 => "Tạo tài khoản thành công.",
|
|
2 => "Tạo tài khoản thất bại.",
|
|
3 => "Email đã tồn tại.",
|
|
4 => "Email không hợp lệ.",
|
|
5 => "Mật khẩu phải có ít nhất 8 ký tự, bao gồm chữ cái và số.",
|
|
6 => "Tên đăng nhập phải có ít nhất 6 ký"
|
|
];
|
|
return $msg[$key] ?? '';
|
|
}
|
|
function getGenderText($gender)
|
|
{
|
|
if ($gender == 1) {
|
|
return 'male';
|
|
}
|
|
if ($gender == 2) {
|
|
return 'female';
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|