...
This commit is contained in:
parent
f417ac8e1e
commit
9f78249324
4 changed files with 79 additions and 3 deletions
BIN
MyObsidianVault/Pasted image 20241126102145.png
Normal file
BIN
MyObsidianVault/Pasted image 20241126102145.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 217 KiB |
BIN
MyObsidianVault/Pasted image 20241126102214.png
Normal file
BIN
MyObsidianVault/Pasted image 20241126102214.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 111 KiB |
|
|
@ -14,8 +14,8 @@ class AuthController extends ResourceController
|
||||||
{
|
{
|
||||||
$rules = [
|
$rules = [
|
||||||
"username" => "required|is_unique[users.username]",
|
"username" => "required|is_unique[users.username]",
|
||||||
"email" => "required|valid_email|is_unique[auth_identities.secret]",
|
"email" => "required|valid_email|is_unique[auth_identities.secret]", // gets written to secret field in user table
|
||||||
"password" => "required"
|
"password" => "required" // gets written to secret 2 field in user table
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!$this->validate($rules)) {
|
if (!$this->validate($rules)) {
|
||||||
|
|
@ -43,4 +43,6 @@ 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/
|
||||||
74
MyObsidianVault/WebDev/2 CodeIgniter/18.) Create login.md
Normal file
74
MyObsidianVault/WebDev/2 CodeIgniter/18.) Create login.md
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
https://www.youtube.com/watch?v=XJHFtfQQmRs&list=PLT9miexWCpPW6Z_OXNv2d78cqCrZaPrVG&index=31
|
||||||
|
|
||||||
|
https://onlinewebtutorblog.com/codeigniter-4-restful-api-using-shield-authentication/
|
||||||
|
|
||||||
|
In AuthController::login:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```php
|
||||||
|
|
||||||
|
// Post
|
||||||
|
public function login()
|
||||||
|
{
|
||||||
|
|
||||||
|
if(auth()->loggedIn()){
|
||||||
|
auth()->logout();
|
||||||
|
}
|
||||||
|
|
||||||
|
$rules = [
|
||||||
|
"email" => "required|valid_email",
|
||||||
|
"password" => "required"
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!$this->validate($rules)) {
|
||||||
|
|
||||||
|
$response = [
|
||||||
|
"status" => false,
|
||||||
|
"message" => $this->validator->getErrors(),
|
||||||
|
"data" => []
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// success
|
||||||
|
$credentials = [
|
||||||
|
"email" => $this->request->getVar("email"),
|
||||||
|
"password" => $this->request->getVar("password")
|
||||||
|
];
|
||||||
|
|
||||||
|
$loginAttempt = auth()->attempt($credentials);
|
||||||
|
|
||||||
|
if (!$loginAttempt->isOK()) {
|
||||||
|
|
||||||
|
$response = [
|
||||||
|
"status" => false,
|
||||||
|
"message" => "Invalid login details",
|
||||||
|
"data" => []
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// We have a valid data set
|
||||||
|
$userObject = new UserModel();
|
||||||
|
|
||||||
|
$userData = $userObject->findById(auth()->id());
|
||||||
|
|
||||||
|
$token = $userData->generateAccessToken("thisismysecretkey");
|
||||||
|
|
||||||
|
$auth_token = $token->raw_token; // Only available after generation
|
||||||
|
|
||||||
|
$response = [
|
||||||
|
"status" => true,
|
||||||
|
"message" => "User logged in successfully",
|
||||||
|
"data" => [
|
||||||
|
"token" => $auth_token
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondCreated($response);
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
https://shield.codeigniter.com/references/authentication/tokens/
|
||||||
Loading…
Add table
Add a link
Reference in a new issue