Skip to content

Commit d3336da

Browse files
authored
Merge pull request #4 from firevel/feature/web-middleware
Cookie middleware
2 parents e53dbf6 + 772333a commit d3336da

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ The driver contains a firebase guard that authenticates user by Firebase Authent
1313
composer require firevel/firebase-authentication
1414
```
1515

16-
2) Update config/auth.php with:
16+
2) Update config/auth.php.
17+
1718
```
1819
'guards' => [
19-
....
20-
'api' => [
20+
'web' => [
2121
'driver' => 'firebase',
2222
'provider' => 'users',
2323
],
24+
25+
'api' => [
26+
'driver' => 'token',
27+
'provider' => 'users',
28+
],
2429
],
2530
```
31+
2632
3) Update your User model with `Firevel\FirebaseAuthentication\FirebaseAuthenticable` trait `$incrementing = false` and fillables.
2733

2834
Eloquent example:
@@ -37,7 +43,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
3743
3844
class User extends Authenticatable
3945
{
40-
use Notifiable;
46+
use Notifiable, FirebaseAuthenticable;
4147
4248
/**
4349
* Indicates if the IDs are auto-incrementing.
@@ -101,6 +107,23 @@ $table->string('picture');
101107
$table->timestamps();
102108
```
103109

110+
## Web guard
111+
112+
In order to use firebase authentication in web routes you must attach bearer token to each http request.
113+
114+
You can also store bearer token in `bearer_token` cookie variable and add to your `Kernel.php`:
115+
```
116+
protected $middlewareGroups = [
117+
'web' => [
118+
...
119+
\Firevel\FirebaseAuthentication\Http\Middleware\AddAccessTokenFromCookie::class,
120+
...
121+
],
122+
123+
...
124+
];
125+
```
126+
104127
## Usage
105128

106129
Attach to each API call regular bearer token provided by Firebase Authentication.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Firevel\FirebaseAuthentication\Http\Middleware;
4+
5+
use Closure;
6+
7+
class AddAccessTokenFromCookie
8+
{
9+
/**
10+
* Store token from cookie in authorization header.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
*
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next)
18+
{
19+
if (empty($request->bearerToken())) {
20+
$tokenCookie = config('firebase.token_cookie', 'bearer_token');
21+
if ($request->hasCookie($tokenCookie)) {
22+
$token = $request->cookie($tokenCookie);
23+
$request->headers->add(['Authorization' => 'Bearer '.$token]);
24+
}
25+
}
26+
27+
return $next($request);
28+
}
29+
}

0 commit comments

Comments
 (0)