in the database i have is_admin is_ac columns which's specify if the user is admin or not , and if s/he have an account or not , im not quite sure how to put that up in the sign up code
signup code
function signup(Request $req)
{
// return $rep -> input();
$user = new User;
$user->Name = $req->name;
$user->Email = $req->email;
$user->Password = Hash::make($req->password);
$user->save();
}
login code just in case
function login(Request $req)
{
$user = User::where(['Username' => $req->username])->first();
$IsAdmin = $user->Is_Admin;
$IsActive = $user->Is_Ac;
if (!$user || !Hash::check($req->password, $user->Password) || $IsActive == 0) {
return("Username or Password is not matched or User is InActive");
} else {
$req->session()->put('user', $user);
$req->session()->put('IsAdmin', $IsAdmin);
return redirect('/');
}
}
CodePudding user response:
Your code needs changes:
public function login(Request $req)
{
$user = User::where(['Username' => $req->username])->first();
if (! $user || ! Hash::check($req->password, $user->Password) || ! $user->Is_Ac) {
return("Username or Password is not matched or User is InActive");
}
$req->session()->put('user', $user);
$req->session()->put('IsAdmin', $user->Is_Admin);
return redirect('/');
}
See how I updated your login
method. I moved the $user->xxx
into the if
but after we checked if $user
has content, then you can do $user->xxx
.
And also see that I have removed the else
. You need to read about If Guard Clauses
here and here.
CodePudding user response:
When you try to fetch the user, you will get a nullable user result (either null or a user).
$user=User::where (['Username'=>$req->username])->first();
Now the first thing you should do is to check if the user is existing or not, and only after trying to read isAdmin and isActive.
function login(Request $req){
$user=User::where(['Username' => $req->username])->first();
if(!$user || !Hash::check($req->password, $user->Password)
|| $user->Is_Ac ==0) {
return "Username or Password is not matched or User is InActive";
}
$req->session()->put('user', $user);
$req->session()->put('IsAdmin', $user->Is_Admin);
return redirect('/');
}