Authcontroller
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class AuthController extends Controller
{
//-----------------------------register users ---------------------------------
public function register(Request $request)
{
$password = password_hash($request->password, PASSWORD_DEFAULT);
$users_count= User::where('username',$request->username)->get();
$email_count= User::where('email',$request->email)->get();
$credentials = request(['username', 'password']);
dd(auth()->attempt($credentials));
if(count($users_count)>0){
$data = [
'status' => 'username found',
];
}
else {
if (count($email_count)>0){
$data = [
'status' => 'email is found',
];
}
else {
$user = User::create(
[
'username'=>$request->username,
'password'=>$password,
'email'=>$request->email,
'fullname'=>$request->fullname,
'natId'=>$request->nat_id,
'role'=>'user'
]);
if($user){
$credentials = request(['username', 'password']);
if (! $token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
else{
$data = [
'username'=>$request->username,
'password'=>$password,
'email'=>$request->email,
'fullname'=>$request->fullname,
'natId'=>$request->nat_id,
'status' => 'success',
'token' => $token
];}
}
else{
$data = [
'status' => 'failed',
];
}
}
}
return $data;
}
}
Curious Crossbill