taha amiri
taha amiri

Reputation: 309

Auth::check() doesn't seem to work properly in my Laravel project

I want to redirect users to homepage (named home in route) if they are logged in, and redirect them to loginpage (named loginpage in route) if they're not. The first one works properly, but when I input the right username and password in loginpage, users are redirected to the same loginpage, not home. Here are the Codes

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;

class ClientController extends Controller
{
    public function home() {
    if (!Auth::check())
      return redirect()->route('loginpage');
    else
      return view('home');
  }

  public function loginpage() {
    if (!Auth::check())
      return view('loginpage');
    else
      return redirect()->route('home');
  }
}

And relevant javascript code in my Loginpage is as follows:

function alerting() {
        var usernam = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        $(document).ready(function () {
            $.ajax({
                type: "POST",
                url: '{{ URL::asset('login/login.php') }}',
                dataType: 'json',
                data: {function_name: 'userLogin', uname: usernam, pname: password},
                success: function (data) {
                    if(data=='1'){
                        window.location.href = '{{route('home')}}';
                    }else{
                        alert("Username or password is wrong.");
                    }
                }
            })
        });

    }

Here is the Login function:

<?php

include "config.php";
$ra = "SET NAMES utf8";
$retan = connection();
$resu = $retan->query($ra);
switch ($_POST['function_name']) {
    case 'userLogin':
  userLogin($_POST['uname'],$_POST['pname']);
  break;
}
function userLogin($username,$password){
   $connect = connection();
    $connect->set_charset("utf8");
        $query_in = "SELECT count(*) AS tedad FROM app_users au WHERE au.user_email='".$username."' AND au.user_password='".$password."'";
        $result = $connect->query($query_in);
        $data = mysqli_fetch_assoc($result);
        $primf = $data['tedad'];
  echo json_encode("".$primf);
}

The config file mentioned above:

<?php
define('DB_SERVER', '127.0.0.1:3306');
    define('DB_USERNAME', '');
    define('DB_PASSWORD', '');
    define('DB_DATABASE', 'test');
function connection(){
    $conn = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
    return $conn;
}
?>

Upvotes: 4

Views: 540

Answers (2)

Jorge Rodr&#237;guez
Jorge Rodr&#237;guez

Reputation: 176

You are using the Auth facade to check the user without having invoked Auth::login with the user instance or Auth::loginUsingId.

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

By the way, your userLogin function is subject to SQL Injection. Please try to do as suggested before and work with the standard auth system by laravel which works like a charm.

Upvotes: 1

Hamed Yarandi
Hamed Yarandi

Reputation: 1211

Did you use php artisan make:auth command for generating authenticate system for laravel?

Why are you using hardcore for login function? you should using auth generator according laravel Documentation Authentication quickstart

Upvotes: 0

Related Questions