#Api laravel issue

37 messages · Page 1 of 1 (latest)

austere kernel
#

I m using laravel api with angular 19 .if someone has some knowledge help me pls .i set everything good,but it isnt working why?

pale shuttle
#

pleasea share your laravel code? from the last two error messages, seems like the path is not defined

austere kernel
#

Ok

astral solar
#

Also, just to be sure, run php artisan optimize:clear. Happens a lot that people somehow cache routes, which would then cause issues as new routes wouldn't be registered. So, run that and then never run any of those caching commands locally again.

austere kernel
#

AuthController.php:
<?php
namespace App\Http\Controllers;

use App\Models\User; // Corrected model to "User"
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash; // Import the Hash facade

class AuthController extends Controller
{
public function signup(Request $request)
{
// Validate incoming request data
$validatedData = $request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6|confirmed', // Use the "confirmed" rule to match password_confirmation
]);

#

Create new user
$user = User::create([ // Use the User model
'name' => $validatedData['name'],
'email' => $validatedData['email'],
'password' => Hash::make($validatedData['password']), // Only hash the password
]);

    // Return success response
    return response()->json(['message' => 'User registered successfully!'], 201);
}

public function login(Request $request)
{
    // Validate incoming request data
    $validatedData = $request->validate([
        'email' => 'required|email',
        'password' => 'required',
    ]);

    // Find user by email
    $user = User::where('email', $validatedData['email'])->first();

    // Check credentials
    if (!$user || !Hash::check($validatedData['password'], $user->password)) {
        return response()->json(['message' => 'Invalid credentials'], 401);
    }

    // Return success response
    return response()->json(['message' => 'Login successful'], 200);
}

}

#

user.php
<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
use HasFactory, Notifiable;

/
 * The attributes that are mass assignable.
 *
 * @var list<string>
 */
protected $fillable = [
    'name',
    'email',
    'password',
];

/
 * The attributes that should be hidden for serialization.
 *
 * @var list<string>
 */
protected $hidden = [
    'password',
    'remember_token',
];

/
 * The attributes that should be cast.
 *
 * @return array<string, string>
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

}

astral solar
#

The problem is probably routes, not the controllers.

austere kernel
#

I will provide routes

#

auth.service.ts:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
providedIn: 'root',
})
export class AuthService {
private baseUrl = 'http://localhost:8000/api';

constructor(private http: HttpClient) {}

signup(data: any) {
return this.http.post(${this.baseUrl}/signup, data);
}

login(data: any) {
return this.http.post(${this.baseUrl}/login, data);
}
}

#

Now?

#

@astral solar What do you think?

astral solar
#

I still see no route definitions? 🤷‍♂️

#

Also, please take note of the #rules, it's explained how to share code properly using code-snippets

austere kernel
#

Ok, where i define the routes. I cant understand

#

i m still beginner

#

@astral solar explain to me

astral solar
austere kernel
#

Ah sorry i remember

#

<?php
use App\Http\Controllers\AuthController;

Route::post('/signup', [AuthController::class, 'signup']);
Route::post('/login', [AuthController::class, 'login']);

#

The routes are defined here

#

@astral solar

astral solar
#

And in which file are those defined?

austere kernel
#

@astral solar

pale shuttle
austere kernel
pale shuttle
#
  1. make sure you didn't cache anything
    • run php artisan optimize:clear to clear all caches
  2. make sure the routes are registered
    • run php artisan route:list to list out all the routes registered at laravel
austere kernel
#

After running route:liste @pale shuttle

#

POST api/login .................................................................... AuthController@login
POST api/signup .................................................................. AuthController@signup

#

But it isnt updating the data base in xampp

pale shuttle
#

so the routes are accessible right now?

austere kernel
pale shuttle
#

so which solve the original issue? clear caches or registering the api route?

austere kernel