Hi,
I have a Laravel project in which I used Laravel's Authentication scaffolding to register, login and logout (amongst other auth actions) users is the web app. Users are stored in the Users table.
The complete system however will also need a way how users can login into their account (Users table) from a mobile application or from other web apps, using API routes. This means that I need a simple way of securely 'forwarding' the email address and password to an APILoginController via an API route (for instance http://localhost/api/login?email=...&password=...).
A function in the APILoginController will:
- Check if the user with the provided email address exists in the Users table.
- If the user is found, the provided password (for example from the mobile application via the API login route) matches the hashed password in the database/Users table, and if these match, the user using the mobile app can be logged in and can request data from other tables in the database of the Laravel project.
Currently, I was thinkng of implementing something like this:
In api.php:
Route::post('/login', 'API\APILoginController@loginUser');
In APILoginController (a controller created under Http/Controllers/API):
public function loginUser(Request $request)
{
$email = $request->input('email');
$password = $request->input('password');
$user = User::where(['email'=>$email)->first();
if($user) // If the user is found
{
// CHECK THAT API PASSWORD MATCHES DB PASSWORD
// LOGIN USER
}else{
return 'user not found'; // You can create a page for this
}
}
My question is, how should I 'login' the user via an API login such that for example mobile app users can login and request data from the database?
I appreciate any advice and thank you in advance!
Brian