By default Laravel 5.3 login user automatically after registration.I need a suggestion for get the user ID after the user automatically logged in and then run a function without redirection.How can I do this?
You can edit create() method in Auth\RegisterController.php and call your function there.
Related
I am stuck somewhere in creating Multi Auth Laravel Project. I setup the project on Laravel 8 and I have done Jetstream, Fortify setup.
I have created 4 nwidart Modules
SuperAdmin
Admin
User
Meeting
Database already linked, User role already created, I am able to login role-based with different user dashboard. My question is.
I want to redirect users to a specific Module after login. If SuperAdmin logged in he should redirect to the above module pages (I already created pages and those are working fine). Here are the screenshots.
So if you are using Jetstream you would probably have a file called AuthenticatedSessionController.php which should handle the login
There is a method called store
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
return redirect()->intended(RouteServiceProvider::HOME);
}
You could change the logic here so that where it has intended(RouteServiceProvider::HOME)
You could change it to:
$route = auth()->user()->isSuperAdmin() ? route('superadmin.index') : route('normaluser.index');
return redirect()->intended($route);
I'm trying to make a user logged in at back-end who is already a user of another website of mine. For now I'm able to fetch the user's data from previous website's database using an API and make user registration during the login process. Same time I want this user to be logged in when data is just inserted because now user is existing. I tried to reuse same method $this->processLogin(); but this method takes request function processLogin(Request $request) I can't feel passing email & pass to utilize this same method. I have tried guzzle self request with 3 parms 'email, password, _token' using POST request which didn't work. I don't want to exclude this route as well because it is being used for normal login. How can i make this user logged in right after inserting the required data? Please advise. Thanks in advance.
// if $id is your user that you want to login, then:
auth()->loginUsingId($id);
I'm trying to understand how an authentication request is handled by laravel in order to be able to write my own authentication method that allows me to check data from multiple tables before authenticating (basically there are some columns that I need to check to understand if a user is able to login).
I'm actually quietly confused about the relation between guards, middleware, provider and driver.
Can someone please explain me the lifecycle of a login request?
Thank you
EDIT: i'm working with laravel 5.7, standard Auth scaffolding that is available using make:auth
To make a custom authentication follow this steps
1.go to routes/web.php and make a get route to handle login view and post login route to handle login logic
Route::get('login','LoginController#show')
Route::post('login','LoginController#login')
2. Make a controller called LoginController
php artisan make:controller LoginController
4.inside LoginController make a function called login to handle login logic like this
public function login(){
$input = $this->validate(request(),['username'=>'required','password'=>'required']);
$model = CustomUsersModel::where('username',$input['username'])
->where('password',bcrypt($input['password']))->first();
if($model){
//user exist and valid login information
auth()->login($model);//login user via model
//now user loggedin
}
//handle wrong login information
}
I am using Multiple Authentication in laravel 5.4. Here I have two controller for login. One is for normal user and other is for admin. Both controller is restricted by guest middleware. Because of using this guest controller is if a person is logged in as admin or user he must not to get any log in page. But this guest middleware only work when I log in using UserController. I mean if I log in using user controller then try to go to the log in page or admin page it redirect me to user dashboard page. That is ok. But when I log in using AdminLoginController then try to log in as user or log in again as admin it allow me to log in again.
I have used this construct function in both controller.
public function __construct()
{
$this->middleware('guest');
}
Help me please.
this question can't solved my problem. Because I have downloaded code from that solution and I still got this error. That logged in admin can visit user log in form. This error also have in this answer.
Restrict them with the auth middleware:
public function __construct()
{
$this->middleware('auth');
}
https://laravel.com/docs/5.5/authentication#protecting-routes
I use Cartalyst Sentinel to authenticate user
$status=Sentinel::authenticate($credentials);
the above code is in the login function in UserController. In the login function I can see that the Sentinel::check() returns some data. However, after login and redirecting the another page in the AccountController, the Sentinel::check() always returns false despite the user has logged in and not yet trigged the logout button.
Anyone knows how to solve this problem ?
You'll need to verify that you are setting the user to remember.
Sentinel::authenticateAndRemember($credentials)
Just authenticating doesn't set the user in the session. If that doesn't fix your problem you should verify the Facade you're using. Sentinel comes packaged with two facades, both a Laravel and Native facade. If you're using Laravel and call the Native Facade you will get a false return. The inverse does the same.
Hope that helps.