Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
API Controller + API Route for Integrate Jetstream + Adalo
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/login', 'App\Http\Api\ApiAuthController@login')->name('login.api');
Route::post('/register','App\Http\Api\ApiAuthController@register')->name('register.api');
Route::post('/logout', 'App\Http\Api\ApiAuthController@logout')->name('logout.api');
<?php
namespace App\Http\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
class ApiAuthController extends Controller
{
public function register (Request $request)
{
\Validator::make($request->all(), [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6'],
])->validate();
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => \Hash::make($request->password),
]);
return response()->json([
'status' => 'Success',
'message' => 'Login Success',
'data' => [
'user' => $user->id,
'token' => $user->createToken('Mobile Token',['mobile'])->plainTextToken
]
], 200);
}
public function login (Request $request) {
$this->validate($request, [
'email' => 'required|email',
'password' => 'required'
]);
$user = User::where('email', $request->email)->first();
if (!$user || !\Hash::check($request->password, $user->password)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
return response()->json([
'status' => 'Success',
'message' => 'Login Success',
'data' => [
'user' => $user->id,
'token' => $user->createToken('Mobile Token',['mobile'])->plainTextToken
]
], 200);
}
public function logout(Request $request)
{
$request->user()->tokens()->delete();
return response()->json('logout successful', 201);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment