Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 45e4d74

Browse files
committed
WIP
1 parent e448cc5 commit 45e4d74

File tree

9 files changed

+96
-14
lines changed

9 files changed

+96
-14
lines changed

README.md

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ This package was developed to give you a quick start to authenticate in laravel.
99

1010
## 💡 What is Laravel Auth?
1111

12-
Laravel Auth is an opinionated way to authenticate in laravel.
12+
Laravel Auth is an internal Laravel Nova Authentication replacement to gain more control over authorizing into Laravel Nova.
13+
1314

1415
## 🛠 Requirements
1516

@@ -64,7 +65,7 @@ MICROSOFT_TENANT_ID=your-tenant-id
6465
APP_URL=https://your-expose-or-ngrok-url.com
6566

6667
# ✅ This is recommended for production as well:
67-
MICROSOFT_REDIRECT_URI="${APP_URL}"/auth/service/microsoft/redirect
68+
MICROSOFT_REDIRECT_URI="${APP_URL}/auth/service/microsoft/redirect"
6869
```
6970

7071
Add the following trait to your `User` model:
@@ -73,6 +74,28 @@ Add the following trait to your `User` model:
7374
use CodebarAg\LaravelAuth\Traits\HasAuthProviders;
7475
```
7576

77+
Update your `App\Http\Middleware\Authenticate` middleware:
78+
```php
79+
<?php
80+
81+
namespace App\Http\Middleware;
82+
83+
use Illuminate\Auth\Middleware\Authenticate as Middleware;
84+
use Illuminate\Http\Request;
85+
86+
class Authenticate extends Middleware
87+
{
88+
/**
89+
* Get the path the user should be redirected to when they are not authenticated.
90+
*/
91+
protected function redirectTo(Request $request): ?string
92+
{
93+
return $request->expectsJson() ? null : route('auth.login');
94+
}
95+
}
96+
97+
```
98+
7699
Finally, run the following command:
77100

78101
```bash
@@ -118,7 +141,7 @@ class NovaServiceProvider extends NovaApplicationServiceProvider
118141

119142
Nova::userMenu(function (Request $request, Menu $menu) {
120143
return $menu
121-
->append(MenuItem::externalLink('Logout', '/auth/logout'));
144+
->append(MenuItem::externalLink('Logout', route('auth.logout')));
122145
});
123146
```
124147

@@ -141,17 +164,21 @@ Next in your `nova.php` config add the following:
141164

142165
Next in your `NovaServiceProvider` replace the routes method with the following:
143166

144-
Note: you can not register routes for `->withAuthenticationRoutes()` or `->withPasswordResetRoutes()` as this will override the changes we made in the `nova.php` config to routes.
167+
Note: you can `not` register routes for `->withAuthenticationRoutes()` or `->withPasswordResetRoutes()` as this will override the changes we made in the `nova.php` config to routes.
145168

146-
```php
169+
```diff
147170
/**
148171
* Register the Nova routes.
149172
*
150173
* @return void
151174
*/
152175
protected function routes()
153176
{
154-
Nova::routes();
177+
- Nova::routes()
178+
- ->withAuthenticationRoutes()
179+
- ->withPasswordResetRoutes();
180+
+ Nova::routes();
181+
155182
}
156183
```
157184

@@ -171,6 +198,25 @@ This is the contents of the published config file:
171198
// config for CodebarAg/LaravelAuth
172199

173200
return [
201+
/*
202+
|--------------------------------------------------------------------------
203+
| Redirect Settings
204+
|--------------------------------------------------------------------------
205+
| You may like to define a different route once the user is
206+
| logged in or out. If no redirects are defined, the package will redirect to the
207+
| intended route. (This is the normal Laravel behaviour)
208+
|
209+
| Use the route name as defined in your routes file.
210+
|
211+
| If password-reset is not defined, the package will redirect to the login redirect route.
212+
|
213+
*/
214+
'redirect' => [
215+
// 'login' => 'dashboard',
216+
// 'logout' => '',
217+
// 'password-reset' => '',
218+
],
219+
174220
/*
175221
|--------------------------------------------------------------------------
176222
| Logo Settings

config/laravel-auth.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
// config for CodebarAg/LaravelAuth
44

55
return [
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Redirect Settings
9+
|--------------------------------------------------------------------------
10+
| You may like to define a different route once the user is
11+
| logged in or out. If no redirects are defined, the package will redirect to the
12+
| intended route. (This is the normal Laravel behaviour)
13+
|
14+
| Use the route name as defined in your routes file.
15+
|
16+
| If password-reset is not defined, the package will redirect to the login redirect route.
17+
|
18+
*/
19+
'redirect' => [
20+
// 'login' => 'dashboard',
21+
// 'logout' => '',
22+
// 'password-reset' => '',
23+
],
24+
625
/*
726
|--------------------------------------------------------------------------
827
| Logo Settings

resources/dist/authcss.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
@props(['href'])
1+
@props(['href', 'attributes'])
22

33
<a
44
href="{{ $href }}"
5-
class="inline-flex items-center justify-center gap-x-2 rounded-md bg-blue-500 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 w-full"
5+
{{ $attributes->merge(['class' => 'inline-flex items-center justify-center gap-x-2 rounded-md px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 w-full']) }}
66
>
77
{{ $slot }}
88
</a>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
<img src="{{ asset(config('laravel-auth.logo.path')) }}" alt="{{__('Logo')}}" {{ $attributes->merge(['class' => 'mx-auto w-1/2']) }}>
1+
@if(config('laravel-auth.logo.path'))
2+
<img src="{{ asset(config('laravel-auth.logo.path')) }}" alt="{{__('Logo')}}" {{ $attributes->merge(['class' => 'mx-auto w-[25%]']) }}>
3+
@endif

resources/views/login.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ class="block text-sm text-gray-900 hover:text-gray-500 underline">{{ __('Forgot
4242
{{ __('Sign In') }}
4343
</x-auth::form.button.button>
4444

45-
<x-auth::form.button.ahref :href="route('auth.provider', \CodebarAg\LaravelAuth\Enums\ProviderEnum::MICROSOFT_OFFICE_365())">
45+
<x-auth::form.button.ahref
46+
:href="route('auth.provider', \CodebarAg\LaravelAuth\Enums\ProviderEnum::MICROSOFT_OFFICE_365())"
47+
class="bg-gray-500"
48+
>
4649
<svg class="fill-white w-4 h-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 278050 333334" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" image-rendering="optimizeQuality" fill-rule="evenodd" clip-rule="evenodd"><path fill="currentColor" d="M278050 305556l-29-16V28627L178807 0 448 66971l-448 87 22 200227 60865-23821V80555l117920-28193-17 239519L122 267285l178668 65976v73l99231-27462v-316z"/></svg>
4750
<span>{{ __('Sign In with Microsoft') }}</span>
4851
</x-auth::form.button.ahref>

src/Controllers/LoginController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function store(LoginRequest $loginRequest)
2828

2929
$loginRequest->session()->regenerate();
3030

31-
return redirect()->intended();
31+
return redirect()->intended(
32+
config('laravel-auth.redirect.login') ?
33+
route(config('laravel-auth.redirect.login')) :
34+
'/'
35+
);
3236
}
3337
}

src/Controllers/LogoutController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public function __invoke(Request $request): RedirectResponse
1515
$request->session()->invalidate();
1616
$request->session()->regenerateToken();
1717

18-
return redirect()->intended('/nova');
18+
return redirect()->intended(
19+
config('laravel-auth.redirect.logout') ?
20+
route(config('laravel-auth.redirect.logout')) :
21+
'/'
22+
);
1923
}
2024
}

src/Controllers/ResetPasswordController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function store(ResetPasswordRequest $request)
6363

6464
flash(__('passwords.reset'), 'success');
6565

66-
return redirect()->intended();
66+
return redirect()->intended(
67+
config('laravel-auth.redirect.password-reset') ?
68+
route(config('laravel-auth.redirect.password-reset')) :
69+
'/'
70+
);
6771
}
6872
}

0 commit comments

Comments
 (0)