Simplest Way Create Laravel Guest User Account Restrict Modifications and Changes
Generally guest user account needed for testing purposes of any web applications on the Internet, Sometime guest account work for many types of web applications for testing behaviour of created code logic and methods and etc. That's why in this article I explaining and giving a logic with code for create Guest User Account and Restrict Modifications and Changes on Own Laravel Application.
In this way a developer can create a guest user account with very simple way for testig purposes of laravel application, User can't make changes and modifications in this way in your laravel application.
Why need this type of guest user account?
Ans: Sometimes a developer and programmer want to show own product to users for testing and for feedback related of own laravel application, that's why we need a guest user account.
In this type of account developer or programmer add some logics for
after this logic user can't Modified and Change in Testing purposes Laravel Application.
1. Setup Laravel Application
- Setup a Laravel Application if already done just ignore first step, If you you don't know how to create Laravel Application just copy given command and paste it on cmd or vs code terminal.
composer create-project laravel/laravel example-app
2. Create Controller
- Create a Controller for write logic for guest user account generally we write logic in admin controller, now creating a admin controller just write given command in cmd or vs code terminal.
php artisan make:controller A
dminController
3. Write Code
- Given below logic first Function Definition "public function edit($id)" also I passed single parameter
'$id'
like ID for educational purpose I choosed Edit Method of admin user profile edit. - Authentication and Authorization Check "
if (auth()->check() && auth()->user()->id == 3)
" this is checking user is authenticated yes or no if logged in then checking user Id if user id == 3 then code execute and id number 3 is identify as a guest user account with aleart message. - In my database phpmyadmin I have a user name with demo and demo's account id is (3) you can find id just visit phpmyadmin -> users -> table, For educational purpose i choosed id "3" you can choose what you want.
{ session()->flash('demo_error', 'You are using a guest account.'); return redirect()->back(); }
If user passed auth and logged in then using session flash and help of demo_error key now if user trying to edit admin profile then showing in view an alert message like this " 'You are using a guest account.' " also return redirect when execute logic function then redirects the user back to the previous page.
Note: Add this code above of all logic like If you add this code in "Edit Method" then write like this given below example:
public function edit($id)
{
if (auth()->check() && auth()->user()->id == 3) {
session()->flash('demo_error', 'You are using a guest account.');
return redirect()->back();
}
$admin = User::findOrFail($id);
return view('admin.backend.admin.edit', compact('admin'));
}
4. Add Code In View
- After logic writing show alert message of logic add few lines code in view where you want, In this article we use admin edit page, If user is logged in with $id "3" and trying to edit, update, delete then showing a alert message.
- I created a view file like given below but you can create like you want.
resources
│ └── views
│ ├── admin
│ │ └── backend
│ │ └── admin
│ │ └── edit.blade.php
│ └── body
│ ├── header.blade.php
│ ├── footer.blade.php
│ └── sidebar.blade.php
- In this structure 'admin' directory contains all admin-related views and body directory contains all admin-related body views, Now in edit.blade.php view file add above given code that's it.
Example of Error Message
Congratulations...
Now you successfully added logic for Guest user account .
Advantages and Disadvantages
Advantages | Disadvantages |
Auth checking and Controlling: If user not authenticated it flashes an error message and redirect to back or homepage. | Update code after every changes: Using this logic if user id is changed then change and update this logic also. |
findOrfail: using this with parametter $id it specified ID is not exist, and user can't logged in without valid user account. | Lack of Flexibility:This code assumes that only one user (with ID 3) has access to the editing functionality. |
Easy understandable: this code is easy understandable and clean code. | Hidden Error: If user is not authenticated and user trying to edit then a hidden error execute without any error flashes message, but If you want show a error message your can add. |
Flash an alert warning message: If user not fulfil it flashes an alert warning message. | Redirect to Back: User is not authorized then might be more appropriate to redirect them to a specific error page or to the homepage. |
"Frequently Asked Questions."
Conclusion
In conclusion, the provided code segment illustrates an edit function within a Laravel application. It begins by checking if the user is authenticated and if their ID matches a predefined value (in this case, 3), indicating a guest account. If this condition is met, a flash message is set to notify the user, and they are redirected back.
However, if the user is not a guest, the function proceeds to fetch the user details based on the provided ID and renders a view for editing the user information. This approach ensures that only authenticated users with appropriate permissions can access the edit functionality, maintaining security and control over user interactions.
Files
What's Your Reaction?