birzha-legalizasia/app/Http/Controllers/AuthController.php

583 lines
20 KiB
PHP
Raw Normal View History

2022-06-24 11:56:01 +00:00
<?php
namespace App\Http\Controllers;
2022-07-07 14:19:10 +00:00
use App\Http\Requests\API\LoginRequest;
use App\Http\Requests\API\RegisterRequest;
use App\Http\Requests\API\UpdateClientRequest;
use App\Http\Resources\ClientResource;
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-07-08 11:05:21 +00:00
use App\Models\Account;
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;
2022-07-06 12:38:03 +00:00
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
2022-06-24 11:56:01 +00:00
2022-06-27 06:32:28 +00:00
/**
* @OA\Info(
* title="Legalization API",
2022-07-07 14:19:10 +00:00
* version="1.0.0"
2022-06-27 06:32:28 +00:00
* )
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-07-13 18:34:24 +00:00
//controller where all auth process for clie-nt happens
2022-06-24 11:56:01 +00:00
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-07-07 14:19:10 +00:00
*
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\Parameter(
* description="Localization",
* in="header",
* name="X-Localization",
* required=false,
* @OA\Schema(type="string"),
* @OA\Examples(example="ru", value="ru", summary="Russian localization"),
* @OA\Examples(example="en", value="en", summary="English localization"),
* @OA\Examples(example="tm", value="tm", summary="Turkmen localization"),
* ),
* @OA\Response(
2022-06-27 06:32:28 +00:00
* response="200",
2022-07-07 14:19:10 +00:00
* description="OK",
* @OA\JsonContent(type="object")
* ),
* @OA\Response(
2022-06-27 06:32:28 +00:00
* response="401",
2022-07-07 14:19:10 +00:00
* description="Unauthorized",
* @OA\JsonContent(type="object")
* )
2022-06-27 06:32:28 +00:00
* )
*/
2022-07-07 14:19:10 +00:00
public function login(LoginRequest $request){
2022-06-24 11:56:01 +00:00
2022-07-07 14:19:10 +00:00
$client = Client::where('email', $request->input('email'))->first();
2022-06-24 11:56:01 +00:00
2022-07-07 09:16:39 +00:00
if($client){
if($client->is_suspended){
return response()->json([
'message' => 'Account with this email is suspended'
], 403);
}
2022-07-07 09:16:39 +00:00
if (!Hash::check(request()->password, $client->password)){
return response()->json([
'message' => 'Unauthorized'
], 401);
2022-07-06 12:38:03 +00:00
}
2022-07-08 11:05:21 +00:00
elseif ($client->is_suspended){
return response()->json([
'message' => 'Account suspended'
], 401);
}
2022-07-07 09:16:39 +00:00
2022-07-13 10:38:12 +00:00
$credentials = $request->only('email', 'password');
Auth::attempt($credentials);
2022-07-07 14:19:10 +00:00
$client->tokens()->delete();
2022-07-08 09:40:16 +00:00
2022-07-07 14:19:10 +00:00
$client->token = $client->createToken('auth_token')->plainTextToken;
2022-07-07 09:16:39 +00:00
2022-07-07 14:19:10 +00:00
return ClientResource::make($client);
}
return response()->json(['message' => Lang::get('auth.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",
* ),
* @OA\Property(
* property="account_type",
* type="string",
* ),
* @OA\Property(
* property="country",
* type="integer",
* ),
* example={"firstname":"Mahri", "lastname":"Ilmedova" ,"email": "ilmedovamahri@gmail.com", "password": 12345678, "country": 1,"account_type":"business"}
2022-06-27 06:32:28 +00:00
* )
* )
* ),
* @OA\Parameter(
* description="Localization",
* in="header",
* name="X-Localization",
* required=false,
* @OA\Schema(type="string"),
* @OA\Examples(example="ru", value="ru", summary="Russian localization"),
* @OA\Examples(example="en", value="en", summary="English localization"),
* @OA\Examples(example="tm", value="tm", summary="Turkmen localization"),
* ),
2022-06-27 06:32:28 +00:00
* @OA\Response(
2022-07-07 14:19:10 +00:00
* response="201",
* description="OK",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="token", type="string"),
* @OA\Property(property="client", type="object",
* @OA\Property(property="id", type="integer"),
* @OA\Property(property="firstname", type="string"),
* @OA\Property(property="lastname", type="string"),
* @OA\Property(property="email", type="string"),
* @OA\Property(property="is_verified", type="boolean"),
* )
* )
2022-06-27 06:32:28 +00:00
* ),
* @OA\Response(
2022-07-07 14:19:10 +00:00
* response="422",
* description="Validation Error",
* @OA\JsonContent(type="object",
* @OA\Property(property="message", type="string"),
* @OA\Property(property="errors", type="object"),
* )
2022-06-27 06:32:28 +00:00
* )
* )
*/
public function register(RegisterRequest $request)
{
$client = new Client($request->only(['email','firstname','lastname']));
$client->password = Hash::make($request->input('password'));
2022-07-06 13:41:48 +00:00
$email_verification = (bool) config('settings.email_verification');
$client->is_verified = ! $email_verification;
2022-07-06 13:41:48 +00:00
if($email_verification)
{
$client->verification_token = rand(10000, 99999);
try{
Mail::to($request->email)
->queue(new EmailVerification($request->firstname, $client->verification_token));
}catch (\Exception $ex){
//eger email ugradyp bolmasa verification edip bolmaz
$client->is_verified = true;
}
2022-07-07 14:19:10 +00:00
}
2022-07-06 13:41:48 +00:00
$account = Account::create([
2022-07-13 18:34:24 +00:00
'country_id' => $request->country,
'legalization_number' => $request->legalization_number,
'type' => $request->account_type
2022-07-13 18:34:24 +00:00
]);
2022-07-06 13:41:48 +00:00
$client->account()->associate($account)->save();
2022-07-06 13:41:48 +00:00
if($client->is_verified){
2022-07-07 14:19:10 +00:00
Auth::login($client);
$client->token = $client->createToken('auth_token')->plainTextToken;
2022-06-27 06:32:28 +00:00
}
2022-07-07 14:19:10 +00:00
return ClientResource::make($client);
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\Parameter(
* description="Localization",
* in="header",
* name="X-Localization",
* required=false,
* @OA\Schema(type="string"),
* @OA\Examples(example="ru", value="ru", summary="Russian localization"),
* @OA\Examples(example="en", value="en", summary="English localization"),
* @OA\Examples(example="tm", value="tm", summary="Turkmen localization"),
* ),
2022-07-06 13:37:51 +00:00
* @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->verification_token === $request->token){
2022-07-08 11:05:51 +00:00
$client->is_verified = 1;
$client->save();
2022-07-07 07:48:12 +00:00
Auth::login($client);
2022-07-08 09:40:16 +00:00
2022-07-08 11:05:51 +00:00
$client->token = $client->createToken('auth_token')->plainTextToken;
2022-07-07 07:48:12 +00:00
2022-07-08 10:48:32 +00:00
return ClientResource::make($client);
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\Parameter(
* description="Localization",
* in="header",
* name="X-Localization",
* required=false,
* @OA\Schema(type="string"),
* @OA\Examples(example="ru", value="ru", summary="Russian localization"),
* @OA\Examples(example="en", value="en", summary="English localization"),
* @OA\Examples(example="tm", value="tm", summary="Turkmen localization"),
* ),
2022-06-30 14:55:51 +00:00
* @OA\Response(
* response="200",
* description="OK"
* ),
* @OA\Response(
* response="401",
* description="Unauthorized"
* )
* )
*/
public function client(Request $request) {
2022-07-07 14:19:10 +00:00
if($client = $request->user()){
return ClientResource::make($client);
2022-06-30 14:55:51 +00:00
}
2022-07-07 14:19:10 +00:00
return response()->json([
'message' => 'token_expired'
], 401);
2022-06-30 14:55:51 +00:00
}
/**
* @OA\POST(
* path="/api/logout",
* summary=" - Logout user",
* tags = {"Authorization"},
* security={
* {"bearerAuth": {}}
* },
* @OA\Parameter(
* description="Localization",
* in="header",
* name="X-Localization",
* required=false,
* @OA\Schema(type="string"),
* @OA\Examples(example="ru", value="ru", summary="Russian localization"),
* @OA\Examples(example="en", value="en", summary="English localization"),
* @OA\Examples(example="tm", value="tm", summary="Turkmen localization"),
* ),
2022-06-30 14:55:51 +00:00
* @OA\Response(
* response="200",
* description="OK"
* ),
* @OA\Response(
* response="401",
* description="Unauthorized"
* )
* )
*/
public function logout(Request $request) {
2022-07-07 14:19:10 +00:00
$request->user()->currentAccessToken()->delete();
return response()->json([
'message' => 'ok'
], 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\Parameter(
* description="Localization",
* in="header",
* name="X-Localization",
* required=false,
* @OA\Schema(type="string"),
* @OA\Examples(example="ru", value="ru", summary="Russian localization"),
* @OA\Examples(example="en", value="en", summary="English localization"),
* @OA\Examples(example="tm", value="tm", summary="Turkmen localization"),
* ),
2022-06-30 14:55:51 +00:00
* @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);
2022-07-08 09:40:20 +00:00
$user['verification_token'] = $token;
2022-07-07 06:13:12 +00:00
$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
}
/**
* @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\Parameter(
* description="Localization",
* in="header",
* name="X-Localization",
* required=false,
* @OA\Schema(type="string"),
* @OA\Examples(example="ru", value="ru", summary="Russian localization"),
* @OA\Examples(example="en", value="en", summary="English localization"),
* @OA\Examples(example="tm", value="tm", summary="Turkmen localization"),
* ),
* @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
}
}
/**
* @OA\POST(
2022-07-08 09:40:16 +00:00
* path="/api/update-client",
* summary=" - Update client account",
* tags = {"Authorization"},
2022-07-08 09:40:16 +00:00
* description = "Every field is required, except password. Password field is optional",
* 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",
* ),
* example={"firstname":"Mahri","lastname":"Ilmedova","password":"Hello001!"}
* )
* )
* ),
* @OA\Parameter(
* description="Localization",
* in="header",
* name="X-Localization",
* required=false,
* @OA\Schema(type="string"),
* @OA\Examples(example="ru", value="ru", summary="Russian localization"),
* @OA\Examples(example="en", value="en", summary="English localization"),
* @OA\Examples(example="tm", value="tm", summary="Turkmen localization"),
* ),
* @OA\Response(
* response="200",
* description="OK"
* )
* )
*/
2022-07-07 14:19:10 +00:00
public function updateClient(UpdateClientRequest $request){
2022-07-07 10:50:39 +00:00
$client = $request->user();
$data = $request->only('firstname', 'lastname', 'password');
2022-07-08 09:40:16 +00:00
if (!isset($data['password']) || !$data['password']) {
2022-07-07 10:50:39 +00:00
unset($data['password']);
2022-07-08 09:40:16 +00:00
}
else {
2022-07-07 10:50:39 +00:00
$data['password'] = bcrypt($data['password']);
}
2022-07-07 10:50:39 +00:00
2022-07-07 14:19:10 +00:00
if($client->fill($data)->save()){
return ClientResource::make($client);
}
2022-07-07 10:50:39 +00:00
return response()->json([
2022-07-07 14:19:10 +00:00
'message' => 'Your account has not been updated.',
],500);
}
2022-06-24 11:56:01 +00:00
}