Login Registration Form Restful Api Using Laravel 9 and Vue 3
Table of Contents
Introduction
This tutorial will guide you through the process of creating a login and registration form using a RESTful API with Laravel 9 and Vue 3. By the end of this tutorial, you will have a fully functional application that allows users to register and log in, utilizing the power of Laravel for the backend and Vue.js for the frontend.
Step 1: Set Up Laravel Backend
-
Install Laravel
- Go to the official Laravel website.
- Copy the installation command for Laravel 9.
- Open your command line interface (CLI) and navigate to your desired directory.
- Create a new Laravel project:
composer create-project --prefer-dist laravel/laravel KMS_backend
-
Create Database
- Use a database management tool (like phpMyAdmin) to create a new database named
DB_KMS. - Update the
.envfile in your Laravel project with your database name:DB_DATABASE=DB_KMS
- Use a database management tool (like phpMyAdmin) to create a new database named
-
Run Migrations
- In your CLI, run:
php artisan migrate
- In your CLI, run:
-
Set Up Routes
- Open the
routes/api.phpfile. - Define routes for registration and login:
Route::post('register', [RegisterController::class, 'store']); Route::post('login', [LoginController::class, 'check']);
- Open the
Step 2: Create Controllers and Models
-
Generate Controllers
- Use the following commands to create the necessary controllers:
php artisan make:controller RegisterController php artisan make:controller LoginController
- Use the following commands to create the necessary controllers:
-
Create User Model
- Ensure you have a User model already present in the
app/Models/User.php.
- Ensure you have a User model already present in the
-
Implement Registration Logic
- In
RegisterController, implement thestoremethod to handle user registration:public function store(Request $request) { $request->validate([ 'name' => 'required', 'email' => 'required|email', 'password' => 'required|min:6', ]); $user = User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => Hash::make($request->password), ]); return response()->json(['status' => true, 'message' => 'Registration successful']); }
- In
-
Implement Login Logic
- In
LoginController, implement thecheckmethod to handle user login:public function check(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { return response()->json(['status' => true, 'message' => 'Login successful']); } return response()->json(['status' => false, 'message' => 'Invalid credentials']); }
- In
Step 3: Set Up Vue 3 Frontend
-
Install Vue CLI
- If you haven't installed Vue CLI, do so with:
npm install -g @vue/cli
- If you haven't installed Vue CLI, do so with:
-
Create Vue Project
- Inside your main project directory, create a Vue project:
vue create KMS_frontend
- Inside your main project directory, create a Vue project:
-
Install Axios for API Calls
- Navigate into your Vue project directory and install Axios:
cd KMS_frontend npm install axios
- Navigate into your Vue project directory and install Axios:
-
Create Registration Component
- Create a new component
Register.vuein thesrc/componentsdirectory:<template> <form @submit.prevent="saveData"> <input v-model="name" placeholder="Name" required /> <input v-model="email" placeholder="Email" required /> <input v-model="password" type="password" placeholder="Password" required /> <button type="submit">Register</button> </form> </template> <script> import axios from 'axios'; export default { data() { return { name: '', email: '', password: '' }; }, methods: { async saveData() { const response = await axios.post('http://your-api-url/api/register', { name: this.name, email: this.email, password: this.password }); alert(response.data.message); } } }; </script>
- Create a new component
-
Create Login Component
- Create another component
Login.vuesimilar to the registration component but for logging in.
- Create another component
Step 4: Integrate Components into Vue Router
-
Set Up Vue Router
- In your Vue project, configure the router to include routes for the registration and login components.
-
Test the Application
- Run both the Laravel server and the Vue application:
php artisan serve # for Laravel npm run serve # for Vue - Access the application in your browser to test the registration and login functionalities.
- Run both the Laravel server and the Vue application:
Conclusion
In this tutorial, you have learned how to set up a login and registration system using Laravel for the backend and Vue.js for the frontend. You created the necessary routes, controllers, and models in Laravel, and built the user interface and API calls in Vue. You can now expand this project further by adding features such as user profile management, password reset, and more.