Skip to content

Commit

Permalink
This fixes the team invite to be simplified. Now the invite link will…
Browse files Browse the repository at this point in the history
… log in the user after clicking
  • Loading branch information
alnutile committed Jun 26, 2024
1 parent 17f49b7 commit 38f3337
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 3 deletions.
1 change: 0 additions & 1 deletion app/Actions/Jetstream/InviteTeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Laravel\Jetstream\Contracts\InvitesTeamMembers;
use Laravel\Jetstream\Events\InvitingTeamMember;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Mail\TeamInvitation;
use Laravel\Jetstream\Rules\Role;

class InviteTeamMember implements InvitesTeamMembers
Expand Down
47 changes: 47 additions & 0 deletions app/Actions/Jetstream/TeamInvitation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Actions\Jetstream;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\URL;
use Laravel\Jetstream\TeamInvitation as TeamInvitationModel;

class TeamInvitation extends Mailable
{
use Queueable, SerializesModels;

/**
* The team invitation instance.
*
* @var \Laravel\Jetstream\TeamInvitation
*/
public $invitation;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct(TeamInvitationModel $invitation)
{
$this->invitation = $invitation;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.team-invitation',
['acceptUrl' => URL::signedRoute('team-invitations.accept',
[
'user' => User::where('email', $this->invitation->email)->first()->id,
'invitation' => $this->invitation,
])])->subject(__('Team Invitation'));
}
}
23 changes: 23 additions & 0 deletions app/Http/Controllers/TeamInviteAcceptController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers;

use App\Models\TeamInvitation;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class TeamInviteAcceptController extends Controller
{
public function accept(Request $request, TeamInvitation $invitation)
{
$user = User::where('email', $invitation->email)->first();
$user->current_team_id = $invitation->team_id;
$user->updateQuietly();
Auth::login($user);

$invitation->delete();

return to_route('collections.index');
}
}
3 changes: 3 additions & 0 deletions app/Models/TeamInvitation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\TeamInvitation as JetstreamTeamInvitation;

class TeamInvitation extends JetstreamTeamInvitation
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
Expand Down
26 changes: 26 additions & 0 deletions database/factories/TeamInvitationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Database\Factories;

use App\Models\Team;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\TeamInvitation>
*/
class TeamInvitationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'role' => 'admin',
'email' => $this->faker->unique()->safeEmail(),
'team_id' => Team::factory(),
];
}
}
2 changes: 1 addition & 1 deletion resources/js/Pages/Teams/Partials/DeleteTeamForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const deleteTeam = () => {
</template>

<template #content>
Are you sure you want to delete this team? Once a team is deleted, all of its resources and data will be permanently deleted.
Are you sure you want to delete this team? Once a team is deleted, all of its resources and data will be permawenently deleted.
</template>

<template #footer>
Expand Down
5 changes: 5 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

Route::get('/team-invitations/{invitation}',
[\App\Http\Controllers\TeamInviteAcceptController::class, 'accept'])
->middleware('signed')
->name('team-invitations.accept');

Route::get('/login/signed/{token}', [\App\Http\Controllers\SignedUrlAuth::class,
'signInWithToken'])
->name('signed_url.login');
Expand Down
38 changes: 38 additions & 0 deletions tests/Feature/Http/Controllers/TeamInviteAcceptControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\TeamInvitation;
use App\Models\User;
use Illuminate\Support\Facades\URL;
use Tests\TestCase;

class TeamInviteAcceptControllerTest extends TestCase
{
/**
* A basic feature test example.
*/
public function test_accepts_team_invitation(): void
{
$userInvited = User::factory()->create();

$invite = TeamInvitation::factory()->create([
'email' => $userInvited->email,
]);

$url = URL::signedRoute('team-invitations.accept',
[
'invitation' => $invite,
]);

$this->get($url);

$this->assertAuthenticatedAs($userInvited);

$this->assertDatabaseCount('team_invitations', 0);

//make the user since this is how the system works
//add that email to the invite
//then make the user and visit there
}
}
2 changes: 1 addition & 1 deletion tests/Feature/InviteTeamMemberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Tests\Feature;

use App\Actions\Jetstream\TeamInvitation;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Laravel\Jetstream\Features;
use Laravel\Jetstream\Mail\TeamInvitation;
use Tests\TestCase;

class InviteTeamMemberTest extends TestCase
Expand Down

0 comments on commit 38f3337

Please sign in to comment.