Skip to content

Commit d0e81a1

Browse files
committed
udpate
1 parent b5e300d commit d0e81a1

File tree

1 file changed

+106
-1
lines changed

1 file changed

+106
-1
lines changed

README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,109 @@ https://firebase-php.readthedocs.io/en/stable/setup.html
66

77
- [x] Inject Configured Firebase Object to container.
88
- [x] Laravel Auth with Firebase uid.
9-
- [x] Laravel Auth Mock for unit test.
9+
- [x] Laravel Auth Mock for unit test.
10+
11+
## Injected Firebase Object
12+
13+
`laravel-firebase` use `kreait/firebase-php`
14+
15+
https://github.com/kreait/firebase-php
16+
17+
You can get `Kreait\Firebase` Object from DI Container.
18+
19+
You will put your firebase config on your `config/services.php`.
20+
21+
```php
22+
<?php
23+
return [
24+
// ...
25+
'firebase' => [
26+
"type"=> "service_account",
27+
"project_id"=> env("FIREBASE_PROJECTID"),
28+
"private_key_id"=> env("FIREBASE_PRIVATEID"),
29+
"private_key"=> str_replace("\\n","\n",env("FIREBASE_PRIVATEKEY")),
30+
"client_email"=> env("FIREBASE_CLIENTEMAIL"),
31+
"client_id"=> env("FIREBASE_CLIENTID"),
32+
"auth_uri"=> "https://accounts.google.com/o/oauth2/auth",
33+
"token_uri"=> "https://oauth2.googleapis.com/token",
34+
"auth_provider_x509_cert_url"=> "https://www.googleapis.com/oauth2/v1/certs",
35+
"client_x509_cert_url"=> env("FIREBASE_X509CERTURL")
36+
],
37+
];
38+
```
39+
40+
containered object also has alias name `firebase`. so you can recieve firebase object also like this.
41+
42+
```php
43+
$firebase = app("firebase");
44+
```
45+
46+
## FirebaseAuth
47+
48+
`laravel-firebase` provide Request Guard that is named `firebase_idtoken`.
49+
50+
With this guard, REST API will be written like below.
51+
52+
```php
53+
Route::group([
54+
"middleware" => [
55+
"auth:firebase_idtoken",
56+
]
57+
],function(){
58+
Route::post('/user', function(){
59+
$user = Auth::guard("firebase_idtoken")->user();
60+
//...
61+
});
62+
});
63+
```
64+
65+
In this guard, `Auth::guard("firebase_idtoken")` will return `\Chatbox\Larabase\FirebaseUser`
66+
that is subclass `\Kreait\Firebase\Auth\UserRecord` and implementation for `Illuminate\Auth\Authenticatable`.
67+
68+
## FirebaseAuth Mock
69+
70+
`TestFirebaseUser` create mock firebase user.
71+
72+
With this, you can test your REST API without firebase setting.
73+
74+
```php
75+
<?php
76+
77+
namespace Tests\Feature;
78+
79+
use Chatbox\Larabase\Testing\TestFirebaseUser;
80+
use Illuminate\Support\Str;
81+
use Tests\TestCase;
82+
83+
class ProjectIdTest extends TestCase
84+
{
85+
86+
protected function setUp(): void
87+
{
88+
parent::setUp();
89+
if(!TestFirebaseUser::users()){
90+
$token = Str::random();
91+
$user = TestFirebaseUser::fake()->recordAs($token);
92+
// Store firebase user to database or ...
93+
$this->withHeader("Authorization", "Bearer $token");
94+
}else{
95+
TestFirebaseUser::remenber(function($token, $user){
96+
$this->withHeader("Authorization", "Bearer $token");
97+
});
98+
}
99+
config()->set("app.url","http://localhost/api/");
100+
}
101+
}
102+
```
103+
104+
`TestFirebaseUser::fake()` create firebase dummy user.
105+
Method `recordAs` will store the user with uid(you generate it!).
106+
107+
`TestFirebaseUser::users()` will return all stored users,
108+
and `TestFirebaseUser::remenber` will pop out each stored user to passed closure.
109+
110+
111+
112+
113+
114+

0 commit comments

Comments
 (0)