Authentication & Authorization Interview Questions
Comprehensive authentication & authorization interview questions and answers for Laravel. Prepare for your next job interview with expert guidance.
Questions Overview
1. What is Laravel's built-in authentication system?
Basic2. How do you implement basic authentication in Laravel?
Basic3. What is the auth middleware in Laravel?
Basic4. How do you check if a user is authenticated?
Basic5. What are guards in Laravel authentication?
Basic6. How do you implement password reset functionality?
Basic7. What is the remember me functionality?
Basic8. How do you implement email verification?
Basic9. What is Laravel Sanctum?
Basic10. What are policies in Laravel?
Basic11. How do you implement role-based authorization?
Moderate12. How do you implement API authentication using Passport?
Moderate13. What are Gates and how are they used?
Moderate14. How do you implement custom authentication guards?
Moderate15. How do you implement multi-authentication?
Moderate16. What is policy auto-discovery?
Moderate17. How do you implement authentication events?
Moderate18. How do you implement resource authorization?
Moderate19. What are policy filters?
Moderate20. How do you implement token abilities in Sanctum?
Moderate21. How do you implement advanced policy responses?
Advanced22. How do you implement custom user providers?
Advanced23. How do you implement authentication rate limiting?
Advanced24. How do you implement OAuth2 authorization code grant?
Advanced25. How do you implement contextual authorization?
Advanced26. How do you implement passwordless authentication?
Advanced27. How do you implement hierarchical authorization?
Advanced28. How do you implement session authentication customization?
Advanced29. How do you implement cross-domain authentication?
Advanced30. How do you implement dynamic policy resolution?
Advanced1. What is Laravel's built-in authentication system?
BasicLaravel provides a complete authentication system out of the box using the Auth facade. It includes features for user registration, login, password reset, and remember me functionality. Can be scaffolded using laravel/ui or breeze/jetstream packages.
2. How do you implement basic authentication in Laravel?
BasicBasic authentication can be implemented using Auth::attempt(['email' => $email, 'password' => $password]) for login, Auth::login($user) for manual login, and Auth::logout() for logging out. Session-based authentication is default.
3. What is the auth middleware in Laravel?
BasicAuth middleware (auth) protects routes by ensuring users are authenticated. Can be applied to routes or controllers using middleware('auth'). Redirects unauthenticated users to login page or returns 401 for API routes.
4. How do you check if a user is authenticated?
BasicUse Auth::check() to verify authentication status, Auth::user() to get current user, or @auth/@guest Blade directives in views. Request object also provides auth()->user() helper.
5. What are guards in Laravel authentication?
BasicGuards define how users are authenticated for each request. Laravel supports multiple authentication guards (web, api) configured in config/auth.php. Each guard specifies provider and driver for authentication.
6. How do you implement password reset functionality?
BasicLaravel includes password reset using Password facade. Uses notifications system to send reset links. Requires password_resets table. Can customize views, expiration time, and throttling.
7. What is the remember me functionality?
BasicRemember me allows users to stay logged in across sessions using secure cookie. Implemented by passing true as second parameter to Auth::attempt() or using remember() method. Requires remember_token column.
8. How do you implement email verification?
BasicEmail verification uses MustVerifyEmail interface and VerifiesEmails trait. Sends verification email on registration. Can protect routes with verified middleware. Customizable verification notice and email.
9. What is Laravel Sanctum?
BasicSanctum provides lightweight authentication for SPAs and mobile applications. Issues API tokens, handles SPA authentication through cookies. Supports multiple tokens per user with different abilities.
10. What are policies in Laravel?
BasicPolicies organize authorization logic around models or resources. Created using make:policy command. Methods correspond to actions (view, create, update, delete). Used with Gate facade or @can directive.
11. How do you implement role-based authorization?
ModerateRole-based authorization can use Gates, Policies, or packages like Spatie permissions. Define roles and permissions in database. Check using can() method or middleware. Support multiple roles per user.
12. How do you implement API authentication using Passport?
ModeratePassport provides OAuth2 server implementation. Install using composer, run migrations, generate encryption keys. Supports password grant, authorization code grant, and personal access tokens.
13. What are Gates and how are they used?
ModerateGates are Closures that determine if user can perform action. Registered in AuthServiceProvider using Gate::define(). Can use Gate::allows() or $user->can() to check authorization. Support custom parameters.
14. How do you implement custom authentication guards?
ModerateCustom guards extend Guard contract. Register in AuthServiceProvider using Auth::extend(). Implement user() and validate() methods. Configure in auth.php. Useful for specialized authentication needs.
15. How do you implement multi-authentication?
ModerateMulti-authentication uses different guards for different user types. Configure multiple providers and guards in auth.php. Use guard() method to specify guard. Support separate sessions and authentication logic.
16. What is policy auto-discovery?
ModeratePolicy auto-discovery automatically registers policies based on naming conventions. Can be disabled in AuthServiceProvider. Override getPolicyFor() for custom mapping. Supports policy discovery in packages.
17. How do you implement authentication events?
ModerateAuthentication events (Login, Logout, Failed, etc.) are dispatched automatically. Can be listened to using Event facade or subscribers. Useful for logging, notifications, or additional security measures.
18. How do you implement resource authorization?
ModerateResource authorization combines CRUD actions with policies. Use authorizeResource() in controllers. Maps controller methods to policy methods. Supports automatic authorization using middleware.
19. What are policy filters?
ModeratePolicy filters run before other policy methods. Define before() method in policy. Can grant or deny all abilities. Useful for super-admin scenarios or global authorization rules.
20. How do you implement token abilities in Sanctum?
ModerateToken abilities define permissions for API tokens. Specified when creating token. Check using tokenCan() method. Support multiple abilities per token. Can be combined with other authorization methods.
21. How do you implement advanced policy responses?
AdvancedPolicy responses can return Response objects instead of booleans. Use response() helper in policies. Support custom messages and status codes. Useful for detailed authorization feedback.
22. How do you implement custom user providers?
AdvancedCustom user providers implement UserProvider contract. Register in AuthServiceProvider using Auth::provider(). Implement retrieveById, retrieveByToken, updateRememberToken methods. Support non-database authentication.
23. How do you implement authentication rate limiting?
AdvancedRate limiting uses ThrottlesLogins trait or custom middleware. Configure attempts and lockout duration. Support IP-based and user-based throttling. Can customize decay time and storage.
24. How do you implement OAuth2 authorization code grant?
AdvancedAuthorization code grant requires client registration, authorization endpoint, token endpoint. Handle redirect URI, state parameter, PKCE. Support refresh tokens and token revocation. Implement scope validation.
25. How do you implement contextual authorization?
AdvancedContextual authorization considers additional parameters beyond user and model. Pass context to policy methods. Support complex authorization rules. Can use additional services or external APIs.
26. How do you implement passwordless authentication?
AdvancedPasswordless auth uses signed URLs or tokens sent via email/SMS. Implement custom guard and provider. Handle token generation and verification. Support expiration and single-use tokens.
27. How do you implement hierarchical authorization?
AdvancedHierarchical authorization handles nested permissions and inheritance. Implement tree structure for roles/permissions. Support permission propagation. Handle circular dependencies and performance.
28. How do you implement session authentication customization?
AdvancedSession authentication can be customized by extending guard, implementing custom user provider. Handle session storage, regeneration. Support custom session drivers and authentication logic.
29. How do you implement cross-domain authentication?
AdvancedCross-domain authentication requires coordinating sessions across domains. Handle CORS, shared tokens. Implement single sign-on. Support token forwarding and validation across domains.
30. How do you implement dynamic policy resolution?
AdvancedDynamic policy resolution determines policy class at runtime. Override getPolicyFor in AuthServiceProvider. Support multiple policy implementations. Handle policy resolution cache.