Created
March 29, 2023 12:33
-
-
Save martinbean/0d1041d84ad61f44b98fe8e80e61edb7 to your computer and use it in GitHub Desktop.
Basic Laravel-based invitation system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
class Invitation extends Model | |
{ | |
protected $fillable = [ | |
'token', | |
'email', | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Http\Requests\Invitations\Acceptance\StoreAcceptanceRequest; | |
use App\Models\Invitation; | |
use App\Models\User; | |
use App\Providers\RouteServiceProvider; | |
use Illuminate\Auth\Events\Registered; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Hash; | |
class InvitationAcceptanceController extends Controller | |
{ | |
public function __construct() | |
{ | |
$this->middleware('signed'); | |
} | |
public function create(Invitation $invitation) | |
{ | |
return view('invitation.acceptance.create')->with([ | |
'invitation' => $invitation, | |
]); | |
} | |
public function store(StoreAcceptanceRequest $request, Invitation $invitation) | |
{ | |
$user = DB::transaction(function () use ($request, $invitation) { | |
// Create user | |
$user = User::create([ | |
'name' => $request->input('name'), | |
// If the user can specify a different email address in the form... | |
'email' => $request->input('email'), | |
// ...Otherwise fill with email from invitation model | |
'email' => $invitation->email, | |
'password' => Hash::make($request->input('password')), | |
]); | |
// Delete invitation | |
$invitation->delete(); | |
// Return user | |
return $user; | |
}); | |
// Log user in and dispatch registered event | |
Auth::login($user); | |
event(new Registered($user)); | |
// Return redirect to home page or whatever | |
return redirect()->to(RouteServiceProvider::HOME); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Http\Controllers\InvitationAcceptanceController; | |
use Illuminate\Support\Facades\Route; | |
// Invitation Acceptance Routes... | |
Route::get('/invitations/{invitation:token}/accept', [InvitationAcceptanceController::class, 'create']); | |
Route::post('/invitations/{invitation:token}/accept', [InvitationAcceptanceController::class, 'store']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment