-
Notifications
You must be signed in to change notification settings - Fork 60
FEAT(auth): implement magic link authentication #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
00b6966
"FEAT(auth): implement magic link authentication
sam-uel-ayo 549ae9c
"fix(auth): handle email sending failure in magic link endpoint
sam-uel-ayo 71cb34e
fix(auth): ensure consistent error response for email sending failure
sam-uel-ayo 78d8369
fix(tests): correct Mail facade mocking in MagicLinkTest
sam-uel-ayo 94d3cfb
fix(tests): simulate email sending failure using shouldReceive
sam-uel-ayo 53f8d74
fix(tests): use chained Mail facade expectation for email sending fai…
sam-uel-ayo d0ff2e0
docs: add magic link authentication endpoint to API documentation
sam-uel-ayo 1a3ad07
fix(auth): remove migration from controller
sam-uel-ayo e4ea17a
FEAT(auth): implement magic link authentication
sam-uel-ayo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Mail; | ||
use App\Mail\MagicLinkEmail; | ||
use Illuminate\Support\Str; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\Validator; | ||
use Exception; | ||
|
||
class MagicLinkController extends Controller | ||
{ | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->artisan('migrate'); // Run migrations for the test database | ||
} | ||
|
||
|
||
// Send a magic link to the user's email. | ||
public function sendMagicLink(Request $request) | ||
{ | ||
try { | ||
|
||
$validator = Validator::make($request->all(), [ | ||
'email' => 'required|email', | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return response()->json([ | ||
'status_code' => 400, | ||
'status' => 'error', | ||
'message' => 'Invalid email address', | ||
'data' => $validator->errors(), | ||
], 400); | ||
} | ||
|
||
$user = User::where('email', $request->email)->first(); | ||
|
||
if (!$user) { | ||
return response()->json([ | ||
'status_code' => 404, | ||
'status' => 'error', | ||
'message' => 'User not found', | ||
'data' => [], | ||
], 404); | ||
} | ||
|
||
// Generate a unique token | ||
$token = Str::random(60); | ||
$expiresAt = Carbon::now()->addMinutes(30); // expires in 30 minutes | ||
|
||
// Save the token to the user in db | ||
$user->magic_link_token = $token; | ||
$user->magic_link_expires_at = $expiresAt; | ||
$user->save(); | ||
|
||
// Send email | ||
Mail::to($user->email)->send(new MagicLinkEmail($token)); | ||
|
||
return response()->json([ | ||
'status_code' => 200, | ||
'status' => 'success', | ||
'message' => 'Verification token sent to email', | ||
], 200); | ||
} catch (\Exception $e) { | ||
return response()->json([ | ||
'status_code' => 500, | ||
'status' => 'error', | ||
'message' => 'Failed to send email', | ||
], 500); | ||
} | ||
} | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Mail; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Mail\Mailables\Content; | ||
use Illuminate\Mail\Mailables\Envelope; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class MagicLinkEmail extends Mailable | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
public $token; // Add a public property to store the token | ||
|
||
/** | ||
* Create a new message instance. | ||
* | ||
* @param string $token | ||
*/ | ||
public function __construct($token) | ||
{ | ||
$this->token = $token; // Store the token | ||
} | ||
|
||
/** | ||
* Get the message envelope. | ||
*/ | ||
public function envelope(): Envelope | ||
{ | ||
return new Envelope( | ||
subject: 'Your Magic Link for Login', // Customize the subject | ||
); | ||
} | ||
|
||
/** | ||
* Get the message content definition. | ||
*/ | ||
public function content(): Content | ||
{ | ||
return new Content( | ||
view: 'emails.magic_link', // Use the custom email view | ||
with: ['token' => $this->token], // Pass the token to the view | ||
); | ||
} | ||
|
||
/** | ||
* Get the attachments for the message. | ||
* | ||
* @return array<int, \Illuminate\Mail\Mailables\Attachment> | ||
*/ | ||
public function attachments(): array | ||
{ | ||
return []; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
database/migrations/2025_03_01_052032_add_magic_link_columns_to_users_table.php
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class AddMagicLinkColumnsToUsersTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->string('magic_link_token', 100)->nullable()->after('remember_token'); | ||
$table->timestamp('magic_link_expires_at')->nullable()->after('magic_link_token'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->dropColumn('magic_link_token'); | ||
$table->dropColumn('magic_link_expires_at'); | ||
}); | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Magic Link Login</title> | ||
</head> | ||
<body> | ||
<h1>Magic Link Login</h1> | ||
<p>Click the link below to log in:</p> | ||
<a href="{{ url('/api/v1/auth/magic-link/verify/' . $token) }}">Login with Magic Link</a> | ||
bhimbho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<p>This link will expire in 30 minutes.</p> | ||
</body> | ||
</html> |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.