Laravel - Trying to get property of non-object when logging in twice - php

Not sure what's happening here.
If have this as my login function in one of my controllers
protected $user;
public function __construct()
{
$this->user = Auth::user();
}
public function postLogin()
{
if( Auth::attempt([
'username'=>Input::get('username'),
'password'=>Input::get('password')
]))
{
return $this->user->username;
}
else
{
return Redirect::to('login');
}
}
and this as my logout
public function getLogout()
{
Auth::logout();
return Redirect::to('login');
}
If I sign in once then it displays the username , just like I want.
If I logout and log in again, the I get a
ErrorException (E_UNKNOWN)
Trying to get property of non-object
Then if I logout and re-login it works once more until I logout again.
Any ideas as to what is going on here?

I think the problem is both in:
$this->user = Auth::user();
and
return $this->user->username;
In constructor you should rather do something like this:
if (Auth::check()) {
$this->user = Auth::user();
}
but when logging in you shouldn't use:
return $this->user->username;
but
return Auth::user()->username;

Related

Call to a member function get() on null - LARAVEL

Seeking for your help again. Just wondering for any idea's to return something if the column is NULL. Please see below codes: Thank you!
Application Controller
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first()->get();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
If user is not authenticated then you will get unexpected error. So first check the auth as Auth::check()
Try this :
use Illuminate\Support\Facades\Auth;
public function index()
{
if(!Auth::check()) {
return view('login');
}
$applications = Application::where('user_id', $Auth::user()->id)->first();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}

Laravel 5 return redirect is not working as it should

I'm having a controller with some functions. In every function I get user data by sharing it from the Contoller.php
In controller.php
public function share_user_data() {
$user = Auth::user();
$this->checkValidation($user);
$this->user = $user;
View::share('user', $user);
}
public function checkValidation($user){
if($user->email_activated == 0){
var_dump($user->email_activated); // I get: int(0)
return redirect('/verifyEmail');
}
}
In the other controller
public function viewCategory(Category $category){
$this->share_user_data(); // here's the check
$this->get_site_lang();
$products = $category->products;
return view('category', compact('category','products'));
}
But I get the category view and not redirected to the verifyEmail route. How to fix this and why it's happening?
The controller function called by the route is the one responsible for the response. I guess it is viewCategory() in your example?
Your viewCategory() function is always returning view(). It must return redirect() instead. I think the main function should be responsible for picking the output of the request.
private function checkValidation($user) {
return $user->email_activated == 0;
}
public function viewCategory(Category $category) {
$user = Auth::user();
/* ... call to share_user_data() or whatever ... */
if ($this->checkValidation($user)) {
return redirect('/verifyEmail');
}
return view('category', compact('category','products'));
}

How to use Get function as object

Im making a login page , so i need to compare the passoword form DB and the on form form , whats wrong with my code , it says Trying to get property of non-object
Using laravel
public function index()
{
// $mahasiswa = DB::table('mahasiswa')-> get();
return view('index');
}
public function cek(Request $request)
{
$user = DB::table('mahasiswa')->where('npm', $request->npm)->first();
if ($user->Password == $request->password) {
redirect('welcome');
} else {
return ('eror');
}
}
I expect its all right to write like that
you can easily use
Auth::attempt
to check user credentials and this is the correct way to do what you want
or
use this code
if (Hash::check($request->password, $user->password)) {
// true...
}
and the error Trying to get property of non-object
as you write in your code
if($user->Password == $request->password){
redirect('welcome');
}
the problem with Password you write p as a capital not small
I hope my answer help you!
try this, if the error persist, check your database password field if its getting the field. $user->password;
public function index()
{
// $mahasiswa = DB::table('mahasiswa')-> get();
return view('index');
}
public function cek(Request $request)
{
$user = DB::table('mahasiswa')->where('npm', $request->input('npm'))->first();
if ($user->Password == $request->input('password')) {
redirect('welcome');
} else {
return ('erorr');
}
}

Double authentication connection cannot be redirected

I am trying to set up a double authentication page under laravel, for that I add a checkTotp method that verifies that the user has activate double authentication and redirect this user to the page in question.
The problem is that I am not redirected and the code continues to execute.
public function login(Request $request)
{
$this->validateLogin($request);
...
$this->checkTotp($request);
dd('after');
...
}
protected function checkTotp(Request $request)
{
$user = User::where('email', $request->get('email'))->first();
if (!is_null($user->totp_key)) {
$request->session()->put('user_id', $user->id);
return redirect('login/totp');
}
}
What happens is that I enter the checkTotp method but the redirect does not work. My output is the dd('after'). I don't understand why I am not redirected. Can someone help me?
Quentin
The checkTotp function returns a redirect, but you want the login function to return that redirect, such that it is passed to the browser. You might want to move the redirect to the main function and let checkTOTP just return true/false.
public function login(Request $request)
{
$this->validateLogin($request);
...
if ($this->checkTotp($request)) return redirect('login/totp');
dd('after');
...
}
protected function checkTotp(Request $request)
{
$user = User::where('email', $request->get('email'))->first();
if (!is_null($user->totp_key)) {
$request->session()->put('user_id', $user->id);
return true;
}
return false;
}

Laravel passing username parameter to controller from user.blade.php

Hi on my website I have profile with username parameter root/user/{username}. I was planning to add button to block the user. My problem is that when other user click block button, the button do stuffs in the Check.php controller but it doesn't pass the user/{username} parameter that I need in if statements. My question is how I can pass the {username} parameter from my user.blade.php to the Check.php controller?
As we are not seeing any code here, it seems that to do what you need, you just have to create a route pointing to your controller method and eventually a route to redirect your users when successufully blocked:
Route::get('user/block/{username}', 'BlockUserController#block');
Route::get('userBlocked', 'BlockUserController#blocked');
And the controller itself:
class BlockUserController extends Controller {
public function block($username)
{
$user = User::where('username', $username);
$user->blocked = true;
$user->save();
return Redirect::to('userBlocked');
}
public function blocked($username)
{
return View::make('user.blocked');
}
}
And then if you click the button pointing to the route:
http://application.com/user/block/user3398940
It will be blocked.
If you want to go a little more advanced in Laravel, you can use dependency injection and remove some code from your controller:
class BlockUserController extends Controller {
private $user;
public function __construct(User $user)
$this->user = $user;
}
public function block($username)
{
if ($user->block($username))
{
return Redirect::to('userBlocked');
}
return Redirect::back()->with('message', 'User not found');
}
public function blocked($username)
{
return View::make('user.blocked');
}
}
And your user model would have to have a block method:
class User extends Eloquent {
public function block($username)
{
if ($user = $this->newQuery()->where('username', $username))
{
$user->blocked = true;
return $user->save();
}
return false;
}
}

Categories