ZahidGopang's avatar

custom remember me and login without Auth?

i want to use custome login and remember me in laravel how to do it? any best suggestion please it's my login function ?

$aCheckUser = Users::SELECT("UserId","UserName","UserPassword", "UserFullName") ->where("UserName", $sUserName) ->where("UserStatus", 1)->first();

		if( ! empty($aCheckUser))
		{
			$sUserName 		= $aCheckUser->UserName;
			$hashedPassword = $aCheckUser->UserPassword;

			if (Hash::check($dTempPassword, $hashedPassword))
		   		return("success|Login Successful..!");
			else
				throw new \Exception('WrongCredentials');
			
		}else
		{
			throw new \Exception('WrongCredentials');
		}
0 likes
18 replies
ZahidGopang's avatar

@a4ashraf i am using second db connection for login not default so Auth getting default connection ? middleware not working ?

jlrdw's avatar

The authentication chapter will cover how remember me works. Also how to manually setup.

a4ashraf's avatar

@zahidgopang

as I understand, are you use the password for confirmation?

please share your complete user controller

ZahidGopang's avatar

but auth using default connection instead second connection , middleware not working for auth .can we force auth to use second DB connection instead default ?

jlrdw's avatar

In a query just use the connection:

$pet = DB::connection('some_connection')->select('select * from dc_pets where petid = ?', [1]);

For eloquent set it in the model:

protected $connection = 'some_connection';

I showed detailed examples here:

https://laracasts.com/discuss/channels/laravel/dbconnection-does-not-work-as-documented

User is all that's needed.

  • Use authentication for login
  • Use authorization to determine what the logged in user can or cannot do.

https://laravel.com/docs/8.x/authorization

1 like
ZahidGopang's avatar

@a4ashraf how to pass Hash password into this? if (Auth::attempt(['UserName' => $sUserName, 'UserPassword' => $sPassword], $iRememberMe)) { return("success|Login Successful..!"); }

i am checking if (Hash::check($dTempPassword, $hashedPassword)) as i have stored hased password into db?

jlrdw's avatar

Have you used any of the laravel Auth scaffolding, or is this totally custom?

Laravel has a "manual" way already:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function authenticate(Request $request)
    {
        $credentials = $request->only('email', 'password');

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

            return redirect()->intended('dashboard');
        }

        return back()->withErrors([
            'email' => 'The provided credentials do not match our records.',
        ]);
    }
}

See:

https://laravel.com/docs/8.x/authentication#authenticating-users

ZahidGopang's avatar

@jlrdw it's custom . i am unable to pass hashed password into this all because it's showing wrong credentials message.?

ZahidGopang's avatar

@jlrdw this is already done i am unable to pass hashed passworod into laravel auth?

jlrdw's avatar

Do you have the email and password in a users table.

And since manually doing this, you probably need to (after your) custom success, then login to laravel via:

https://laravel.com/docs/8.x/authentication#other-authentication-methods

Perhaps get id from table while checking an:

$id is id you get from users table:
Auth::loginUsingId($id);

But it will be so much easier to follow laravel conventions.

ZahidGopang's avatar

i have UserName instead email, and UserPassword instead password in users table

ZahidGopang's avatar

"Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, string given

getting this error when using Auth::login($user, $remember = true);

jlrdw's avatar

Did you try how to retrieve the ID while logging in, and use it to login.

You might be missing some use statements in your controller.

Edit:

  • A. Are you wanting to use custom login and also use laravel Auth?

  • B. Or is everything going to be custom?

If B, then use your own session strategy.

If A, (and just suggestion) follow laravel documention on authentication.

Please or to participate in this conversation.