Mada za sehemu hiiBuilding the back-end using a frameworkMada 3
- Implementing routing and handling of user requests
- Implementing interaction with databases
- Implementing authentication and authorisation
Authentication and Authorisation
Authentication and Authorisation are two essential security processes that protect web applications. Authentication verifies who a user is (like checking a password), while Authorisation controls what an authenticated user can do (like allowing only admins to delete records).
Authentication is the process of confirming a user's identity before granting access to a system. It answers the question: "Are you who you claim to be?"
In web applications, authentication typically works by requiring users to provide credentials—usually a username or email and a password. When users enter these credentials, the system checks them against stored user records. If the credentials match, the system creates an authenticated session, allowing the user to access protected areas of the application.
Without authentication, anyone could access private pages, view sensitive data, or perform actions they should not be allowed to perform.
Authorisation determines what an authenticated user is permitted to do within the application. It answers the question: "What are you allowed to do?"
For example, in a school portal:
- A student can view their own marks and notes
- A teacher can add marks and view class records
- An administrator can manage all user accounts
Authorisation works after authentication. Even if a user has successfully logged in, they may still be restricted from certain pages or actions based on their role.
Laravel Breeze is a starter kit that provides ready-made components for user registration, login, logout, and password reset. It follows security best practices and is easy to set up.
Installation Steps
-
Install Laravel Breeze by running:
composer require laravel/breeze --dev -
Run the Breeze installer:
php artisan breeze:installWhen prompted, choose "Blade with Alpine" as the stack, select "no" for dark mode, and choose "Pest" as the testing framework.
-
Install JavaScript dependencies:
npm install && npm run dev -
Run migrations to create the users table:
php artisan migrate -
Start the development server:
php artisan serveThen visit
http://127.0.0.1:8000/registerto see the registration form.
After these steps, users can register, log in, and log out. The system automatically creates routes for /register, /login, and /dashboard.
Role-Based Access Control (RBAC) is a method of restricting system access based on user roles. Common roles include:
- User (regular user with basic access)
- Teacher (can manage academic data)
- Admin (full system access)
Implementing RBAC in Laravel
Step 1: Add a role column to the users table
Create a migration to add the role field:
php artisan make:migration add_role_to_users_table --table=users
Edit the migration file:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('role')->default('user');
});
}
Run the migration:
php artisan migrate
Step 2: Create Admin Middleware
Create middleware to check if a user is an admin:
php artisan make:middleware AdminMiddleware
Edit the middleware file (app/Http/Middleware/AdminMiddleware.php):
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::user()->role === 'admin') {
return $next($request);
}
return redirect('/dashboard')->with('error', 'Access denied.');
}
Step 3: Register the Middleware
In app/Http/Kernel.php, add to the $routeMiddleware array:
'admin' => \App\Http\Middleware\AdminMiddleware::class
Step 4: Protect Admin Routes
In routes/web.php:
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('/admin', [AdminController::class, 'index'])
->name('admin.dashboard');
});
Step 5: Assign Admin Role
Use Laravel Tinker to promote a user to admin:
php artisan tinker
>>> $user = App\Models\User::find(1);
>>> $user->role = 'admin';
>>> $user->save();
Consider a Tanzanian secondary school portal where:
- Students can access
/dashboardto view their results - Teachers can access
/marksto enter student marks - Administrators can access
/adminto manage all users
The route configuration would look like:
// All authenticated users can access dashboard
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware('auth');
// Only teachers can access marks
Route::get('/marks', [MarkController::class, 'index'])
->middleware(['auth', 'teacher']);
// Only admins can access admin panel
Route::get('/admin', [AdminController::class, 'index'])
->middleware(['auth', 'admin']);
If a student tries to visit /admin, the AdminMiddleware checks their role. Since they are not an admin, they are redirected back to /dashboard with an error message.
- Authentication verifies user identity through login credentials
- Authorisation controls what authenticated users can access based on roles
- Middleware is code that runs between a request and a response to enforce rules
- Roles define user categories with different permission levels
- Laravel Breeze provides ready-made authentication functionality
- Always use hashed passwords (Laravel does this automatically with Bcrypt)
- Validate all user input on both front-end and back-end
- Use middleware to protect sensitive routes
- Never store plain-text passwords
- Log out users after inactivity when dealing with sensitive data
In Tanzania, many schools are adopting digital systems for managing student records, fees, and results. When a parent checks their child's marks through a school portal, authentication ensures only authorized parents can view their own child's data, while authorisation ensures students cannot modify their own marks and only administrators can manage user accounts. This protects sensitive information and maintains the integrity of academic records.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Create secure login and registration systems.
- Implement role-basedaccess control for different user types.
Tathmini ya matokeo
A secure application that authenticates users, handles sessions or tokens and enforces role-based access.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how
- authentication and authorization in securing applications is carried out.
Principles: The student should explain the principles of:
- security best practices like hashing, salting, and avoiding hardcoding credentials.
Theories: The student should explain:
- the concepts of techniques of Authentication and Authorization
- session management, token-based authentication.
Circumstantial knowledge:
Detailed knowledge about:
- real-world Authentication and Authorization challenges like brute force attacks, session hijacking
Swali
What is the main purpose of authentication in a web application?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado