Session using api

  Mon 19 / 02 / 2024

  Posted by: Angel Thomas

Category : Laravel

Tags : laravel , backend , frontend

Description

my project is an expense tracker, its backend in working using laravel and front using bootstrap(no framework used)
using axios api calls communication is happening

issue:
i need to display username in index.html (at k.anderson) using session i tried my level best.
could you pls help me to solve this issue


View Code
                        LoginController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User; // Import the User model if not already imported
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function register(Request $request)
    {
        // Get data from the POST request
        $fullname = $request->input('fullname');
        $password = $request->input('password');
        $email = $request->input('email');
        $mobile = $request->input('mobile');

        // Hashing
        $hashedpassword = bcrypt($password); // Laravel helper function for password hashing

        // Perform the insertion into the 'users' table
        $user = new User();
        $user->fullname = $fullname;
        $user->email = $email;
        $user->phone = $mobile;
        $user->password = $hashedpassword;

        if ($user->save()) {
            $response = ['success' => true, 'message' => 'User registered successfully'];
            return response()->json($response, 200);
        } else {
            $response = ['success' => false, 'message' => 'Error: Unable to register user'];
            return response()->json($response, 500);
        }
    }


public function login(Request $request)
{
    if ($request->isMethod('post')) {
        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
            $response = ['success' => true, 'message' => 'Logged in'];
            return response()->json($response, 200);
        } else {
            $response = ['success' => false, 'message' => 'Incorrect email or password. Please try again.'];
            return response()->json($response, 401);
        }
    }

    $response = ['success' => false, 'message' => 'Invalid request.'];
    return response()->json($response, 400);
}


    // public function login(Request $request)
    // {
    //     if ($request->isMethod('post')) 
    //     {
            
    //         $username=$request->input('email');
    //         $password=$request->input('password');

    //         $user=User::where('email', $username)->first();

    //         if($user) {
    //             if (password_verify($password, $user->password)) 
    //             {
    //                 $response=['success' => true, 'message'=>'Logged in'];
    //                 return response()->json($response, 200);
    //             } else {
    //                 $response=['success' => false, 'message'=>'Incorrect password. Please try again.'];
    //                 return response()->json($response, 401);
    //             }
    //         } 
    //         else{
    //             $response=['success' => false, 'message'=>'User not found. Please check your username.'];
    //             return response()->json($response, 400);
    //         }
    //     }
    //}
}


api.php
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\ExpenseController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

 
Route::post('/register', [LoginController::class, 'register']);
Route::post('/login', [LoginController::class, 'login']);

Route::post('/store', [ExpenseController::class, 'store']);
                
  Answered by CDL

@Angel you are using Laravel with Bootstrap, and in this case, you don't need to use api.php file, web.php will work perfectly for you without too much time.

You can install any of the Laravel starter kit and it will give login functionality where you login the user and get login user with auth()->user();


Or if you still want to use api, watch my API course in Laravel.

https://bit.ly/41MZwBY

  Comment   Share
0 Likes   1 Comment


comment-author
Angel Thomas Author

I'm using laravel only for backend, no framework is used for front end connection between frontend and backend is done using Axios API calls(api.php) i haven't used web.php



Reply


commented 9 months ago
comment-author
Hadayat Niazi Super Admin

Was your problem resolved?



comment-author
Hadayat Niazi Super Admin

Ok that's fine to use api.php, then watch above attached video, which team teach you, how to build stand-alone api with login stuff.