Skip to content

Commit 957c992

Browse files
Update README.md
1 parent d82f368 commit 957c992

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,75 @@
22

33
Provides Azure Authentication Middleware for a Laravel App.
44

5-
## Installation
5+
## Normal Installation
66

77
1. `composer require rootinc/laravel-azure-middleware`
8-
2. In our routes folder (most likely `web.php`), add ```Route::get('/login/azure', '\RootInc\LaravelAzureMiddleware\Azure@azure');Route::get('/login/azurecallback', '\RootInc\LaravelAzureMiddleware\Azure@azurecallback');```
8+
2. In our routes folder (most likely `web.php`), add
9+
```php
10+
Route::get('/login/azure', '\RootInc\LaravelAzureMiddleware\Azure@azure');
11+
Route::get('/login/azurecallback', '\RootInc\LaravelAzureMiddleware\Azure@azurecallback');
12+
```
13+
914
3. In our `App\Http\Kernel.php` add `'azure' => \RootInc\LaravelAzureMiddleware\Azure::class,` most likely to the `$routeMiddleware` array.
1015
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/
1116
5. Add the `azure` middleware to your route groups (or wherever) and enjoy :tada:
17+
6. If you need custom callbacks, see #Extended Installation.
1218

1319
## Routing
20+
1421
`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.
22+
1523
`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.
1624

1725
## Front End
1826

1927
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>`
2028

29+
## Extended Installation
30+
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:
32+
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.
34+
2. Add this as a starting point in this file:
35+
36+
```php
37+
<?php
38+
39+
namespace App\Http\Middleware;
40+
41+
use RootInc\LaravelAzureMiddleware\Azure as Azure;
42+
43+
use App\User;
44+
45+
class AppAzure extends Azure
46+
{
47+
protected function success($request, $access_token, $refresh_token, $profile)
48+
{
49+
$email = strtolower($profile->unique_name);
50+
51+
$user = User::updateOrCreate(['email' => $email], [
52+
'firstName' => $profile->given_name,
53+
'lastName' => $profile->family_name
54+
]);
55+
56+
$request->session()->put('user_id', $user->id);
57+
58+
return parent::success($request, $access_token, $refresh_token, $profile);
59+
}
60+
}
61+
```
62+
63+
The above gives us a way to add/update users after a successful handshake.  `$profile` contains all sorts of metadata that we use to create or update our user. More information here: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code#jwt-token-claims . The default implementation redirects to `/`, so we call the parent here. Feel free to not extend the default and to redirect elsewhere.
64+
65+
3. Our routes need to be updated to the following:
66+
67+
```php
68+
Route::get('/login/azure', '\App\Http\Middleware\AppAzure@azure');
69+
Route::get('/login/azurecallback', '\App\Http\Middleware\AppAzure@azurecallback');
70+
```
71+
72+
4. Finally, update `Kernel.php`'s `azure` key to be `'azure' => \App\Http\Middleware\AppAzure::class,`
73+
2174
## Contributing
2275

2376
TODO

0 commit comments

Comments
 (0)