how to show username, name after login on laravel

763 Views Asked by At

I created login with code =

login code

and now I want to show username, name after login, how to write the code for it? thank all

2

There are 2 best solutions below

0
Voxxii On

Hello if you using the default auth model you can get the currently authenticated user via Auth::user().

For name: Auth::user()->name;

For email: Auth::user()->email;

0
Wahyu Kristianto On

The attempt method is normally used to handle authentication attempts from your application's "login" form. If authentication is successful, you should regenerate the user's session to prevent session fixation:

public function process(Request $request)
{
    $credentials = $request->validate([
        'username' => 'required',
        'password' => 'required',
    ]);

    if (Auth::attempt($credentials)) {
        $request->session()->regenerate();

        return redirect('/');
    }

    return back()->with(['error' => 'Login Gagal']);    
}

Retrieving the authenticated user :

// Retrieve the currently authenticated user...
$user = Auth::user();

dd($user->username);