-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from simi41098/Api
Api's for login, change password and sign up
- Loading branch information
Showing
9 changed files
with
228 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\API\Auth; | ||
|
||
use App\Models\User; | ||
use Laravel\Passport\Http\Controllers\AccessTokenController as ATC; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class AccessTokenController extends ATC | ||
{ | ||
/** | ||
* Check username and password and redirect to dashboard also update user device | ||
* @param ServerRequestInterface $request | ||
* @return \Illuminate\Http\Response|never | ||
*/ | ||
public function issueToken(ServerRequestInterface $request) { | ||
$requestUsername = request()->input('username'); | ||
$user = User::where('email', $requestUsername)->orWhere('mobile', $requestUsername)->first(); | ||
if(!$user) return abort(422, "The credentials are incorrect"); | ||
|
||
return parent::issueToken($request); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/Http/Controllers/API/Auth/ChangePasswordController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\API\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\Api\ChangePasswordRequest; | ||
use App\Models\User; | ||
use App\MyClasses\ApiHelpers; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Hash; | ||
|
||
class ChangePasswordController extends Controller{ | ||
|
||
/** | ||
* Changes the user password | ||
* | ||
* @param ChangePasswordRequest $request | ||
* @return JsonResponse | ||
* @throws \Throwable | ||
*/ | ||
public function changePassword(ChangePasswordRequest $request){ | ||
|
||
$user = Auth::user(); | ||
|
||
if (! Hash::check($request->input('current_password'), $user->password)) { | ||
return ApiHelpers::response('Current Password is incorrect', [], 422); | ||
} | ||
$user->update(['password' => Hash::make($request->input('new_password'))]); | ||
return ApiHelpers::response('Password has been updated successfully!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\API\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\Api\RegistrationRequest; | ||
use App\Http\Resources\UserResource; | ||
use App\Models\User; | ||
use App\MyClasses\ApiHelpers; | ||
use App\Repositories\Admin\UserRepository; | ||
use Carbon\Carbon; | ||
use Illuminate\Auth\Events\Registered; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Support\Facades\DB; | ||
use Response; | ||
|
||
class RegistrationController extends Controller { | ||
|
||
public function __construct(UserRepository $userRepo) { | ||
$this->userRepository = $userRepo; | ||
} | ||
|
||
/** | ||
* Register api | ||
* | ||
* @param RegistrationRequest $request | ||
* @return JsonResponse | ||
* @throws \Throwable | ||
*/ | ||
public function register(RegistrationRequest $request) { | ||
DB::beginTransaction(); | ||
|
||
$user = User::create($request->validated()); | ||
$user->assignRole('Super Admin'); | ||
$this->userRepository->updateOrCreate_avatar($user, $request); | ||
$objToken = $user->createToken($user->name); | ||
$strToken = $objToken->accessToken; | ||
$expiration = $objToken->token->expires_at->diffInSeconds(Carbon::now()); | ||
|
||
DB::commit(); | ||
|
||
event(new Registered($user)); | ||
|
||
return ApiHelpers::response('User Registered Registered Successfully try to login', [ | ||
'user' => new UserResource($user), | ||
'token' => [ | ||
'access_token' => $strToken, | ||
'expires_in' => $expiration, | ||
'token_type' => 'Bearer', | ||
] | ||
]); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class ChangePasswordRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'current_password' => 'required', | ||
'new_password' => 'required', | ||
'confirm_password' => 'required|same:new_password' | ||
]; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
use App\Http\Requests\BaseRequest; | ||
use App\Models\User; | ||
use App\Repositories\Admin\UserRepository; | ||
|
||
class RegistrationRequest extends BaseRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function rules() | ||
{ | ||
$rules = User::$rules; | ||
unset($rules['role']); | ||
unset($rules['role.*']); | ||
return $rules; | ||
} | ||
|
||
protected function prepareForValidation() | ||
{ | ||
$this->merge(UserRepository::requestHandler($this)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use App\Models\User; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use JsonSerializable; | ||
|
||
/** | ||
* Class StickerResource | ||
* @mixin User | ||
* @package App\Http\Resources | ||
*/ | ||
class UserResource extends JsonResource { | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param Request $request | ||
* @return array|Arrayable|JsonSerializable | ||
*/ | ||
public function toArray($request) { | ||
return array_merge($this->only([ | ||
'name', | ||
'email', | ||
'mobile', | ||
'uuid', | ||
]), [ | ||
'roles' => $this->getRoleNames()->join(', '), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\MyClasses; | ||
|
||
use Illuminate\Http\Response; | ||
|
||
class ApiHelpers { | ||
|
||
/** | ||
* @param string $message | ||
* @param array $payload | ||
* @param int $status | ||
* @return \Illuminate\Http\JsonResponse | ||
* @throws \Throwable | ||
*/ | ||
public static function response(string $message, array $payload = [], int $status = Response::HTTP_OK) { | ||
return response()->json(['message' => $message, 'payload' => $payload, 'datetime' => now()->toDateTimeString(),], $status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters