Skip to content

Commit 6ce777d

Browse files
Merge pull request #2 from rootinc/handlecallback-login-route
v0.4.0
2 parents 5355af2 + 4cc7895 commit 6ce777d

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

README.md

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ Route::get('/login/azurecallback', '\RootInc\LaravelAzureMiddleware\Azure@azurec
1313

1414
3. In our `App\Http\Kernel.php` add `'azure' => \RootInc\LaravelAzureMiddleware\Azure::class,` most likely to the `$routeMiddleware` array.
1515
4. In our `.env` add `TENANT_ID, CLIENT_ID, CLIENT_SECRET and RESOURCE`. We can get these values/read more here: https://portal.azure.com/
16-
5. Add the `azure` middleware to your route groups (or wherever) and enjoy :tada:
17-
6. If you need custom callbacks, see #Extended Installation.
16+
5. Add the `azure` middleware to your route groups on any routes that needs protected by auth and enjoy :tada:
17+
6. If you need custom callbacks, see [Extended Installation](#extended-installation).
1818

1919
## Routing
2020

2121
`Route::get('/login/azure', '\RootInc\LaravelAzureMiddleware\Azure@azure');` First parameter can be wherever you want to route the azure login. Change as you would like.
2222

2323
`Route::get('/login/azurecallback', '\RootInc\LaravelAzureMiddleware\Azure@azurecallback');` First parameter can be whatever you want to route after your callback. Change as you would like.
2424

25-
## Front End
25+
### Front End
2626

2727
It's best to have an Office 365 button on our login webpage that routes to `/login/azure` (or whatever you renamed it to). This can be as simple as an anchor tag like this `<a href="/login/azure" class="officeButton"></a>`
2828

2929
## Extended Installation
3030

31-
The out-of-the-box implementation let's you login users. However, let's say we would like to store this user into a database. There are two callbacks that are recommended to extend from the Azure class called `success` and `fail`. The following provides information on how to extend the Root Laravel Azure Middleware Library:
31+
The out-of-the-box implementation let's you login users. However, let's say we would like to store this user into a database There are two callbacks that are recommended to extend from the Azure class called `success` and `fail`. The following provides information on how to extend the Root Laravel Azure Middleware Library:
3232

33-
1. To get started (assuming we've followed the #Normal Installation directions), create a file called `AppAzure.php` in the `App\Http\Middleware` folder. You can either do this through `artisan` or manually.
33+
1. To get started (assuming we've followed the [Normal Installation](#normal-installation) directions), create a file called `AppAzure.php` in the `App\Http\Middleware` folder. You can either do this through `artisan` or manually.
3434
2. Add this as a starting point in this file:
3535

3636
```php
@@ -71,9 +71,67 @@ Route::get('/login/azurecallback', '\App\Http\Middleware\AppAzure@azurecallback'
7171

7272
4. Finally, update `Kernel.php`'s `azure` key to be `'azure' => \App\Http\Middleware\AppAzure::class,`
7373

74+
### Callback on Every Handshake
75+
76+
As of v0.4.0, we added a callback after every successful handle (handshake). The default is to simply call the `$next` closure. However, let's say we want to store a Singleton of a user. Here's an example of how to go about that:
77+
78+
```php
79+
<?php
80+
81+
namespace App\Http\Middleware;
82+
83+
use Closure;
84+
85+
use RootInc\LaravelAzureMiddleware\Azure as Azure;
86+
87+
use App\User;
88+
89+
class AppAzure extends Azure
90+
{
91+
protected function handlecallback($request, Closure $next, $access_token, $refresh_token)
92+
{
93+
$user_id = $request->session()->get('user_id');
94+
95+
if ($user_id)
96+
{
97+
$user = User::find($user_id);
98+
99+
\App::singleton('user', function() use($user){
100+
return $user;
101+
});
102+
}
103+
104+
return parent::handlecallback($request, $next, $access_token, $refresh_token);
105+
}
106+
}
107+
```
108+
109+
Building off of our previous example from [Extended Installation](#extended-installation), we have a `user_id` set in the session. We can use this id to query against the user model. Once we have the user model, we can setup the singleton to return the user. The callback should call the closure, `$next($request);` and return it. In our case, the default implementation redirects to `/`, so we call the parent here.
110+
111+
#### Different Login Route
112+
113+
As of v0.4.0, we added the ability to change the `$login_route` in the middelware. Building off [Extended Installation](#extended-installation), in our `AppAzure` class, we can simply set `$login_route` to whatever. For example:
114+
115+
```php
116+
<?php
117+
118+
namespace App\Http\Middleware;
119+
120+
use RootInc\LaravelAzureMiddleware\Azure as Azure;
121+
122+
class AppAzure extends Azure
123+
{
124+
protected $login_route = "/";
125+
}
126+
```
127+
128+
The above would now set `$login_route` to `/` or root.
129+
74130
## Contributing
75131

76-
TODO
132+
Thank you for considering contributing to the Laravel Azure Middleware! To encourage active collaboration, we encourage pull requests, not just issues.
133+
134+
If you file an issue, the issue should contain a title and a clear description of the issue. You should also include as much relevant information as possible and a code sample that demonstrates the issue. The goal of a issue is to make it easy for yourself - and others - to replicate the bug and develop a fix.
77135

78136
## License
79137

src/Azure.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class Azure
1313
{
14+
protected $login_route = "/login";
15+
1416
protected $baseUrl = "https://login.microsoftonline.com/";
1517
protected $route = "/oauth2/";
1618

@@ -27,9 +29,14 @@ public function handle($request, Closure $next)
2729
$access_token = $request->session()->get('_rootinc_azure_access_token');
2830
$refresh_token = $request->session()->get('_rootinc_azure_refresh_token');
2931

32+
if (env("APP_ENV") === "testing")
33+
{
34+
return $this->handlecallback($request, $next, $access_token, $refresh_token);
35+
}
36+
3037
if (!$access_token || !$refresh_token)
3138
{
32-
return redirect("/login");
39+
return redirect($this->$login_route);
3340
}
3441

3542
$client = new Client();
@@ -52,7 +59,7 @@ public function handle($request, Closure $next)
5259
$request->session()->put('_rootinc_azure_access_token', $contents->access_token);
5360
$request->session()->put('_rootinc_azure_refresh_token', $contents->refresh_token);
5461

55-
return $next($request);
62+
return $this->handlecallback($request, $next, $access_token, $refresh_token);
5663
}
5764

5865
public function azure(Request $request)
@@ -100,4 +107,9 @@ protected function fail(Request $request, RequestExcpetion $e)
100107
{
101108
return implode("", explode(PHP_EOL,$e->getMessage()));
102109
}
110+
111+
protected function handlecallback($request, Closure $next, $access_token, $refresh_token)
112+
{
113+
return $next($request);
114+
}
103115
}

0 commit comments

Comments
 (0)