Skip to content

Commit 6a8a7dc

Browse files
authored
Merge pull request #422 from codeigniter4/mobile-token-guides
docs: mobile token guides
2 parents c293ab3 + 718a9e7 commit 6a8a7dc

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

docs/guides/mobile_apps.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Mobile Authentication with Access Tokens
2+
3+
Access Tokens can be used to authenticate mobile applications that are consuming your API. This is similar to how you would work with [third-party users](api_tokens.md) of your API, but with small differences in how you would issue the tokens.
4+
5+
## Issuing the Tokens
6+
7+
Typically, a mobile application would issue a request from their login screen, passing in the credentials to authenticate with. Once authenticated you would return the `raw token` within the response and that would be saved on the device to use in following API calls.
8+
9+
Start by creating a route that would handle the request from the login screen on the mobile device. The device name can be any arbitrary string, but is typically used to identify the device the request is being made from, like "Johns iPhone 13".
10+
11+
```php
12+
13+
// Routes.php
14+
$route->post('auth/token', 'App\Controllers\Auth\LoginController::mobileLogin');
15+
16+
// LoginController.php
17+
namespace App\Controllers\Auth;
18+
19+
use CodeIgniter\Controllers\BaseController;
20+
21+
class LoginController extends BaseController
22+
{
23+
public function mobileLogin()
24+
{
25+
// Validate credentials
26+
$rules = setting('Validation.login') ?? [
27+
'email' => [
28+
'label' => 'Auth.email',
29+
'rules' => config('AuthSession')->emailValidationRules,
30+
],
31+
'password' => [
32+
'label' => 'Auth.password',
33+
'rules' => 'required',
34+
],
35+
];
36+
37+
if (! $this->validate($rules)) {
38+
return $this->response
39+
->setJSON(['errors' => $this->validator->getErrors()])
40+
->setStatusCode(422)
41+
}
42+
43+
// Attempt to login
44+
$result = auth()->attempt($this->request->getPost(setting('Auth.validFields')));
45+
if (! $result->isOK()) {
46+
return $this->response
47+
->setJSON(['error' => $result->reason])
48+
->setStatusCode(401);
49+
}
50+
51+
// Generate token and return to client
52+
$token = auth()->user()->generateAccessToken(service('request')->getVar('device_name'));
53+
54+
return $this->response
55+
->setJSON(['token' => $token->raw_token]);
56+
}
57+
}
58+
```
59+
60+
When making all future requests to the API, the mobile client should return the raw token in the `Authorization` header as a `Bearer` token.

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
Guides:
1414
* [Protecting an API with Access Tokens](guides/api_tokens.md)
15+
* [Mobile Authentication with Access Tokens](guides/mobile_apps.md)

0 commit comments

Comments
 (0)