I am very new at APIs and stuff. I would appreciate it if anyone can help me to understand or guide me on how to accomplish the following.
I have a contact form in Wordpress with the Contact-form 7 plugin. It is a basic contact form to capture enquiries with just 3 fields i.e. Name, Contact Number & Type of Enquiry. The data is sent to Laravel Application, and saves in Lead Management where the followups are taken care of.
I referred to some tutorials online to accomplish the following.
My issue is, I am not able to get the data in my Laravel application. While testing in postman, the api works when I manually insert the type as bearer and set the token. Or when the application is logged-in in the same browser.
I dont know how to authenticate the user.
My API Routes
//API route for register new user
Route::post('/register', [App\Http\Controllers\API\AuthController::class, 'register']);
//API route for login user
Route::post('/login', [App\Http\Controllers\API\AuthController::class, 'login']);
Route::group(['middleware' => ['auth:sanctum']], function() {
Route::get('/profile', function(Request $request) {
return auth()->user();
});
// API route for Cf7 to system
Route::resource('externalsales', App\Http\Controllers\API\SalesController::class);
// API route for logout user
Route::post('/logout', [App\Http\Controllers\API\AuthController::class, 'logout']);
});
My AuthController
class AuthController extends Controller
{
public function register(Request $request)
{
$validator = Validator::make($request->all(),[
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8'
]);
if($validator->fails()){
return response()->json($validator->errors());
}
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password)
]);
$token = $user->createToken('auth_token')->plainTextToken;
return response()
->json(['data' => $user,'access_token' => $token, 'token_type' => 'Bearer', ]);
}
public function login(Request $request)
{
if (!Auth::attempt($request->only('email', 'password')))
{
return response()
->json(['message' => 'Unauthorized'], 401);
}
$user = User::where('email', $request['email'])->firstOrFail();
$token = $user->createToken('auth_token')->plainTextToken;
return response()
->json(['message' => 'Hi '.$user->name.', welcome to home','access_token' => $token, 'token_type' => 'Bearer', ]);
}
// method for user logout and delete token
public function logout()
{
auth()->user()->tokens()->delete();
return [
'message' => 'You have successfully logged out and the token was successfully deleted'
];
}
}
My SalesController
public function index()
{
$data = SalesManagement::latest()->get();
return response()->json([SalesResource::collection($data), 'Leads fetched.']);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(),[
'fullname' => 'required|string|max:255',
'mobile' => 'required',
'description' => 'required'
]);
if($validator->fails()){
return response()->json($validator->errors());
}
$sales = SalesManagement::create([
'fullname' => $request->fullname,
'mobile' => $request->mobile,
'description' => $request->description,
]);
$token = $request->bearerToken();
return response()->json(['Lead created successfully.','access_token' => $token, 'token_type' => 'Bearer', new SalesResource($sales)]);
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.