Skip to content

Commit

Permalink
update readme and add login route name config (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru authored Sep 9, 2022
1 parent c6d0b9f commit 9249337
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
46 changes: 41 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![Run Unit Tests](https://github.com/norbybaru/laravel-passwordless-authentication/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/norbybaru/laravel-passwordless-authentication/actions/workflows/run-tests.yml) [![PHPStan](https://github.com/norbybaru/laravel-passwordless-authentication/actions/workflows/phpstan.yml/badge.svg?branch=main)](https://github.com/norbybaru/laravel-passwordless-authentication/actions/workflows/phpstan.yml) [![Laravel Pint](https://github.com/norbybaru/laravel-passwordless-authentication/actions/workflows/pint.yml/badge.svg?branch=main)](https://github.com/norbybaru/laravel-passwordless-authentication/actions/workflows/pint.yml)

![PASSWORDLESS-AUTH](./loginlink.png)
# LARAVEL PASSWORDLESS AUTHENTICATION
Laravel Passwordless Authentication with Magic Link.

Expand Down Expand Up @@ -53,14 +56,22 @@ class User extends Authenticatable implements CanUsePasswordlessAuthenticatable
```

## Preparing `config/passwordless.php`
Open `config/passwordless.php` file and update `default_redirect_route` to the correct route name the user should land by default once authenticated in case you have different route name than `home`.

Open config file `config/passwordless.php`
- Update `default_redirect_route` to the correct route name the user should land by default once authenticated in case you have different route name than `home`.
eg.
```
'default_redirect_route' => 'dashboard',
```

- Update `login_route` to the correct route name of your login page to allow redirecting user
back to that page on invalid magic link.
eg.
```
'login_route' => 'auth.login',
```

## Setup Login Routes
Make sure to setup new login routes and update your application to use the new login route

```php
<?php
Expand All @@ -72,16 +83,41 @@ Route::post('login', function (Request $request) {
'email' => 'required|email|exists:users',
]);

$message = Passwordless::magicLink()->sendLink($validated);
$status = Passwordless::magicLink()->sendLink($validated);

return redirect()->back()->with([
'status' => $message
'status' => trans($message)
]);
});

```

## Setup Mail provider
## Setup Mail Provider
Make sure to have your application mail provider setup and working 100% for your Laravel application
```
MAIL_MAILER=
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME="${APP_NAME}"
```

## Setup Translations
Add file `passwordless.php` in your translations directory and copy the entry below.
Feel free to update text to suit your application needs.

```php
return [
'sent' => 'Login link sent to inbox.',
'throttled' => 'Login link was already sent. Please check your inbox or try again later.',
'invalid_token' => 'Invalid link supplied. Please request new one.',
'invalid_user' => 'Invalid user info supplied.',
'verified' => 'Login successful.',
];
```

## Setup Auth Provider

Expand Down
11 changes: 11 additions & 0 deletions config/passwordless.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
*/
'default_redirect_route' => 'home',

/*
|--------------------------------------------------------------------------
| Login route name
|--------------------------------------------------------------------------
|
| Set current login route name of your application to give the package ability to redirect to the page
| whenever an issue occurred with magic link validation
|
*/
'login_route' => 'login',

/*
|--------------------------------------------------------------------------
| Auth Provider
Expand Down
Binary file added loginlink.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions src/Traits/PasswordlessAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function loginByEmail(Request $request): RedirectResponse|Response
]);
}

return redirect()->to($this->redirectRoute($request))
return redirect()->to($this->redirectRoute($request, false))
->withInput($request->only('email'))
->withErrors(['email' => trans($response)]);
}
Expand All @@ -46,7 +46,7 @@ public function loginByEmail(Request $request): RedirectResponse|Response
: redirect()->intended($this->redirectRoute($request));
}

protected function redirectRoute(Request $request): string
protected function redirectRoute(Request $request, bool $success = true): string
{
if ($request->get('redirect_to')) {
return $request->get('redirect_to');
Expand All @@ -56,7 +56,13 @@ protected function redirectRoute(Request $request): string
return $this->redirectTo();
}

return route(config('passwordless.default_redirect_route'));
$routeName = config('passwordless.default_redirect_route');

if (! $success) {
$routeName = config('passwordless.login_route');
}

return route($routeName);
}

protected function verifyMagicLink(Request $request): string|Authenticatable|CanUsePasswordlessAuthenticatable
Expand Down

0 comments on commit 9249337

Please sign in to comment.