Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysLees committed Sep 7, 2023
1 parent e448cc5 commit 45e4d74
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 14 deletions.
58 changes: 52 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ This package was developed to give you a quick start to authenticate in laravel.

## 💡 What is Laravel Auth?

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


## 🛠 Requirements

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

# ✅ This is recommended for production as well:
MICROSOFT_REDIRECT_URI="${APP_URL}"/auth/service/microsoft/redirect
MICROSOFT_REDIRECT_URI="${APP_URL}/auth/service/microsoft/redirect"
```

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

Update your `App\Http\Middleware\Authenticate` middleware:
```php
<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;

class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*/
protected function redirectTo(Request $request): ?string
{
return $request->expectsJson() ? null : route('auth.login');
}
}

```

Finally, run the following command:

```bash
Expand Down Expand Up @@ -118,7 +141,7 @@ class NovaServiceProvider extends NovaApplicationServiceProvider

Nova::userMenu(function (Request $request, Menu $menu) {
return $menu
->append(MenuItem::externalLink('Logout', '/auth/logout'));
->append(MenuItem::externalLink('Logout', route('auth.logout')));
});
```

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

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

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.
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.

```php
```diff
/**
* Register the Nova routes.
*
* @return void
*/
protected function routes()
{
Nova::routes();
- Nova::routes()
- ->withAuthenticationRoutes()
- ->withPasswordResetRoutes();
+ Nova::routes();

}
```

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

return [
/*
|--------------------------------------------------------------------------
| Redirect Settings
|--------------------------------------------------------------------------
| You may like to define a different route once the user is
| logged in or out. If no redirects are defined, the package will redirect to the
| intended route. (This is the normal Laravel behaviour)
|
| Use the route name as defined in your routes file.
|
| If password-reset is not defined, the package will redirect to the login redirect route.
|
*/
'redirect' => [
// 'login' => 'dashboard',
// 'logout' => '',
// 'password-reset' => '',
],

/*
|--------------------------------------------------------------------------
| Logo Settings
Expand Down
19 changes: 19 additions & 0 deletions config/laravel-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
// config for CodebarAg/LaravelAuth

return [
/*
|--------------------------------------------------------------------------
| Redirect Settings
|--------------------------------------------------------------------------
| You may like to define a different route once the user is
| logged in or out. If no redirects are defined, the package will redirect to the
| intended route. (This is the normal Laravel behaviour)
|
| Use the route name as defined in your routes file.
|
| If password-reset is not defined, the package will redirect to the login redirect route.
|
*/
'redirect' => [
// 'login' => 'dashboard',
// 'logout' => '',
// 'password-reset' => '',
],

/*
|--------------------------------------------------------------------------
| Logo Settings
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/authcss.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions resources/views/components/form/button/ahref.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@props(['href'])
@props(['href', 'attributes'])

<a
href="{{ $href }}"
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"
{{ $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']) }}
>
{{ $slot }}
</a>
4 changes: 3 additions & 1 deletion resources/views/components/logo.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<img src="{{ asset(config('laravel-auth.logo.path')) }}" alt="{{__('Logo')}}" {{ $attributes->merge(['class' => 'mx-auto w-1/2']) }}>
@if(config('laravel-auth.logo.path'))
<img src="{{ asset(config('laravel-auth.logo.path')) }}" alt="{{__('Logo')}}" {{ $attributes->merge(['class' => 'mx-auto w-[25%]']) }}>
@endif
5 changes: 4 additions & 1 deletion resources/views/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class="block text-sm text-gray-900 hover:text-gray-500 underline">{{ __('Forgot
{{ __('Sign In') }}
</x-auth::form.button.button>

<x-auth::form.button.ahref :href="route('auth.provider', \CodebarAg\LaravelAuth\Enums\ProviderEnum::MICROSOFT_OFFICE_365())">
<x-auth::form.button.ahref
:href="route('auth.provider', \CodebarAg\LaravelAuth\Enums\ProviderEnum::MICROSOFT_OFFICE_365())"
class="bg-gray-500"
>
<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>
<span>{{ __('Sign In with Microsoft') }}</span>
</x-auth::form.button.ahref>
Expand Down
6 changes: 5 additions & 1 deletion src/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function store(LoginRequest $loginRequest)

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

return redirect()->intended();
return redirect()->intended(
config('laravel-auth.redirect.login') ?
route(config('laravel-auth.redirect.login')) :
'/'
);
}
}
6 changes: 5 additions & 1 deletion src/Controllers/LogoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public function __invoke(Request $request): RedirectResponse
$request->session()->invalidate();
$request->session()->regenerateToken();

return redirect()->intended('/nova');
return redirect()->intended(
config('laravel-auth.redirect.logout') ?
route(config('laravel-auth.redirect.logout')) :
'/'
);
}
}
6 changes: 5 additions & 1 deletion src/Controllers/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function store(ResetPasswordRequest $request)

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

return redirect()->intended();
return redirect()->intended(
config('laravel-auth.redirect.password-reset') ?
route(config('laravel-auth.redirect.password-reset')) :
'/'
);
}
}

0 comments on commit 45e4d74

Please sign in to comment.