...
This commit is contained in:
parent
ba3de1ae99
commit
4d205a337b
8 changed files with 96 additions and 102 deletions
|
|
@ -12,7 +12,12 @@ $routes->get("logout", "AuthController::logout", ["filter" => "apiauth"]);
|
|||
|
||||
Api/AuthController.php
|
||||
```php
|
||||
use CodeIgniter\Restful\ResourceController;
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Api;
|
||||
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use CodeIgniter\RESTful\ResourceController;
|
||||
use CodeIgniter\Shield\Models\UserModel;
|
||||
use CodeIgniter\Shield\Entities\User;
|
||||
|
||||
|
|
@ -22,46 +27,47 @@ class AuthController extends ResourceController
|
|||
public function register()
|
||||
{
|
||||
$rules = [
|
||||
"username" => "required|is_unique[users.username]",
|
||||
"email" => "required|valid_email|is_unique[auth_identities.secret]", // gets written to secret field in user table
|
||||
"password" => "required" // gets written to secret 2 field in user table
|
||||
"username" => "required|is_unique[users.username]",
|
||||
"email" => "required|valid_email|is_unique[auth_identities.secret]", // gets written to secret field in user table
|
||||
"password" => "required" // gets written to secret 2 field in user table
|
||||
];
|
||||
|
||||
if (!$this->validate($rules)) {
|
||||
|
||||
if (!$this->validate($rules))
|
||||
{
|
||||
return $this->respondCreated([
|
||||
"status" => false,
|
||||
"message" => $this->validator->getErrors(),
|
||||
"message" => $this->validator->getErrors(),
|
||||
"data" => []
|
||||
]
|
||||
);
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
$um = new UserModel(); // coming from Shield
|
||||
$userEntity = new User([
|
||||
"username" => $this->request->getVar("username"),
|
||||
"email" => $this->request->getVar("email"),
|
||||
"password" => $this->request->getVar("password")
|
||||
]);
|
||||
|
||||
$um->save($userEntity);
|
||||
|
||||
|
||||
return $this->respondCreated( [
|
||||
"status" => true,
|
||||
"message" => "User saved successfully",
|
||||
"data" => []
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
// Post
|
||||
public function login()
|
||||
{
|
||||
if(auth()->loggedIn()){
|
||||
{
|
||||
if(auth()->loggedIn()) {
|
||||
auth()->logout();
|
||||
}
|
||||
|
||||
$rules = [
|
||||
"email" => "required|valid_email",
|
||||
"password" => "required"
|
||||
"email" => "required|valid_email",
|
||||
"password" => "required"
|
||||
];
|
||||
|
||||
if (!$this->validate($rules)) {
|
||||
|
|
@ -69,45 +75,70 @@ class AuthController extends ResourceController
|
|||
"status" => false,
|
||||
"message" => $this->validator->getErrors(),
|
||||
"data" => []
|
||||
]);
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
// success
|
||||
$credentials = [
|
||||
"email" => $this->request->getVar("email"),
|
||||
"password" => $this->request->getVar("password")
|
||||
"email" => $this->request->getVar("email"),
|
||||
"password" => $this->request->getVar("password")
|
||||
];
|
||||
|
||||
|
||||
$loginAttempt = auth()->attempt($credentials);
|
||||
|
||||
if (!$loginAttempt->isOK()) {
|
||||
return $this->respondCreated([
|
||||
"status" => false,
|
||||
"message" => "Invalid login details",
|
||||
"data" => []
|
||||
"status" => false,
|
||||
"message" => "Invalid login details",
|
||||
"data" => []
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
// We have a valid data set
|
||||
|
||||
$um = new UserModel();
|
||||
$userData = $um->findById(auth()->id());
|
||||
$token = $userData->generateAccessToken("thisismysecretkey");
|
||||
$auth_token = $token->raw_token; // Only available after generation
|
||||
|
||||
|
||||
return $this->respondCreated( [
|
||||
"status" => true,
|
||||
"message" => "User logged in successfully",
|
||||
"data" => [
|
||||
"status" => true,
|
||||
"message" => "User logged in successfully",
|
||||
"data" => [
|
||||
"token" => $auth_token
|
||||
] );
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////
|
||||
|
||||
public function logout() {
|
||||
|
||||
public function logout()
|
||||
{
|
||||
auth()->logout();
|
||||
auth()->user()->revokeAllAccessTokens();
|
||||
|
||||
return $this->respondCreated([
|
||||
"status" => true,
|
||||
"message" => "User logged out successfully",
|
||||
"status" => true,
|
||||
"message" => "User logged out successfully",
|
||||
"data" => []
|
||||
]);
|
||||
}
|
||||
|
||||
//Get
|
||||
public function profile()
|
||||
{
|
||||
$userId = auth()->id();
|
||||
return $this->respondCreated([
|
||||
"status" => true,
|
||||
"message" => "Profile api called",
|
||||
"data" => []
|
||||
]);
|
||||
}
|
||||
|
||||
public function accessDenied()
|
||||
{
|
||||
return $this->respondCreated([
|
||||
"status" => false,
|
||||
"message" => "Invalid access",
|
||||
"data" => []
|
||||
]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue