...
This commit is contained in:
parent
ba3de1ae99
commit
4d205a337b
8 changed files with 96 additions and 102 deletions
|
|
@ -60,7 +60,7 @@ Access mysql from inside container
|
||||||
|
|
||||||
mysql -h 127.0.0.1 -u root
|
mysql -h 127.0.0.1 -u root
|
||||||
|
|
||||||
CREATE DATABASE TEST_DB;
|
CREATE DATABASE godotweb;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,28 @@ ln -s myprojectapp/public htdocs
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Setup db settings in .env:
|
||||||
|
```txt
|
||||||
|
CI_ENVIRONMENT = development
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------
|
||||||
|
# APP
|
||||||
|
#--------------------------------------------------------------------
|
||||||
|
app.baseURL = 'http://godotweb.local'
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------
|
||||||
|
# DATABASE
|
||||||
|
#--------------------------------------------------------------------
|
||||||
|
|
||||||
|
database.default.hostname = godotweb.local
|
||||||
|
database.default.database = godotweb
|
||||||
|
database.default.username = root
|
||||||
|
database.default.password =
|
||||||
|
database.default.DBDriver = MySQLi
|
||||||
|
database.default.DBPrefix =
|
||||||
|
database.default.port = 3306
|
||||||
|
```
|
||||||
|
|
||||||
Add shield:
|
Add shield:
|
||||||
```bash
|
```bash
|
||||||
composer require codeigniter4/shield
|
composer require codeigniter4/shield
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,12 @@ $routes->get("logout", "AuthController::logout", ["filter" => "apiauth"]);
|
||||||
|
|
||||||
Api/AuthController.php
|
Api/AuthController.php
|
||||||
```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\Models\UserModel;
|
||||||
use CodeIgniter\Shield\Entities\User;
|
use CodeIgniter\Shield\Entities\User;
|
||||||
|
|
||||||
|
|
@ -22,18 +27,19 @@ class AuthController extends ResourceController
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$rules = [
|
$rules = [
|
||||||
"username" => "required|is_unique[users.username]",
|
"username" => "required|is_unique[users.username]",
|
||||||
"email" => "required|valid_email|is_unique[auth_identities.secret]", // gets written to secret field in user table
|
"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
|
"password" => "required" // gets written to secret 2 field in user table
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!$this->validate($rules)) {
|
if (!$this->validate($rules))
|
||||||
|
{
|
||||||
return $this->respondCreated([
|
return $this->respondCreated([
|
||||||
"status" => false,
|
"status" => false,
|
||||||
"message" => $this->validator->getErrors(),
|
"message" => $this->validator->getErrors(),
|
||||||
"data" => []
|
"data" => []
|
||||||
]
|
]);
|
||||||
);
|
}
|
||||||
|
|
||||||
$um = new UserModel(); // coming from Shield
|
$um = new UserModel(); // coming from Shield
|
||||||
$userEntity = new User([
|
$userEntity = new User([
|
||||||
|
|
@ -41,6 +47,7 @@ class AuthController extends ResourceController
|
||||||
"email" => $this->request->getVar("email"),
|
"email" => $this->request->getVar("email"),
|
||||||
"password" => $this->request->getVar("password")
|
"password" => $this->request->getVar("password")
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$um->save($userEntity);
|
$um->save($userEntity);
|
||||||
|
|
||||||
return $this->respondCreated( [
|
return $this->respondCreated( [
|
||||||
|
|
@ -51,17 +58,16 @@ class AuthController extends ResourceController
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
||||||
// Post
|
// Post
|
||||||
public function login()
|
public function login()
|
||||||
{
|
{
|
||||||
if(auth()->loggedIn()){
|
if(auth()->loggedIn()) {
|
||||||
auth()->logout();
|
auth()->logout();
|
||||||
}
|
}
|
||||||
|
|
||||||
$rules = [
|
$rules = [
|
||||||
"email" => "required|valid_email",
|
"email" => "required|valid_email",
|
||||||
"password" => "required"
|
"password" => "required"
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!$this->validate($rules)) {
|
if (!$this->validate($rules)) {
|
||||||
|
|
@ -69,45 +75,70 @@ class AuthController extends ResourceController
|
||||||
"status" => false,
|
"status" => false,
|
||||||
"message" => $this->validator->getErrors(),
|
"message" => $this->validator->getErrors(),
|
||||||
"data" => []
|
"data" => []
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
// success
|
// success
|
||||||
$credentials = [
|
$credentials = [
|
||||||
"email" => $this->request->getVar("email"),
|
"email" => $this->request->getVar("email"),
|
||||||
"password" => $this->request->getVar("password")
|
"password" => $this->request->getVar("password")
|
||||||
];
|
];
|
||||||
|
|
||||||
$loginAttempt = auth()->attempt($credentials);
|
$loginAttempt = auth()->attempt($credentials);
|
||||||
|
|
||||||
if (!$loginAttempt->isOK()) {
|
if (!$loginAttempt->isOK()) {
|
||||||
return $this->respondCreated([
|
return $this->respondCreated([
|
||||||
"status" => false,
|
"status" => false,
|
||||||
"message" => "Invalid login details",
|
"message" => "Invalid login details",
|
||||||
"data" => []
|
"data" => []
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
// We have a valid data set
|
// We have a valid data set
|
||||||
|
|
||||||
$um = new UserModel();
|
$um = new UserModel();
|
||||||
$userData = $um->findById(auth()->id());
|
$userData = $um->findById(auth()->id());
|
||||||
$token = $userData->generateAccessToken("thisismysecretkey");
|
$token = $userData->generateAccessToken("thisismysecretkey");
|
||||||
$auth_token = $token->raw_token; // Only available after generation
|
$auth_token = $token->raw_token; // Only available after generation
|
||||||
|
|
||||||
return $this->respondCreated( [
|
return $this->respondCreated( [
|
||||||
"status" => true,
|
"status" => true,
|
||||||
"message" => "User logged in successfully",
|
"message" => "User logged in successfully",
|
||||||
"data" => [
|
"data" => [
|
||||||
"token" => $auth_token
|
"token" => $auth_token
|
||||||
] );
|
]
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////
|
////////////////////////
|
||||||
|
|
||||||
public function logout() {
|
public function logout()
|
||||||
|
{
|
||||||
auth()->logout();
|
auth()->logout();
|
||||||
auth()->user()->revokeAllAccessTokens();
|
auth()->user()->revokeAllAccessTokens();
|
||||||
|
return $this->respondCreated([
|
||||||
|
"status" => true,
|
||||||
|
"message" => "User logged out successfully",
|
||||||
|
"data" => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get
|
||||||
|
public function profile()
|
||||||
|
{
|
||||||
|
$userId = auth()->id();
|
||||||
return $this->respondCreated([
|
return $this->respondCreated([
|
||||||
"status" => true,
|
"status" => true,
|
||||||
"message" => "User logged out successfully",
|
"message" => "Profile api called",
|
||||||
|
"data" => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function accessDenied()
|
||||||
|
{
|
||||||
|
return $this->respondCreated([
|
||||||
|
"status" => false,
|
||||||
|
"message" => "Invalid access",
|
||||||
"data" => []
|
"data" => []
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,52 +20,3 @@ $routes->group("api", ["namespace" => "App\Controller\Api"], function($routes) {
|
||||||
//$routes->delete("delete-project/(:num)", "ProjectController::deleteProject/$1");
|
//$routes->delete("delete-project/(:num)", "ProjectController::deleteProject/$1");
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
$routes->get("logout", "AuthController::logout", ["filter" => "apiauth"]);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```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]", // gets written to secret field in user table
|
|
||||||
"password" => "required" // gets written to secret 2 field in user table
|
|
||||||
];
|
|
||||||
|
|
||||||
if (!$this->validate($rules)) {
|
|
||||||
return $this->respondCreated([
|
|
||||||
"status" => false,
|
|
||||||
"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" => []
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
@ -17,7 +17,6 @@ public function before(RequestInterface $request, $arguments = null)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Routes.php
|
Routes.php
|
||||||
```php
|
```php
|
||||||
// API ROUTES
|
// API ROUTES
|
||||||
|
|
@ -28,20 +27,6 @@ $routes->group("api",["namespace]=> "App\Controllers\Api"], function($routes) {
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
Define accessDenied method in app/Controllers/Api/AuthController.php
|
|
||||||
```php
|
|
||||||
...
|
|
||||||
public function accessDenied()
|
|
||||||
{
|
|
||||||
return $this->respondCreated([
|
|
||||||
"status" => false,
|
|
||||||
"message" => "Invalid access",
|
|
||||||
"data" => []
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
Add filter to app/Config/Filters.php
|
Add filter to app/Config/Filters.php
|
||||||
```php
|
```php
|
||||||
use App\Filters\AuthFilter;
|
use App\Filters\AuthFilter;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
![[Pasted image 20241214094809.png]]
|
||||||
|
|
||||||
|
![[Pasted image 20241214094906.png]]
|
||||||
|
600933326ac42a7f1669bfcaceb4bf1f973c8f64f54dccff9d6c424a9c2c0e5d
|
||||||
|
|
||||||
BIN
MyObsidianVault/_images/Pasted image 20241214094809.png
Normal file
BIN
MyObsidianVault/_images/Pasted image 20241214094809.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
BIN
MyObsidianVault/_images/Pasted image 20241214094906.png
Normal file
BIN
MyObsidianVault/_images/Pasted image 20241214094906.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 85 KiB |
Loading…
Add table
Add a link
Reference in a new issue