1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[589],{8713:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>u,contentTitle:()=>a,default:()=>h,frontMatter:()=>s,metadata:()=>o,toc:()=>l});var r=n(5893),i=n(1151);const s={sidebar_position:2},a="Authentication",o={id:"guide/authentication",title:"Authentication",description:"Web applications often require a robust authentication system for users to securely log in. Implementing such a feature can be complex and pose potential risks. Laravel Firestore Eloquent aims to simplify and expedite the process by providing tools that empower you to implement authentication swiftly, securely, and with ease.",source:"@site/versioned_docs/version-2.x/guide/authentication.mdx",sourceDirName:"guide",slug:"/guide/authentication",permalink:"/docs/2.x/guide/authentication",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/versioned_docs/version-2.x/guide/authentication.mdx",tags:[],version:"2.x",sidebarPosition:2,frontMatter:{sidebar_position:2},sidebar:"tutorialSidebar",previous:{title:"Artisan Command",permalink:"/docs/2.x/guide/artisan-command"},next:{title:"Eloquent ORM",permalink:"/docs/2.x/category/eloquent-orm"}},u={},l=[{value:"Authenticating Users",id:"authenticating-users",level:2},{value:"Retrieving The Authenticated User",id:"retrieving-the-authenticated-user",level:2},{value:"Logging Out",id:"logging-out",level:2},{value:"Authentication helper",id:"authentication-helper",level:2}];function c(e){const t={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",hr:"hr",p:"p",pre:"pre",strong:"strong",...(0,i.a)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(t.h1,{id:"authentication",children:"Authentication"}),"\n",(0,r.jsx)(t.p,{children:"Web applications often require a robust authentication system for users to securely log in. Implementing such a feature can be complex and pose potential risks. Laravel Firestore Eloquent aims to simplify and expedite the process by providing tools that empower you to implement authentication swiftly, securely, and with ease."}),"\n",(0,r.jsx)(t.h2,{id:"authenticating-users",children:"Authenticating Users"}),"\n",(0,r.jsxs)(t.p,{children:["To utilize Laravel Firestore Eloquent authentication services, we'll access them through the ",(0,r.jsx)(t.code,{children:"FAuth"})," class. Therefore, ensure to import the ",(0,r.jsx)(t.code,{children:"FAuth"})," class at the top of your class. Now, let's delve into the attempt method. Typically the attempt is employed to manage authentication attempts from your application's login form, the attempt method, if successful, should include session regeneration for the user to avert session fixation:"]}),"\n",(0,r.jsx)(t.admonition,{title:"PROTECTING ROUTES",type:"note",children:(0,r.jsxs)(t.p,{children:["To add authentication middleware to your auth routes.",(0,r.jsx)(t.a,{href:"/docs/guide/authentication",children:"See here"}),"."]})}),"\n",(0,r.jsx)(t.p,{children:(0,r.jsx)(t.strong,{children:"Usage For Livewire"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-php",children:"<?php\r\n//Livewire Example\r\n\r\n//namespace ...;\r\n\r\nuse Livewire\\Component;\r\nuse Roddy\\FirestoreEloquent\\Auth\\FAuth;\r\n\r\nclass Login extends Component\r\n{\r\n public $password, $email;\r\n\r\n protected $rules = [\r\n //validation rules...\r\n ];\r\n\r\n protected $messages = [\r\n //validation messages...\r\n ];\r\n\r\n public function login()\r\n {\r\n /**\r\n * Validates the user input data.\r\n */\r\n $this->validate();\r\n\r\n /**\r\n * Attempt to authenticate the user with the given email and password.\r\n * If the authentication fails, flash an error message to the session.\r\n *\r\n * @return void\r\n */\r\n if(!FAuth::attempt(['email' => $this->email, 'password' => $this->password])){\r\n return session()->flash('credentials', 'Incorrect credentials');\r\n }\r\n\r\n /**\r\n * Check if the user's account status is active before allowing login.\r\n * If the user's account status is not active, flash a message indicating the reason for login failure.\r\n */\r\n if(FAuth::user()->status !== 'active'){\r\n return session()->flash('credentials', 'You cannot login because your account is '.FAuth::user()->status );\r\n }\r\n request()->session()->regenerate();\r\n return redirect()->route('dashboard');\r\n }\r\n}\n"})}),"\n",(0,r.jsx)(t.hr,{}),"\n",(0,r.jsx)(t.p,{children:(0,r.jsx)(t.strong,{children:"Usage For Laravel"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-php",children:"<?php\r\n//Laravel Example\r\n\r\nnamespace App\\Http\\Controllers;\r\n \r\nuse Illuminate\\Http\\Request;\r\nuse Illuminate\\Http\\RedirectResponse;\r\nuse Roddy\\FirestoreEloquent\\Auth\\FAuth;\r\n \r\nclass LoginController extends Controller\r\n{\r\n /**\r\n * Handle an authentication attempt.\r\n */\r\n public function authenticate(Request $request): RedirectResponse\r\n {\r\n $credentials = $request->validate([\r\n 'email' => ['required', 'email'],\r\n 'password' => ['required'],\r\n ]);\r\n \r\n if (FAuth::attempt($credentials)) {\r\n $request->session()->regenerate();\r\n \r\n return redirect()->route('dashboard');\r\n }\r\n \r\n return back()->withErrors([\r\n 'email' => 'The provided credentials do not match our records.',\r\n ])->onlyInput('email');\r\n }\r\n}\n"})}),"\n",(0,r.jsx)(t.h2,{id:"retrieving-the-authenticated-user",children:"Retrieving The Authenticated User"}),"\n",(0,r.jsxs)(t.p,{children:["To retrieve the authenticated user, we'll use the ",(0,r.jsx)(t.code,{children:"user"})," method on the ",(0,r.jsx)(t.code,{children:"FAuth"})," class. This method will return an instance of the authenticated user:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-php",children:"<?php\r\n\r\nuse Roddy\\FirestoreEloquent\\Auth\\FAuth;\r\n\r\n// Retrieve the currently authenticated user...\r\n$user = FAuth::user();\r\n\r\n// Retrieve the currently authenticated user's ID...\r\n$id = FAuth::id();\n"})}),"\n",(0,r.jsx)(t.h2,{id:"logging-out",children:"Logging Out"}),"\n",(0,r.jsxs)(t.p,{children:["To log users out of your application, you may use the ",(0,r.jsx)(t.code,{children:"logout"})," method on the ",(0,r.jsx)(t.code,{children:"FAuth"})," class. This will clear the authentication information in the user's session:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-php",children:"<?php\r\n\r\nuse Illuminate\\Http\\Request;\r\nuse Illuminate\\Http\\RedirectResponse;\r\nuse Roddy\\FirestoreEloquent\\Auth\\FAuth;\r\n \r\n/**\r\n * Log the user out of the application.\r\n */\r\npublic function logout(Request $request): RedirectResponse\r\n{\r\n FAuth::logout();\r\n \r\n $request->session()->invalidate();\r\n \r\n $request->session()->regenerateToken();\r\n \r\n return redirect('/');\r\n}\n"})}),"\n",(0,r.jsx)(t.h2,{id:"authentication-helper",children:"Authentication helper"}),"\n",(0,r.jsxs)(t.p,{children:["The ",(0,r.jsx)(t.code,{children:"fauth"})," function returns an authenticator instance. You may use it as an alternative to the ",(0,r.jsx)(t.code,{children:"FAuth"})," class:"]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-php",children:"// Retrieve the currently authenticated user...\r\n$user = fauth()->user();\r\n\r\n// Retrieve the currently authenticated user's ID...\r\n$id = fauth()->id();\n"})})]})}function h(e={}){const{wrapper:t}={...(0,i.a)(),...e.components};return t?(0,r.jsx)(t,{...e,children:(0,r.jsx)(c,{...e})}):c(e)}},1151:(e,t,n)=>{n.d(t,{Z:()=>o,a:()=>a});var r=n(7294);const i={},s=r.createContext(i);function a(e){const t=r.useContext(s);return r.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function o(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:a(e.components),r.createElement(s.Provider,{value:t},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.