In this chapter, we will learn how can we implement a login page and make a user login. Then maintain a session for other page queries and logout to destroy session.
Before we start, we assume you have all knowledge of setting up the Firebase Authentication. If not please read this Chapter 6
First of all, below is the code of login method with takes email and password and checks if the credentials is correct or incorrect.
Now if Credentials are correct, then store the uid in the Laravel Session
Now create a custom middleware. lets us name it as 'firebase'. Your middleware code will look like below:
Then register your middleware in your Kernel.php
Then use it the routes of pages that are accessible after login
Then after log in, you can use the session uid, to query, like fetch details
Now on logout simply forgot the uid in session
Hope this chapter of Laravel Firebase login session is clear to you. This is the very basic example. We would have more chapters on this topic. So stay tuned with us and let us know what things you want us to cover in upcoming chapters in below comment section. And please share with your connections who might find this helpful. And for more interaction, please visit for facebook page https://www.facebook.com/laravelwithfirebase
Before we start, we assume you have all knowledge of setting up the Firebase Authentication. If not please read this Chapter 6
First of all, below is the code of login method with takes email and password and checks if the credentials is correct or incorrect.
try {
$logged_user = app('firebase.auth')->verifyPassword($request->emailaddress, $request->password);
//Credentials are correct
} catch (\Kreait\Firebase\Exception\Auth\InvalidPassword $ex) {
return back()->withErrors(['Credentials are incorrect']);
}
Now if Credentials are correct, then store the uid in the Laravel Session
\Session::put('uid', $logged_user->uid);
Now create a custom middleware. lets us name it as 'firebase'. Your middleware code will look like below:
<?php
namespace App\Http\Middleware;
use Closure;
class Firebase
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(\Session::has('uid'))
return $next($request);
else
return redirect('/');
}
}
Then register your middleware in your Kernel.php
protected $routeMiddleware = [
............
'firebase' =>\App\Http\Middleware\Firebase::class
];
Then use it the routes of pages that are accessible after login
Route::group([
'middleware' => 'firebase',
], function () {
//All the routes after logged in
});
Then after log in, you can use the session uid, to query, like fetch details
$student = app('firebase.firestore')->database()->collection('Students')
->document(\Session::get('uid'))->snapshot();
Now on logout simply forgot the uid in session
Route::get('logout', function(){
Session::forget('uid');
});
Hope this chapter of Laravel Firebase login session is clear to you. This is the very basic example. We would have more chapters on this topic. So stay tuned with us and let us know what things you want us to cover in upcoming chapters in below comment section. And please share with your connections who might find this helpful. And for more interaction, please visit for facebook page https://www.facebook.com/laravelwithfirebase
7 Comments
Let us know your doubts here.
ReplyDeletehttps://youtu.be/75aEgBYaexg help at all...
DeleteThanks suhasrkms.
DeleteSure. We will check out this video.
when i tried above code, this error Call to undefined method Kreait\Firebase\Auth::verifyPassword() showing...
ReplyDeleteHi Subina,
DeleteThanks for reaching out us.
Let us know only verify password is not working. Or all other method of sdk not working.
Please read Chapter 5 - Integrate Laravel with Google Firebase connecting Cloud Firestore
https://laravelwithfirebase.blogspot.com/2020/02/chapter-5-integrate-laravel-with-google-firebase-connecting-cloud-firestore.html
It can be package installation be the problem.
Please click the top left follow button. It helps us.
Thanks.......... I'ts worked
ReplyDeleteHi SemicoloCG,
DeleteExtremely happy to hear from you that our blog helped you.
Please show your love and support to this blog, by clicking follow button on top.