This commit is contained in:
Torsten 2024-11-23 14:01:36 +01:00
parent b42271a924
commit f417ac8e1e
11 changed files with 103 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 KiB

View file

@ -0,0 +1,35 @@
```bash
php spark make:controller Api/Auth --suffix --restful
php spark make:controller Api/Project --suffix --restful
```
![[Pasted image 20241123100630.png]]
![[Pasted image 20241123100706.png]]
![[Pasted image 20241123100734.png]]
![[Pasted image 20241123100756.png]]
--- PROJECT CONTROLLER
![[Pasted image 20241123100834.png]]
![[Pasted image 20241123100851.png]]
![[Pasted image 20241123100913.png]]
ADD SHIELD USERMODEL
![[Pasted image 20241123101019.png]]
```bash
php spark make:model Project --suffix
```
extend allowedFields, e.g.
```php
$allowedFields = ["user_id","name"]
```
Add Project Model to ProjectController
```php
use App\Models\ProjectModel;
```

View file

@ -0,0 +1,22 @@
https://www.youtube.com/watch?v=_ZRHt-fWJpc&list=PLT9miexWCpPW6Z_OXNv2d78cqCrZaPrVG&index=29
app/Config/Routes.php
Remember: Shield added =>
```php
service('auth')->routes($routes)
```
```php
// API routes (put below service('auth'))
$routes->group("api", ["namespace" => "App\Controller\Api"], function($routes) {
$routes->post("register", "AuthController::register");
$routes->post("login", "AuthController::login");
$routes->get("profile", "AuthController::profile");
$routes->get("logout", "AuthController::logout");
$routes->post("add-project", "ProjectController::addProject");
$routes->get("list-projects", "ProjectController::listProjects");
$routes->delete("delete-project/(:num)", "ProjectController::deleteProject/$1");
});
```

View file

@ -0,0 +1,46 @@
AuthController:
```php
use CodeIgniter\Restful\ResourceController;
use CodeIgniter\Shield\Models\UserModel;
use CodeIgniter\Shield\Entities\User;
class AuthController extends ResourceController
{
//Post
public function register()
{
$rules = [
"username" => "required|is_unique[users.username]",
"email" => "required|valid_email|is_unique[auth_identities.secret]",
"password" => "required"
];
if (!$this->validate($rules)) {
$response = [
"status" => false,
"message" => $this->validator->getErrors(),
"data" => []
];
} else {
$userObject = new UserModel(); // coming from Shield
$userEntityObject = new User([
"username" => $this->request->getVar("username"),
"email" => $this->request->getVar("email"),
"password" => $this->request->getVar("password")
]);
$userObject->save($userEntityObject);
$response = [
"status" => true,
"message" => "User saved successfully",
"data" => []
]
}
return $this->respondCreated($response);
}
}
```
Note: shield stores users in auth_identies. Email address should be stored in field secret.