skip to Main Content

Drawing a blank here as I have been out of the game for some time and busy on a new application.

Objective: Send a newly registered user a welcome message with a modal when he logs in for the first time.

This should only happen the first time he logs in, for example:

newuser.blade.php

@if ($first_login == true)
    <h3>Welcome Popup</h3>
@else 
    <h3>Hey! 🖐 Nothing to Show</h3>
@endif

I have added a boolean column to my users table called: first_login

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->boolean('first_login')->default(true);
    });
}

Made a controller called FirstLogin.php:

<?php

namespace AppHttpControllers;

use Auth;
use IlluminateHttpRequest;

class FirstLogin extends Controller
{
    public function FirstLogin()
    {
        if (Auth::user()->first_login) {
            $first_login = true;
            Auth::user()->first_login = true;
            Auth::user()->save();
        } else {
            $first_login = false;
        }

        return view(
            'test',
            ['first_login' => $first_login]
        );
    }
}

What are now the next steps? I am really pulling a blank on this as, like I said, been years not building anything.

So to cut it short, the column should update after the user logs out I guess?

Because if we update the column when the user logs in the first time the modal can not be shown.

I have the feeling I am forgetting something here…

Any advice is appreciated.

3

Answers


  1. You can modify the login and logout methods in the relevant controller, e.g., LoginController, or wherever you handle the user login and logout process. You can update the first_login column.

    use Auth;
    use IlluminateHttpRequest;
    
    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');
    
        if (Auth::attempt($credentials)) {
            $user = Auth::user();
    
            if ($user->first_login) {
                $user->first_login = false;
                $user->save();
                // Perform actions for first login (e.g., send welcome message)
            } else {
                // Actions for subsequent logins (e.g., update last login timestamp)
            }
    
            // Redirect the user to their dashboard or desired page
            return redirect()->intended('/dashboard');
        } else {
            // Handle invalid login credentials
            return redirect()->back()->withErrors(['email' => 'Invalid credentials']);
        }
    }
    
    public function logout(Request $request)
    {
        $user = Auth::user();
        $user->first_login = false;
        $user->save();
    
        Auth::logout();
    
        return redirect('/'); // Redirect to the homepage or desired page
    }
    
    Login or Signup to reply.
  2. You can store a variable named firstLogin in the session when the use login for the first time.

    You might use the following controller to have the sessions flag created.

    <?php
    
    namespace AppHttpControllers;
    
    use Auth;
    use IlluminateHttpRequest;
    
    class AuthenticationController extends Controller
    {
        public function authenticate(Request $request)
        {
            $user = Auth::user();
    
            if ($user->first_login) {
                $request->session()->put('firstLogin', true);
                $user->first_login = false;
                $user->save();
            }
                
            return view('test');
        }
    }
    

    Then in your blade template you can check if the session variable exists:

    @if (session('firstLogin'))
        <h3>Welcome Popup</h3>
    @else 
        <h3>Hey! 🖐 Nothing to Show</h3>
    @endif
    

    Note: This will keep showing the popup to user until a new session is established.

    Login or Signup to reply.
  3. You can rename the first_login column to logged_in_before with default value false:

    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->boolean('logged_in_before')->default(false);
        });
    }
    

    And then, change it to true on the first opening of your dashboard:

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    
    class DashboardController extends Controller
    {
        public function __invoke(Request $request)
        {
            $user = $request->user();
            $shouldShowWelcomeModal = false;
    
            if ($user->logged_in_before === false) {
                $shouldShowWelcomeModal = true;
                
                $user->logged_in_before = true;
                $user->save();
            }
    
            return view(
                'dashboard',
                ['show_welcome_modal' => $shouldShowWelcomeModal]
            );
        }
    }
    

    Additionally, you can not bind the logic to first login and just make variable is_welcome_modal_shown that will work as well as in the code above. Anyway, it guarantees that the modal will be shown one time and doesn’t require any tricks with session and logout.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search