2022-06-24 11:56:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2022-07-06 12:38:03 +00:00
|
|
|
use App\Mail\EmailVerification;
|
2022-06-30 14:55:51 +00:00
|
|
|
use App\Mail\ResetPassword;
|
2022-06-24 11:56:01 +00:00
|
|
|
use App\Models\Client;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2022-06-30 14:55:51 +00:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
2022-07-06 12:38:03 +00:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2022-06-24 11:56:01 +00:00
|
|
|
|
2022-06-27 06:32:28 +00:00
|
|
|
/**
|
|
|
|
|
* @OA\Info(
|
2022-07-01 06:05:41 +00:00
|
|
|
* title="Legalization API",
|
2022-06-27 06:32:28 +00:00
|
|
|
* version="1.0.1"
|
|
|
|
|
* )
|
2022-06-30 14:55:51 +00:00
|
|
|
* @OA\SecurityScheme(
|
|
|
|
|
* securityScheme="bearerAuth",
|
|
|
|
|
* in="header",
|
|
|
|
|
* name="bearerAuth",
|
|
|
|
|
* type="http",
|
|
|
|
|
* scheme="bearer",
|
|
|
|
|
* bearerFormat="JWT",
|
|
|
|
|
* ),
|
2022-06-27 06:32:28 +00:00
|
|
|
*/
|
2022-06-24 11:56:01 +00:00
|
|
|
|
2022-06-30 14:55:51 +00:00
|
|
|
|
2022-06-24 11:56:01 +00:00
|
|
|
//controller where all auth process for client happens
|
|
|
|
|
class AuthController extends Controller
|
|
|
|
|
{
|
|
|
|
|
|
2022-06-27 06:32:28 +00:00
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
* path="/api/login",
|
|
|
|
|
* summary=" - Login user",
|
2022-06-30 14:55:51 +00:00
|
|
|
* tags = {"Authorization"},
|
2022-06-27 06:32:28 +00:00
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/json",
|
|
|
|
|
* @OA\Schema(
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="email",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="password",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
2022-06-30 14:55:51 +00:00
|
|
|
* example={"email": "ilmedovamahri@gmail.com", "password": 12345678}
|
2022-06-27 06:32:28 +00:00
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
2022-06-30 14:55:51 +00:00
|
|
|
* description="OK"
|
2022-06-27 06:32:28 +00:00
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="401",
|
|
|
|
|
* description="Unauthorized"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
2022-06-30 14:55:51 +00:00
|
|
|
public function login(Request $request){
|
2022-07-07 09:16:39 +00:00
|
|
|
$data = $request->all();
|
|
|
|
|
$rules= [
|
2022-07-07 10:31:18 +00:00
|
|
|
'email'=>'required|email',
|
2022-07-07 09:16:39 +00:00
|
|
|
'password' => 'required'
|
|
|
|
|
];
|
2022-06-24 11:56:01 +00:00
|
|
|
|
2022-07-07 09:16:39 +00:00
|
|
|
$validator = Validator::make($data, $rules);
|
2022-06-24 11:56:01 +00:00
|
|
|
|
2022-07-07 09:16:39 +00:00
|
|
|
if($validator->fails()){
|
2022-07-07 10:31:18 +00:00
|
|
|
return response()->json(['message' => 'validation failed' ,'errors'=>$validator->errors()],422);
|
2022-07-07 09:16:39 +00:00
|
|
|
}
|
2022-06-24 11:56:01 +00:00
|
|
|
|
2022-07-07 09:16:39 +00:00
|
|
|
$client = Client::where('email', request()->email)->first();
|
2022-06-24 11:56:01 +00:00
|
|
|
|
2022-07-07 09:16:39 +00:00
|
|
|
if($client){
|
|
|
|
|
if (!Hash::check(request()->password, $client->password)){
|
|
|
|
|
return response()->json([
|
|
|
|
|
'message' => 'Unauthorized'
|
|
|
|
|
], 401);
|
2022-07-06 12:38:03 +00:00
|
|
|
}
|
2022-07-07 09:16:39 +00:00
|
|
|
|
|
|
|
|
Auth::login($client);
|
|
|
|
|
$tokenResult = $client->createToken('auth_token');
|
|
|
|
|
|
|
|
|
|
return response()->json(['data' => ['token' => $tokenResult, 'client' => $client]], 200);
|
2022-07-05 12:36:10 +00:00
|
|
|
}
|
2022-07-07 09:16:39 +00:00
|
|
|
return response()->json(['message' => 'email not found'], 404);
|
2022-06-24 11:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 06:32:28 +00:00
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
* path="/api/register",
|
|
|
|
|
* summary=" - Register user",
|
2022-06-30 14:55:51 +00:00
|
|
|
* tags = {"Authorization"},
|
2022-06-27 06:32:28 +00:00
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/json",
|
|
|
|
|
* @OA\Schema(
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="firstname",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="lastname",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="email",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="password",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
2022-07-07 09:16:39 +00:00
|
|
|
* @OA\Property(
|
|
|
|
|
* property="confirm_password",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* example={"firstname":"Mahri", "lastname":"Ilmedova" ,"email": "ilmedovamahri@gmail.com", "password": 12345678, "confirm_password": 12345678}
|
2022-06-27 06:32:28 +00:00
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
2022-06-30 14:55:51 +00:00
|
|
|
* description="OK"
|
2022-06-27 06:32:28 +00:00
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="401",
|
|
|
|
|
* description="Unauthorized"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
2022-06-30 14:55:51 +00:00
|
|
|
public function register(Request $request){
|
2022-07-07 09:16:39 +00:00
|
|
|
$data = $request->all();
|
|
|
|
|
$rules= [
|
2022-07-07 07:39:35 +00:00
|
|
|
'email' => 'required|email|unique:clients,email',
|
|
|
|
|
'password' => 'required|min:6',
|
2022-07-07 09:16:39 +00:00
|
|
|
'confirm_password' => 'required|same:password',
|
2022-07-07 07:39:35 +00:00
|
|
|
'firstname' => 'required',
|
|
|
|
|
'lastname' => 'required',
|
2022-07-07 09:16:39 +00:00
|
|
|
];
|
|
|
|
|
$validator = Validator::make($data, $rules);
|
|
|
|
|
|
|
|
|
|
if($validator->fails()){
|
2022-07-07 10:31:18 +00:00
|
|
|
return response()->json(['message' => 'validation failed' ,'errors'=>$validator->errors()],422);
|
2022-07-07 09:16:39 +00:00
|
|
|
}
|
2022-07-06 13:41:48 +00:00
|
|
|
|
2022-07-07 07:39:35 +00:00
|
|
|
$data = $request->all();
|
2022-07-06 13:41:48 +00:00
|
|
|
|
2022-07-07 07:39:35 +00:00
|
|
|
$data['password'] = Hash::make($data['password']);
|
2022-07-06 13:41:48 +00:00
|
|
|
|
2022-07-07 10:31:18 +00:00
|
|
|
$email_verification = (bool) Config::get('settings.email_verification');
|
2022-07-07 07:39:35 +00:00
|
|
|
$data['is_verified'] = $email_verification;
|
2022-07-06 13:41:48 +00:00
|
|
|
|
2022-07-07 07:39:35 +00:00
|
|
|
$data['token'] = rand(1000, 9999);//generate code;
|
2022-07-07 09:16:39 +00:00
|
|
|
$data['status'] = 0 ;
|
2022-07-06 13:41:48 +00:00
|
|
|
|
2022-07-07 07:39:35 +00:00
|
|
|
try{
|
|
|
|
|
$client = Client::create($data);
|
2022-07-06 13:41:48 +00:00
|
|
|
|
2022-07-07 07:39:35 +00:00
|
|
|
if($email_verification)
|
|
|
|
|
{
|
2022-07-07 09:16:39 +00:00
|
|
|
Mail::to($request->email)->queue(new EmailVerification($request->firstname, $data['token']));
|
2022-07-06 13:41:48 +00:00
|
|
|
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'confirm code',
|
|
|
|
|
'data' => [
|
|
|
|
|
'email' => $request->email
|
|
|
|
|
]
|
2022-07-06 13:41:48 +00:00
|
|
|
],200);
|
|
|
|
|
}
|
2022-07-07 07:39:35 +00:00
|
|
|
else{
|
|
|
|
|
Auth::login($client);
|
|
|
|
|
$tokenResult = $client->createToken('auth_token');
|
2022-07-06 12:38:03 +00:00
|
|
|
|
|
|
|
|
return response()->json([
|
2022-07-07 07:39:35 +00:00
|
|
|
'data' => [
|
|
|
|
|
'token' => $tokenResult,
|
|
|
|
|
'client' => $client
|
2022-07-07 07:18:57 +00:00
|
|
|
]
|
2022-07-07 07:39:35 +00:00
|
|
|
], 200);
|
2022-07-06 12:38:03 +00:00
|
|
|
}
|
2022-07-06 13:41:48 +00:00
|
|
|
}
|
2022-07-06 13:42:43 +00:00
|
|
|
catch(\Exception $e){
|
2022-07-07 07:39:35 +00:00
|
|
|
return response()->json(['message' => $e->getMessage()], 400);
|
2022-06-27 06:32:28 +00:00
|
|
|
}
|
2022-06-24 11:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-06 13:37:51 +00:00
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
* path="/api/verify-email",
|
|
|
|
|
* summary=" - Verify email of client",
|
|
|
|
|
* tags = {"Authorization"},
|
|
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/json",
|
|
|
|
|
* @OA\Schema(
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="email",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="token",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* example={"email": "ilmedovamahri@gmail.com", "token": "4515"}
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="OK"
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="400",
|
|
|
|
|
* description="Missing fields (email or token)"
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="404",
|
|
|
|
|
* description="Client not found"
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="401",
|
|
|
|
|
* description="Unauthorised. Tokens do not match"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
2022-07-06 12:38:03 +00:00
|
|
|
public function verifyEmail(Request $request){
|
2022-07-07 07:48:12 +00:00
|
|
|
$request->validate([
|
|
|
|
|
'email' => 'required|email',
|
|
|
|
|
'token' => 'required'
|
|
|
|
|
]);
|
2022-07-06 13:37:51 +00:00
|
|
|
|
2022-07-07 07:48:12 +00:00
|
|
|
$client = Client::where('email', $request->email)->first();
|
2022-07-06 13:37:51 +00:00
|
|
|
|
2022-07-07 07:48:12 +00:00
|
|
|
if($client){
|
|
|
|
|
if($client->token === $request->token){
|
|
|
|
|
Auth::login($client);
|
|
|
|
|
$tokenResult = $client->createToken('auth_token');
|
|
|
|
|
|
|
|
|
|
return response()->json(['data' => ['token' => $tokenResult, 'client' => $client]], 200);
|
2022-07-06 13:37:51 +00:00
|
|
|
}
|
|
|
|
|
else{
|
2022-07-07 09:16:39 +00:00
|
|
|
return response()->json(['message' => 'tokens don\'t match'], 401);
|
2022-07-06 13:37:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-07 07:48:12 +00:00
|
|
|
else{
|
2022-07-07 07:18:57 +00:00
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'no such client'
|
2022-07-07 07:18:57 +00:00
|
|
|
], 404);
|
2022-07-06 13:37:51 +00:00
|
|
|
}
|
2022-07-06 12:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
2022-06-30 14:55:51 +00:00
|
|
|
/**
|
|
|
|
|
* @OA\GET(
|
|
|
|
|
* path="/api/client",
|
|
|
|
|
* summary=" - Get user",
|
|
|
|
|
* tags = {"Authorization"},
|
|
|
|
|
* security={
|
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
|
* },
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="OK"
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="401",
|
|
|
|
|
* description="Unauthorized"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
public function client(Request $request) {
|
2022-07-07 06:13:12 +00:00
|
|
|
try{
|
|
|
|
|
$user = $request->user();
|
|
|
|
|
if($user){
|
|
|
|
|
return response()->json([
|
2022-07-07 07:18:57 +00:00
|
|
|
'data' => [
|
2022-07-07 06:13:12 +00:00
|
|
|
'client' => $request->user()
|
|
|
|
|
]
|
|
|
|
|
],200);
|
|
|
|
|
}
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'token_expired'
|
2022-07-07 06:13:12 +00:00
|
|
|
], 401);
|
|
|
|
|
}
|
|
|
|
|
catch(\Exception $e){
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'Oops! Something went wrong'
|
|
|
|
|
], 500);
|
2022-06-30 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
* path="/api/logout",
|
|
|
|
|
* summary=" - Logout user",
|
|
|
|
|
* tags = {"Authorization"},
|
|
|
|
|
* security={
|
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
|
* },
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="OK"
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="401",
|
|
|
|
|
* description="Unauthorized"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
public function logout(Request $request) {
|
2022-07-07 06:13:12 +00:00
|
|
|
try{
|
|
|
|
|
// Revoke the token that was used to authenticate the current request
|
|
|
|
|
$request->user()->currentAccessToken()->delete();
|
|
|
|
|
//$request->user->tokens()->delete(); // use this to revoke all tokens (logout from all devices)
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'ok'
|
2022-07-07 06:13:12 +00:00
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
catch(\Exception $e){
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => $e->getMessage()
|
2022-07-07 06:13:12 +00:00
|
|
|
], 200);
|
|
|
|
|
}
|
2022-06-30 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
* path="/api/forgot-password",
|
|
|
|
|
* summary=" - Send a user password reset link",
|
|
|
|
|
* tags = {"Authorization"},
|
|
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/json",
|
|
|
|
|
* @OA\Schema(
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="email",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* example={"email": "ilmedovamahri@gmail.com"}
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="OK"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
public function sendPasswordResetLinkEmail(Request $request) {
|
2022-07-07 06:13:12 +00:00
|
|
|
try{
|
|
|
|
|
$request->validate(['email' => 'required|email']);
|
2022-06-30 14:55:51 +00:00
|
|
|
|
2022-07-07 06:13:12 +00:00
|
|
|
$user = Client::where('email', $request->email)->first();
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'user with provided email not found'
|
2022-07-07 06:13:12 +00:00
|
|
|
], 404);
|
|
|
|
|
}
|
2022-06-30 14:55:51 +00:00
|
|
|
|
2022-07-07 06:13:12 +00:00
|
|
|
$token = rand(1000, 9999);
|
|
|
|
|
$user['token'] = $token;
|
|
|
|
|
$user->save();
|
2022-06-30 14:55:51 +00:00
|
|
|
|
2022-07-07 09:16:39 +00:00
|
|
|
Mail::to($request->email)->queue(new ResetPassword($user->firstname, $token));
|
2022-06-30 14:55:51 +00:00
|
|
|
|
2022-07-07 06:13:12 +00:00
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'sent reset code'
|
2022-07-07 06:13:12 +00:00
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
catch(\Exception $e){
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => $e->getMessage()
|
2022-07-07 06:13:12 +00:00
|
|
|
], 200);
|
|
|
|
|
}
|
2022-06-30 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-04 06:51:55 +00:00
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
* path="/api/reset-password",
|
|
|
|
|
* summary=" - Reset client password and enter new",
|
|
|
|
|
* tags = {"Authorization"},
|
|
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/json",
|
|
|
|
|
* @OA\Schema(
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="email",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="token",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="password",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="confirm_password",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* example={"email": "ilmedovamahri@gmail.com", "token":"2546", "password":"Hello001!", "confirm_password":"Hello001!"}
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="OK"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
2022-06-30 14:55:51 +00:00
|
|
|
public function updatePassword(Request $request) {
|
2022-07-07 06:13:12 +00:00
|
|
|
try{
|
|
|
|
|
$this->validate($request, [
|
|
|
|
|
'token' => 'required',
|
2022-07-07 07:52:29 +00:00
|
|
|
'email' => 'required|email',
|
2022-07-07 06:13:12 +00:00
|
|
|
'password' => 'required',
|
|
|
|
|
'confirm_password' => 'required|same:password'
|
|
|
|
|
]);
|
2022-07-07 07:18:57 +00:00
|
|
|
|
2022-07-07 06:13:12 +00:00
|
|
|
$user = Client::where('email', $request->email)->first();
|
|
|
|
|
if($user && $request->token == $user->token){
|
|
|
|
|
$user['password'] = Hash::make($request->password);
|
|
|
|
|
$user->save();
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'OK'
|
2022-07-07 06:13:12 +00:00
|
|
|
], 200);
|
|
|
|
|
}
|
2022-07-07 07:18:57 +00:00
|
|
|
|
2022-06-30 14:55:51 +00:00
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'not_found'
|
2022-07-07 06:13:12 +00:00
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
catch(\Exception $e){
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => $e->getMessage()
|
2022-07-07 06:13:12 +00:00
|
|
|
], 500);
|
2022-06-30 14:55:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 06:51:55 +00:00
|
|
|
/**
|
|
|
|
|
* @OA\POST(
|
|
|
|
|
* path="/api/update-account",
|
|
|
|
|
* summary=" - Update client account",
|
|
|
|
|
* tags = {"Authorization"},
|
2022-07-04 10:01:39 +00:00
|
|
|
* description = "All the fields are optional (if password field is being updated then confirm_password is required)",
|
2022-07-04 06:51:55 +00:00
|
|
|
* security={
|
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
|
* },
|
|
|
|
|
* @OA\RequestBody(
|
|
|
|
|
* @OA\MediaType(
|
|
|
|
|
* mediaType="application/json",
|
|
|
|
|
* @OA\Schema(
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="firstname",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="lastname",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="password",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Property(
|
|
|
|
|
* property="confirm_password",
|
|
|
|
|
* type="string",
|
|
|
|
|
* ),
|
|
|
|
|
* example={"firstname":"Mahri","lastname":"Ilmedova","email": "ilmedovamahri@gmail.com", "password":"Hello001!", "confirm_password":"Hello001!"}
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
* ),
|
|
|
|
|
* @OA\Response(
|
|
|
|
|
* response="200",
|
|
|
|
|
* description="OK"
|
|
|
|
|
* )
|
|
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
public function updateClient(Request $request){
|
2022-07-07 06:13:12 +00:00
|
|
|
try{
|
|
|
|
|
$user = $request->user();
|
|
|
|
|
$client = Client::find($user->id);
|
|
|
|
|
$data = $request->all();
|
|
|
|
|
if($client){
|
|
|
|
|
if(count($data) > 0){
|
|
|
|
|
if(isset($data['firstname'])){
|
|
|
|
|
$client['firstname'] = $data['firstname'];
|
|
|
|
|
}
|
|
|
|
|
if(isset($data['lastname'])){
|
|
|
|
|
$client['lastname'] = $data['lastname'];
|
|
|
|
|
}
|
|
|
|
|
if(isset($data['password'])){
|
2022-07-07 09:16:39 +00:00
|
|
|
$data = $request->all();
|
|
|
|
|
$rules= [
|
|
|
|
|
'confirm_password' => 'required|same:password',
|
|
|
|
|
];
|
|
|
|
|
$validator = Validator::make($data, $rules);
|
|
|
|
|
|
|
|
|
|
if($validator->fails()){
|
|
|
|
|
return response()->json(['message' => 'validation failed' ,'errors'=>$validator->errors()]);
|
2022-07-07 06:13:12 +00:00
|
|
|
}
|
2022-07-07 09:16:39 +00:00
|
|
|
$client['password'] = Hash::make($request->password);
|
2022-07-05 12:48:06 +00:00
|
|
|
}
|
2022-07-04 06:51:55 +00:00
|
|
|
}
|
2022-07-07 06:13:12 +00:00
|
|
|
$client->save();
|
|
|
|
|
return response()->json([
|
2022-07-07 07:18:57 +00:00
|
|
|
'data' => [
|
2022-07-07 06:13:12 +00:00
|
|
|
'client' => $client
|
|
|
|
|
]
|
|
|
|
|
], 200);
|
2022-07-04 06:51:55 +00:00
|
|
|
}
|
2022-07-07 06:13:12 +00:00
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'unauthorised'
|
2022-07-07 06:13:12 +00:00
|
|
|
], 401);
|
|
|
|
|
}
|
|
|
|
|
catch(\Exception $e){
|
|
|
|
|
return response()->json([
|
2022-07-07 09:16:39 +00:00
|
|
|
'message' => 'unauthorised'
|
2022-07-07 06:13:12 +00:00
|
|
|
], 401);
|
2022-07-04 06:51:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-24 11:56:01 +00:00
|
|
|
}
|