bombenprodukt/laravel-passage

Passage by 1Password for Laravel


README

This project was created by, and is maintained by Brian Faust, and is a package to integrate Passage by 1Password with Laravel. Be sure to browse through the changelog, code of conduct, contribution guidelines, license, and security policy.

Installation

Note This package requires PHP 8.2 or later, and it supports Laravel 10 or later.

To get the latest version, simply require the project using Composer:

$ composer require bombenprodukt/laravel-passage

You can publish the migrations by using:

$ php artisan vendor:publish --tag="laravel-passage-migrations"

You can publish the configuration file by using:

$ php artisan vendor:publish --tag="laravel-passage-config"

Usage

Please review the contents of our test suite for detailed usage examples.

Examples

Authentication with Magic Link

<?php

declare(strict_types=1);

use App\Models\User;
use BombenProdukt\Passage\Facades\Passage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::post('/passage/login', function (Request $request): void {
    Passage::authentication()->magicLink()->login($request->get('email'));

    $request->session()->flash('status', 'We have e-mailed your magic link!');

    return redirect()->back();
});

Route::post('/passage/register', function (Request $request): void {
    Passage::authentication()->magicLink()->register($request->get('email'));

    User::create(['email' => $request->get('email')]);

    $request->session()->flash('status', 'We have e-mailed your magic link!');

    return redirect()->back();
});

Route::get('/passage/{YOUR_APP_ID}', function (Request $request) {
    $response = Passage::authentication()->magicLink()->activate($request->query('psg_magic_link'));
    $currentUser = Passage::authentication()->authenticatedUsers($response['auth_token'])->currentUser();

    Auth::login(User::where('email', $currentUser['email'])->firstOrFail());

    $request->session()->flash('status', 'You have been logged in!');

    return redirect($response['redirect_url']);
});

Route::get('/passage/{YOUR_APP_ID}/dashboard', function (Request $request): void {
    return view('dashboard');
});