Get User ID Within Custom Controller - laravel-5

I've got a Laravel app set up with user authentication, and a form on the user's homepage after logging in. The form posts to a custom controller. What I need is to be able to retrieve the ID of the current user (the one logged in) from within the controller, so it can be saved to a model along with the formdata.
It seems extremely easy, but I'm struggling to get it working. I have:
use Auth;
at the top of my custom controller. Inside the function that the form posts to I have tried:
var_dump(Auth::id());
var_dump(Auth::user()->id);
var_dump(Auth::user());
All of these return null.

As per mokiSRB's suggestion, I added a check for:
if (Auth::check())
And found that the user had been timed out without my knowing. This check is thus important to wrap anywhere you need to retrieve information about the logged in user.

Related

How to allow Laravel admin user to simulate or authenticate as any other user on my site?

I'm trying to figure out how quickest and easiest to allow the admin user on the site I'm building to access and update any user's settings etc. E.g. I've written the code for a regular user to update their settings (and various other actions). Ideally I want an admin to be able to "be" that user as far as my code is concerned, i.e. allow the admin to do anything a user can, to that user's account. Is there any way of doing this?
If I Auth::login() as admin then from the point of view of Laravel I'm the admin user and not the user they might want to edit. If I login as the user then I don't have admin rights (which in my case means an extra admin menu on the navbar with options to suspend or delete the user, or search for other users).
Any thoughts on how to do this please, or am I overcomplicating things? I am looking for a specific functions/code to allow this, rather than a general strategy. I'm using Laravel 5.4, deployed on Heroku. I know there's middleware but it doesn't seem to do what I want as above.
Many thanks.
You could do as suggested in the above comment by Tim Lewis, or you could accept an "override" property in the user edit page where you can pass a specific user ID and then view the page as that user. For instance, the method might look like this:
public function editUser(User $user=null) {
//User that you want to edit can be provided. If not provided, $user will be null and we will load the user that is currently logged in.
if($user!=null && Auth::user()->role=='admin')
$user_to_edit = $user;
else
$user_to_edit = Auth::user();
//other code goes here
}
Then, if you pass a $user object to the method, you will be given the edit page for that user, rather than the Admin. Otherwise, a user will be able to use the same route in order to always view their own edit page.
Be very careful with code like this! You will want to make sure that non-admins do not have the ability to load in a user object and see somebody else's information. That's why I added the $user->role check in the if/else statement, but you might want to add extra security in the form of middleware.
spatie permissions is a wonderful package that I use to make permissions to resources available to super-administrators. https://github.com/spatie/laravel-permission

How to make sure a user can only delete his own records using Laravel

How to make sure a user can only delete his own records.
Here how i am deleting a post with the following url.
http://example.com/dashboard/sessions/delete/{id}
Now user can also pass any post id here and then record will be deleted for that id regardless if the post does not belong to user.
How can i overcome this issue using Laravel
The best way is to use policies for this purpose
Policies are classes that organize authorization logic around a particular model or resource. For example, if your application is a blog, you may have a Post model and a corresponding PostPolicy to authorize user actions such as creating or updating posts.
If for some reason you don't want to use policies, you can check user manually:
if (auth()->check && auth()->user()->id === $post->user_id) {
// Delete post.
}

Changing default functionality in ion auth

I need to change some default functionality of ion auth once a new user has been created by an admin.
The scenario is like so...
Admin creates the user account.
Activation email is sent to the user.
Clicking the link sends the user to the set password page.
Upon setting a password, the user is activated and can log in.
Currently I haven't found a way to define the activation link once ion auth calls the register function and the email is sent. It's set to auth/activate.
The options I see are as follows...
Redefine the behaviour of Auth/activate(). Is this recommended though? Should I be touching the methods in the Auth controller?
Turn off $config['email_activation'] and handle everything myself.
Somehow changing the default controller/method behaviour to handle the activation of the user.
What do people usually do in this situation? Which is best practice?
Ok, after much searching I was reading through https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/2/libraries/Ion_auth.php and noticed the email message being generated was being sent to an email_template (in the view), which I had forgot to check. The activate.tpl.php is where you can find the code that generates the path to the Auth controller.
<?php
echo sprintf(lang('email_activate_subheading'), anchor('auth/activate/'. $id .'/'. $activation, lang('email_activate_link')));
?>
Now I can just easily change the controller path, it makes sense to write my own controller.

How to populate form values after login in Laravel

I got a multi-step form where a user select a phone model, brand, etc and when they click get quote they get a price based on their selection.
To be able to confirm the quote, they need to put their address details or if they already have an account they can login.
The question is how can I redirect the user after they login to a different view with the quote details?
Thanks
By default after users login they get redirected to /home. If you want to change this functionality just define a redirect path in your AuthController like this:
protected $redirectPath = '/your-show-quote-page';
Check Authentication in Laravel Docs for more information
Based on your question, one way of doing what you want is:
Create a quotes table and store the quote information with your multi-step form,
Ask the user to login,
Redirect the user and show him the quote based on his selections.
Lastly, depending if the user is interested in buying the phone you can redirect him to one place or another and delete the quote when you donĀ“t need it anymore.
Other way would be to store the information about the quote in the session and display it when needed. For this you can check Session in Laravel Docs. The process would be the same.
Hope it helps.

Let Authenticated User view only his/her own profile

In my codeigniter application following is the format of user profile
http://example.com/foo/view_profile/userid
how can I restrict a user to view others profile? that means he cannot browse any other link than his profile.
so user foobar420 can not browse following links for example
http://example.com/foo/view_profile/foobar250
http://example.com/foo/view_profile/
http://example.com/any-this-else
How can I achieve this?
Was going to comment this, but it's sort of an answer. Well an idea on this subject at least.
Instead of having "/view-profile/userId" why not just "/view-profile" and send the user model as an object to the page. Then you can just render the proper information only for the user who is actually logged in to the server. Assuming you have access to the user model in your server side script, this is the preferred method.
And if no user model is present, redirect to the login page.

Resources