UserRegistrationMagicLink.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\EmailVerification;
  5. use App\User;
  6. class UserRegistrationMagicLink extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'user:app-magic-link {--username=} {--email=}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Get the app magic link for users who register in-app but have not recieved the confirmation email';
  20. /**
  21. * Execute the console command.
  22. *
  23. * @return int
  24. */
  25. public function handle()
  26. {
  27. $username = $this->option('username');
  28. $email = $this->option('email');
  29. if(!$username && !$email) {
  30. $this->error('Please provide the username or email as arguments');
  31. $this->line(' ');
  32. $this->info('Example: ');
  33. $this->info('php artisan user:app-magic-link --username=dansup');
  34. $this->info('php artisan user:app-magic-link --email=dansup@pixelfed.com');
  35. return;
  36. }
  37. $user = User::when($username, function($q, $username) {
  38. return $q->whereUsername($username);
  39. })
  40. ->when($email, function($q, $email) {
  41. return $q->whereEmail($email);
  42. })
  43. ->first();
  44. if(!$user) {
  45. $this->error('We cannot find any matching accounts');
  46. return;
  47. }
  48. if($user->email_verified_at) {
  49. $this->error('User already verified email address');
  50. return;
  51. }
  52. if(!$user->register_source || $user->register_source !== 'app' || !$user->app_register_token) {
  53. $this->error('User did not register via app');
  54. return;
  55. }
  56. $verify = EmailVerification::whereUserId($user->id)->first();
  57. if(!$verify) {
  58. $this->error('Cannot find user verification codes');
  59. return;
  60. }
  61. $appUrl = 'pixelfed://confirm-account/'. $user->app_register_token . '?rt=' . $verify->random_token;
  62. $this->line(' ');
  63. $this->info('Magic link found! Copy the following link and send to user');
  64. $this->line(' ');
  65. $this->line(' ');
  66. $this->info($appUrl);
  67. $this->line(' ');
  68. $this->line(' ');
  69. return Command::SUCCESS;
  70. }
  71. }