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

472 lines
15 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-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;
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-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-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\Response(
* response="200",
2022-07-07 14:19:10 +00:00
* description="OK",
* @OA\JsonContent(type="object")
2022-06-27 06:32:28 +00:00
* ),
* @OA\Response(
* 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 (!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);
2022-07-07 14:19:10 +00:00
$client->tokens()->delete();
$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);
}
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 14:19:10 +00:00
*
* example={"firstname":"Mahri", "lastname":"Ilmedova" ,"email": "ilmedovamahri@gmail.com", "password": 12345678}
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
* )
* )
*/
2022-07-07 14:19:10 +00:00
public function register(RegisterRequest $request)
{
2022-07-06 13:41:48 +00:00
2022-07-07 14:19:10 +00:00
$data = $request->only(['email','firstname','lastname']);
2022-07-06 13:41:48 +00:00
2022-07-07 14:19:10 +00:00
$data['password'] = Hash::make($request->input('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 14:19:10 +00:00
2022-07-07 07:39:35 +00:00
$data['is_verified'] = $email_verification;
2022-07-06 13:41:48 +00:00
2022-07-08 09:40:20 +00:00
$data['verification_token'] = rand(1000, 9999);//generate code;
2022-07-06 13:41:48 +00:00
2022-07-07 14:19:10 +00:00
// $data['status'] = 0 ;
2022-07-06 13:41:48 +00:00
2022-07-07 14:19:10 +00:00
$client = Client::create($data);
2022-07-06 13:41:48 +00:00
2022-07-07 14:19:10 +00:00
if($email_verification)
{
Mail::to($request->email)->queue(new EmailVerification($request->firstname, $client->token));
2022-07-06 13:41:48 +00:00
}
2022-07-07 14:19:10 +00:00
else{
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\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 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\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();
//$request->user->tokens()->delete(); // use this to revoke all tokens (logout from all devices)
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\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\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(
* path="/api/update-account",
* summary=" - Update client account",
* tags = {"Authorization"},
* description = "All the fields are optional (if password field is being updated then confirm_password is required)",
* 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"
* )
* )
*/
2022-07-07 10:50:39 +00:00
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');
if (! isset($data['password']) || ! $data['password']) {
unset($data['password']);
} else {
$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
}