...
This commit is contained in:
parent
58deb877e4
commit
a9e7afd13a
2 changed files with 99 additions and 1 deletions
|
|
@ -45,4 +45,50 @@ class AuthController extends ResourceController
|
||||||
|
|
||||||
Note: shield stores users in auth_identies. Email address should be stored in field secret.
|
Note: shield stores users in auth_identies. Email address should be stored in field secret.
|
||||||
|
|
||||||
https://onlinewebtutorblog.com/codeigniter-4-restful-api-using-shield-authentication/
|
https://onlinewebtutorblog.com/codeigniter-4-restful-api-using-shield-authentication/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
My version:
|
||||||
|
```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" => []
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
@ -72,3 +72,55 @@ In AuthController::login:
|
||||||
|
|
||||||
```
|
```
|
||||||
https://shield.codeigniter.com/references/authentication/tokens/
|
https://shield.codeigniter.com/references/authentication/tokens/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
My version:
|
||||||
|
```php
|
||||||
|
// Post
|
||||||
|
public function login()
|
||||||
|
{
|
||||||
|
if(auth()->loggedIn()){
|
||||||
|
auth()->logout();
|
||||||
|
}
|
||||||
|
|
||||||
|
$rules = [
|
||||||
|
"email" => "required|valid_email",
|
||||||
|
"password" => "required"
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!$this->validate($rules)) {
|
||||||
|
return $this->respondCreated([
|
||||||
|
"status" => false,
|
||||||
|
"message" => $this->validator->getErrors(),
|
||||||
|
"data" => []
|
||||||
|
]);
|
||||||
|
|
||||||
|
// success
|
||||||
|
$credentials = [
|
||||||
|
"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" => []
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 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" => [
|
||||||
|
"token" => $auth_token
|
||||||
|
] );
|
||||||
|
}
|
||||||
|
```
|
||||||
Loading…
Add table
Add a link
Reference in a new issue