I am using PHP Laravel 7, I'm working on a project for which i have to use authentication from outer source, API's are provided for signup and login. I just want to know is it possible that i can use the authentication of Laravel and use my custom login and signup and can i use authentication guard for stopping the URL routes.
I had already created pages for login and register so far but haven't added authentication yet.
if (Auth::check()) {
// The user is logged in...
}
I want something like this for custom login in.
You can receive all the data from your API and then manually create a user and login them.
A piece of example from a real application
$user = User::firstOrCreate(['vk_id' => $data['user_id']]);
auth()->login($user, true); //true is to remember them
After that you can add any data to any field you want:
$user->position = 'ceo';
$user->save();
Or any other logic you want.
You can add this logic to your auth register or login controller, or create a separate one.
Virtually, built-in Auth controllers are the same controller with their own logic to work with a database and sessions.
But login the user manually using ->login() function.
Related
I'm building a system with users and roles using Jetstream(Livewire)/Fortify auth, but it's a closed system (I mean, there is no way to register yourself, you must be invited) so I disabled Fortify's registration feature, but I want the first user to be able to register himself.
In other words: Fortify's registration should be off, but while there isn't an user in database, it should be on (email validation too, but If I can figure out the registration part, the verification should be easy).
I tried making an middleware to check if there isn't a registered user and redirect to register view, but I found some errors (like the view doesn't even exist 'cause fortify registration is off).
Does someone know a way to make this work using the Jetstream stack? Thanks!
and what about this, in the FortifyServiceProvider.php boot method
Fortify::registerView(function () {
if(!User::all()->count())
return view('auth.register');
else
abort(404,'No registration allowed');
});
I have a requirement where I should login and register users through external API (no local database). How can I login user with external API so that, I can use all Laravel User:: and Auth:: features?
You can use Auth::login(). Here is some example code you can modify to your specific API calls.
$response = $api->login($credentials);
$user = User::findByApiId($response->id);
Auth::login($user);
Firstly I'm a real beginner with Laravel so I will try to describe my problem as best as I can.
I am building a website using Laravel however the information on users will not be stored on my server but rather externally on another server.
The only way to access the user's data is through an external API; I am not allowed access to their database. The API request returns a token and I use this token to check with their server to see if the user is logged in.
My question is: how do I authenticate the user so that I can still use Laravel's out of the box guards.
It's really handy to use methods like Auth::check() to determine if the user is still logged in.
You'll either need to modify Laravel's default authentication middleware in app/Http/middleware/Authenticate.php or you'll need to create your own middleware class that runs the authentication that you need. Create a class in the app/Http/middleware folder and register that middleware. https://laravel.com/docs/master/middleware
I am new to Laravel and felt that the Authorization system provided out of the box is difficult to customize and implement.
Is it a good idea to implement the Login/Register functionality from scratch using controllers and middlewares in laravel?
For example, a middleware 'webAuth' will keep a check on whether the user is logged in just by checking request->session()->has('userId').
The login controller will take email and password and search for user the user in database using email, get the password and Hash::check() it with the one provided by user during login. If the password check succeeds, the user will get be authenticated by setting $request->session()->put('userId', $user->userId).
Finally, a register controller can create a new user by taking in the details such as name, email, password, dob etc. using a form and creating a new user model. The user can then be logged in using the same method as mentioned above.
The login and register controllers will of course use Validator and may also redirect user appropriately.
I'm looking for a way for users that are logged in to register new users. I don't want unregistered users creating new users. The problem that I'm coming up against is that Laravel does a lot of rerouting when you use their registration controllers so that it auto reroutes you away from the registration page if you are already logged in. Is there a way that I can get around this functionality without having to rewrite all of the registration logic?
I've tried simply adding auth middleware to the registration route but it immediately reroutes to the home page of the app before you hit the registration view. If I create a route that redirects to the registration view like this-
Route::get('register', function(){
return view('auth.register');
});
It will give me the registration page but silently fail to add any users to the database. I think it may be because the RegistersUsers class has use RedirectsUsers as its first statement. Short of rewriting the code there is there another way to accomplish what I'm trying to do?
I'm using Laravel 5.2 with the make:auth standard views