...
This commit is contained in:
parent
afe2245110
commit
d69b379f97
4 changed files with 158 additions and 0 deletions
BIN
MyObsidianVault/Pasted image 20241129092837.png
Normal file
BIN
MyObsidianVault/Pasted image 20241129092837.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 150 KiB |
|
|
@ -0,0 +1,91 @@
|
|||
https://www.youtube.com/watch?v=f0NifEgwh30&list=PLT9miexWCpPW6Z_OXNv2d78cqCrZaPrVG&index=33
|
||||
|
||||
|
||||
```php
|
||||
auth()->user()
|
||||
```
|
||||
|
||||
|
||||
```php
|
||||
$userId = auth()->id();
|
||||
```
|
||||
|
||||
![[Pasted image 20241129092837.png]]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
AuthController
|
||||
```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 {
|
||||
|
||||
// to test: directly getting $userObject via auth()->user()
|
||||
|
||||
// We have a valid data set
|
||||
$userObject = new UserModel();
|
||||
|
||||
$userData = $userObject->findById(auth()->id());
|
||||
|
||||
$token = $userData->generateAccessToken("thisismysecretkey");
|
||||
|
||||
$auth_token = $token->raw_token;
|
||||
|
||||
$response = [
|
||||
"status" => true,
|
||||
"message" => "User logged in successfully",
|
||||
"data" => [
|
||||
"token" => $auth_token
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->respondCreated($response);
|
||||
}
|
||||
|
||||
//...
|
||||
```
|
||||
21
MyObsidianVault/WebDev/2 CodeIgniter/21.) User Logout API.md
Normal file
21
MyObsidianVault/WebDev/2 CodeIgniter/21.) User Logout API.md
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
https://www.youtube.com/watch?v=H1KiXEsmsCs&list=PLT9miexWCpPW6Z_OXNv2d78cqCrZaPrVG&index=34
|
||||
|
||||
Routes.php:
|
||||
```php
|
||||
//Get
|
||||
$routes->get("logout", "AuthController::logout", ["filter" => "apiauth"]);
|
||||
```
|
||||
|
||||
AuthController.php
|
||||
```php
|
||||
public function logout() {
|
||||
auth()->logout();
|
||||
auth()->user()->revokeAllAccessTokens();
|
||||
|
||||
return $this->respondCreated([
|
||||
"status" => true,
|
||||
"message" => "User logged out successfully",
|
||||
"data" => []
|
||||
]);
|
||||
}
|
||||
```
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
https://www.youtube.com/watch?v=104Q76g7DWA&list=PLT9miexWCpPW6Z_OXNv2d78cqCrZaPrVG&index=36
|
||||
|
||||
ProjectController
|
||||
```php
|
||||
// Post
|
||||
public function addProject()
|
||||
{
|
||||
$rules = [
|
||||
"name" => "required",
|
||||
"budget" => "required"
|
||||
];
|
||||
|
||||
if(!$this->validate($rules)) {
|
||||
$response = [
|
||||
"status" => false,
|
||||
"message" => $this->validator->getErrors(),
|
||||
"data" => []
|
||||
];
|
||||
}else {
|
||||
$user_id = auth()-id();
|
||||
$name = $this->request->getVar("name");
|
||||
$budget = $this->request->getVar("budget");
|
||||
$pm = new ProjectModel();
|
||||
$data= [
|
||||
"user_id" => $user_id,
|
||||
"name" => name,
|
||||
"budget" => budget
|
||||
];
|
||||
if($pm->insert($data)) {
|
||||
$response = [
|
||||
"status" => true,
|
||||
"message" => "project saved",
|
||||
"data" => []
|
||||
];
|
||||
}else{
|
||||
$response = [
|
||||
"status" => false,
|
||||
"message" => "project save failed",
|
||||
"data" => []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$return $this->respondCreated($response);
|
||||
}
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue