Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adnanerlansyah403's avatar

How to generate a random code for everyday

Hello everyone I need a help. About how to generate a random code every next day, so when a day's already tomorrow then the code will be generated. So for example, I have a case like this.

// Generate route dynamic for login
    // Check if already tomorrow
    $generateRoute = '62639d1c3fbdd';
    if(Carbon::now()->isTomorrow()) {
        $generateRoute = uniqid();
    }
    $users = User::where('is_admin', 1)->get();

    foreach($users as $user) {
        // Send generateRoute to email user 
        Mail::to($user->email)->send(new sendInfoRouteLogin($generateRoute));
        
    }
        Route::get('/login/'.$generateRoute, [AuthenticatedSessionController::class, 'create'])
            ->middleware(['guest:'.config('fortify.guard')])
            ->name('login');
    }

Is it a true way?, I need a suggestions.

And thanks for all everyone 🙏.

0 likes
7 replies
Snapey's avatar

how can now() ever be tomorrow?

1 like
underdash's avatar

Is it a true way?

Definitely NOT !

At the top of my head, I will recommend you create a dynamic route in your web.php file; And process requests to that endpoint accordingly.

web.php

use Illuminate\Support\Facades\Route;

Route::get('/daily/{id}', function($id) {
 // Check whether id is valid or not
});

I think a file based cache will utterly suit the very feature you want to implement.

web.php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Cache;

Route::get('/daily/{token}', function($token) {
 $presistedToken = Cache::remeber('daily_token', now()->endOfDay(), function() {
  $newToken = bin2hex(random_bytes(7));
  // Email token to all users
  return $newToken;
 });
 // Do whatever you like, for example
 abort_if( $token != $persistedToken, 403 );
});

This code will generate a new token for you and stores it inside Laravel's default cache driver when clock turns to 00:00:00 every day.

adnanerlansyah403's avatar

@underdash but I wanted to implement it in login controller, and I'm using a Jetstream authentication for that. And I think it's little bit tricky to settting like your example. Because this controller login isn't a closure.

underdash's avatar

You can simply move this logic to any controller you want.

What is preventing you for doing so ?

adnanerlansyah403's avatar

@underdash I just changed the logic like this.

$presistedToken = Cache::remember('login_token', now()->endOfDay(), function () {

            $newToken = Str::random(60);

            return $newToken;

        });

        $users = User::where('is_admin', 1)->get();

        foreach($users as $user){
            $userVisitLink = $user->visit_link_login_at;

            
            if($userVisitLink == null){
                $user->visit_link_login_at = now();
                $user->save();
                Mail::to($user->email)->send(new sendInfoRouteLogin($presistedToken));
            }

            if(now() == now()->endOfDay()) {
                $user->visit_link_login_at = null;
                $user->save();
            }

        }
        
        Route::get('/login/'.$presistedToken, [AuthenticatedSessionController::class, 'create'])
            ->middleware(['guest:'.config('fortify.guard')])
            ->name('login');
underdash's avatar

@adnanerlansyah403 Defining dynamic routes like you did is not recommended you have to check provided token in your controller. Not defining a new route with with some random string.

underdash's avatar

But in order to address such a problem; The common and accepted way is to create a temporary signed route.

Laravel URL Documentation

use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Route;

// For example
Route::get('generate', function() {
 return URL::temporarySignedRoute(
  'dashboard',
  now()->endOfDay(),
  ['user' => auth()->id()]
 );
})

Route::get('dashboard', function(){
 abort_unless(request()->hasValidSignature(), 403);
 return view('dashboard');
})->name('dashboard');

Please or to participate in this conversation.