obsidian/MyObsidianVault/Knowledgebase/WebDev/2 CodeIgniter/API Project setup/02.) Create Auth Controller.md

139 lines
2.9 KiB
Markdown
Raw Normal View History

2024-12-08 09:19:03 +01:00
```bash
php spark make:controller Api/Auth --suffix --restful
```
Api/AuthController.php
```php
2024-12-14 09:51:39 +01:00
<?php
namespace App\Controllers\Api;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
2024-12-08 09:19:03 +01:00
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Shield\Entities\User;
class AuthController extends ResourceController
{
//Post
public function register()
{
$rules = [
2024-12-14 09:51:39 +01:00
"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
2024-12-08 09:19:03 +01:00
];
2024-12-14 09:51:39 +01:00
if (!$this->validate($rules))
{
2024-12-08 09:19:03 +01:00
return $this->respondCreated([
"status" => false,
2024-12-14 09:51:39 +01:00
"message" => $this->validator->getErrors(),
2024-12-08 09:19:03 +01:00
"data" => []
2024-12-14 09:51:39 +01:00
]);
}
2024-12-08 09:19:03 +01:00
$um = new UserModel(); // coming from Shield
$userEntity = new User([
"username" => $this->request->getVar("username"),
"email" => $this->request->getVar("email"),
"password" => $this->request->getVar("password")
]);
2024-12-14 09:51:39 +01:00
2024-12-08 09:19:03 +01:00
$um->save($userEntity);
2024-12-14 09:51:39 +01:00
2024-12-08 09:19:03 +01:00
return $this->respondCreated( [
"status" => true,
"message" => "User saved successfully",
"data" => []
]);
}
2024-12-14 09:51:39 +01:00
2024-12-08 09:19:03 +01:00
//////////////////////////////////////
// Post
public function login()
2024-12-14 09:51:39 +01:00
{
if(auth()->loggedIn()) {
2024-12-08 09:19:03 +01:00
auth()->logout();
}
$rules = [
2024-12-14 09:51:39 +01:00
"email" => "required|valid_email",
"password" => "required"
2024-12-08 09:19:03 +01:00
];
if (!$this->validate($rules)) {
return $this->respondCreated([
"status" => false,
"message" => $this->validator->getErrors(),
"data" => []
2024-12-14 09:51:39 +01:00
]);
}
2024-12-08 09:19:03 +01:00
// success
$credentials = [
2024-12-14 09:51:39 +01:00
"email" => $this->request->getVar("email"),
"password" => $this->request->getVar("password")
2024-12-08 09:19:03 +01:00
];
2024-12-14 09:51:39 +01:00
2024-12-08 09:19:03 +01:00
$loginAttempt = auth()->attempt($credentials);
2024-12-14 09:51:39 +01:00
2024-12-08 09:19:03 +01:00
if (!$loginAttempt->isOK()) {
return $this->respondCreated([
2024-12-14 09:51:39 +01:00
"status" => false,
"message" => "Invalid login details",
"data" => []
2024-12-08 09:19:03 +01:00
]);
2024-12-14 09:51:39 +01:00
}
2024-12-08 09:19:03 +01:00
// We have a valid data set
2024-12-14 09:51:39 +01:00
2024-12-08 09:19:03 +01:00
$um = new UserModel();
$userData = $um->findById(auth()->id());
$token = $userData->generateAccessToken("thisismysecretkey");
$auth_token = $token->raw_token; // Only available after generation
2024-12-14 09:51:39 +01:00
2024-12-08 09:19:03 +01:00
return $this->respondCreated( [
2024-12-14 09:51:39 +01:00
"status" => true,
"message" => "User logged in successfully",
"data" => [
2024-12-08 09:19:03 +01:00
"token" => $auth_token
2024-12-14 09:51:39 +01:00
]
]);
2024-12-08 09:19:03 +01:00
}
2024-12-14 09:51:39 +01:00
2024-12-08 09:19:03 +01:00
////////////////////////
2024-12-14 09:51:39 +01:00
public function logout()
{
2024-12-08 09:19:03 +01:00
auth()->logout();
auth()->user()->revokeAllAccessTokens();
2024-12-14 09:51:39 +01:00
return $this->respondCreated([
"status" => true,
"message" => "User logged out successfully",
"data" => []
]);
}
2024-12-08 09:19:03 +01:00
2024-12-14 09:51:39 +01:00
//Get
public function profile()
{
$userId = auth()->id();
2024-12-08 09:19:03 +01:00
return $this->respondCreated([
2024-12-14 09:51:39 +01:00
"status" => true,
"message" => "Profile api called",
"data" => []
]);
}
public function accessDenied()
{
return $this->respondCreated([
"status" => false,
"message" => "Invalid access",
2024-12-08 09:19:03 +01:00
"data" => []
]);
}
}
```