I am using Laravel 8. After using Auth::login, I am checking if a session is created using Auth::check() and Auth::user() and indeed I can see the user and true for Auth::check(). However upon redirecting the user back to the homepage and checking if the user is logged in using Auth::check() the session seems to be lost because I get false.
Basically I will be getting the user information from another Laravel project and DB through an API if the user credentials are correct. Thus in this Laravel project I only want to create temporary session for the user to access data related to his account.
The session was not persisting since the user did not exist in the users table. Upon creating the user it works. However in reality I will not be storing the users in this Laravel DB but in another.
Thus my question is, is it possible to use Auth::login on a $user which does not exist in the DB?
You cannot use Laravel's Auth system for this. Auth expects an ID of the user to be in session and will reload the user from the database on every request (or try to)
Just put the user credentials into session and write your own middleware and functions to check if these values exist in session.
once you reload the page, yes the session may clear and user details lost...
This is incorrect. We can rely on sessions being persistent otherwise our users would not manage to stay logged in, their shopping carts to remain intact etc.
In your 'login' just store the user's name and any other identifier you need in session. DO NOT use any Auth:: middleware or facade just look for yourself in session, are there credentials there - yes? The user is 'known'
Sorry for any confusion caused, I ended up writing my own Auth Guard and User Provider which authenticates the user credentials through an API call to another backend.