drxy
drxy

Reputation: 135

How to authenticate without database in Laravel 5.2?

I have tried the following code to authenticate in Laravel 5.2. But I don't know how to make login true. I tried Auth::login(true) after if statement but it is not working properly. I have found some articles about this topic but i couldn't find a detailed example.

public static function Login($email,$password)
{
    $fp = fsockopen ( "www.mydomain.com" , 110 );
    if (!$fp) {
        return "Connection Error";
    }
    $trash = fgets ( $fp, 128 );
    fwrite ( $fp, "USER ".$email."\r\n" );
    $trash = fgets ( $fp, 128 );
    fwrite ( $fp, "PASS ".$password."\r\n" );
    $result = fgets ( $fp, 128 );
    if(substr ( $result, 0, 3 ) == '+OK')
        return true; //user will be logged in and then redirect
    else
        return false;
}

If it is possible, could you please add a code here that how to make auth true?

P.S. I can retrieve the user information using email with a simple query after login.

Upvotes: 3

Views: 2920

Answers (1)

Pastor Bones
Pastor Bones

Reputation: 7361

If you know who the user is (IE: have a $user instance already), you can simply login without using any guards as shown in the documentation

Auth::login($user);

I would pass in a User instance instead of $email/$password to your function

$user = User::whereEmail($email)->first();
$class->login($user);

and then in your class

public static function Login(User $user)
{
    // ...
    if(substr ( $result, 0, 3 ) == '+OK')
        Auth::login($user);
        return true; //user will be logged in and then redirect
    else
        return false;
}

Upvotes: 1

Related Questions