Last active
April 19, 2021 10:13
-
-
Save imknight/079268a8e648d50248feed872cf2b804 to your computer and use it in GitHub Desktop.
API Controller + API Route for Integrate Jetstream + Adalo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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