#Login auth not working

22 messages · Page 1 of 1 (latest)

ruby briar
#

Hello, so when I log in my account, it logs successfully but my auth() doesnt work, it always takes me as a guest so I can't display the user's username or email or anything related

peak imp
#

Did you check your middleware? If you can invite me to your github I will be able to check and help you.

ruby briar
#

Hello @peak imp, sadly I don't have a github atm but I can show you the routes and stuff, I'm lowkey new to laravel

peak imp
#

I want to check your code, so please zip it and upload it to file.io

ruby briar
#
public function register()
    {

        return view('ExoCrypt.Account.register');
    }

    public function store()
    {
        $validate = request()->validate([
            'username' => 'required|min:3|max:20',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:5|max:40|confirmed'
        ]);

        do {
            $customID = $this->generateID();
        } while (User::where('id_user', $customID)->exists());

        $user = User::create([
            'id_user' => $customID,
            'username' => $validate['username'],
            'email' => $validate['email'],
            'password' => Hash::make($validate['password'])
        ]);

        // Mail in proccess later

        return redirect()->route('index')->with('success', 'Account created successfully!');
    }

Register ^

public function authenticate()
    {
        $validate = request()->validate(
            [
                'email' => 'required|email',
                'password' => 'required|min:5'
            ]
        );

        if (Auth::attempt($validate)) {
            request()->session()->regenerate();
            return redirect()->route('index')->with('success', 'logged in successfully');
            // I still go to my index as a guest
        }
        return redirect()->route('login')->withErrors([
            'password' => 'no matching user'
        ]);
    }

login ^

I've made sure my forms also have @csrf aswell

midnight bear
#

@ruby briar You change the User Model??, if you don't change, for default Auth don't have username field.

ruby briar
#
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'username',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
        ];
    }
}
midnight bear
#

Show user migration

ruby briar
#

1 second

#
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->string('id_user')->primary();
            $table->string('username');
            $table->string('email')->unique();
            $table->string('phone_number')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

        Schema::create('password_reset_tokens', function (Blueprint $table) {
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->foreignId('user_id')->nullable()->index();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->longText('payload');
            $table->integer('last_activity')->index();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('sessions');
    }
};

is this what u looking for ?
I've also removed the 'phone_number' since I was not planning on using it

midnight bear
#

I see that username field exists in migration....
Are you sure that user are authenticated?

Try this, login with user and make this:

dd(Auth::user());

And show me the print.

ruby briar
#

sure let me do it real quick

#

That's the account I try to log in with

#

my login works successfuly but when I redirect to the index, I could still see the @guest @endguest info instead of @auth

#

This could be weird but should I change my id_user to auto increment instead of a string ? I just thought a string would look better but from gpt's, it said that

  • user table can lead to some complications, especially when working with Laravel’s authentication system, which expects an auto-incrementing integer (id) by default. While it’s possible to use a string as the primary key, it requires some additional configurations.
midnight bear
#

Why you change type of ID field?
Maybe this is a problem.

#

Try rollback....

#

$table->id();

ruby briar
#

I still haven't modified anything

ruby briar
#

so I just don't know whats going on