Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Laravel Passwordless Authentication
Norby Baruani
Norby Baruani

Posted on

     

Laravel Passwordless Authentication

Passwordless authentication is a method whereby users access an app without entering passwords. It is the most effective way to reduce risky password management practices and prevent credential theft attacks.

Image description

Above is an architecture diagram of a Passwordless authentication flow.

we will be using this laravel packagelaravel-passwordless-authentication to implement a passwordless authentication by sending a magic link to the user's email address to authenticate them.

Install

Setup new Laravel application

composer create-project laravel/laravel passwordless-app
Enter fullscreen modeExit fullscreen mode

InstallLaravel Breeze to scaffold quick UI

composer require laravel/breeze--devphp artisan breeze:installphp artisan migratenpminstallnpm run dev
Enter fullscreen modeExit fullscreen mode

Install passwordless package and follow instruction to setup package.

composer require norbybaru/passwordless-authphp artisan vendor:publish--provider="NorbyBaru\Passwordless\PasswordlessServiceProvider"--tag="passwordless-config"php artisan vendor:publish--provider="NorbyBaru\Passwordless\PasswordlessServiceProvider"--tag="passwordless-migrations"php artisan migrate
Enter fullscreen modeExit fullscreen mode

1. Mail driver Setup

Setup mail driver withmailtrap.io. Copy below values into your .env and replaceMAIL_USERNAME andMAIL_PASSWORD with your correct credentials frommailtrap.io.

MAIL_MAILER=smtpMAIL_HOST=smtp.mailtrap.ioMAIL_PORT=2525MAIL_USERNAME=MAIL_PASSWORD=MAIL_ENCRYPTION=tlsMAIL_FROM_ADDRESS=support@example.testMAIL_FROM_NAME="${APP_NAME}"
Enter fullscreen modeExit fullscreen mode

2. User Model Setup

Setup User model to work withlaravel-passwordless-authentication package by extendingCanUsePasswordlessAuthenticatable::class and implementingPasswordlessAuthenticatable::class on the model.

<?phpnamespaceApp\Models;useIlluminate\Database\Eloquent\Factories\HasFactory;useIlluminate\Foundation\Auth\UserasAuthenticatable;useIlluminate\Notifications\Notifiable;useLaravel\Sanctum\HasApiTokens;useNorbyBaru\Passwordless\CanUsePasswordlessAuthenticatable;useNorbyBaru\Passwordless\Traits\PasswordlessAuthenticatable;classUserextendsAuthenticatableimplementsCanUsePasswordlessAuthenticatable{useHasApiTokens,HasFactory,Notifiable,PasswordlessAuthenticatable;...}
Enter fullscreen modeExit fullscreen mode

3. Login Form

Update login form to capture only email address of user as an identifier to send magic link to login.

Image description

4. Login Route

Update login route inroutes/auth.php to require an email address and uselaravel-passwordless-authentication package to send magic link token.

Route::post('login',function(Request$request){$validated=$request->validate(['email'=>'required|email|exists:users|max:255',]);$status=Passwordless::magicLink()->sendLink($validated);returnredirect()->back()->with(['status'=>trans($status)]);});
Enter fullscreen modeExit fullscreen mode

5. Update Translation

Add filepasswordless.php under translation directorylang/en/passwordless.php with the following values to show correct message back to user depending on response status from sending magic link to user.

<?phpreturn['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.',];
Enter fullscreen modeExit fullscreen mode

Final Steps

Start your application and make sure to create or seed some dummy user to test login flow with them.

Image description

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Cape Town, South Africa
  • Pronouns
    He/Him
  • Work
    Software Developer Engineer
  • Joined

More fromNorby Baruani

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp