use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Support\Facades\Password;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\User;
class AuthController extends Controller
{
public function signup(Request $request) {
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6|confirmed',
]);
$validatedData['password'] = Hash::make($validatedData['password']);
if(User::create($validatedData)) {
return response()->json(null, 201);
}
return response()->json(null, 404);
}
public function login(Request $request) {
$request->validate([
'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([
'user' => $user,
'access_token' => $user->createToken($request->email)->plainTextToken
], 200);
}
public function logout(Request $request) {
// Revoke the token that was used to authenticate the current request
$request->user()->currentAccessToken()->delete();
//$request->user->tokens()->delete(); // use this to revoke all tokens (logout from all devices)
return response()->json(null, 200);
}
public function getAuthenticatedUser(Request $request) {
return $request->user();
}
public function sendPasswordResetLinkEmail(Request $request) {
$request->validate(['email' => 'required|email']);
$status = Password::sendResetLink(
$request->only('email')
);
if($status === Password::RESET_LINK_SENT) {
return response()->json(['message' => __($status)], 200);
} else {
throw ValidationException::withMessages([
'email' => __($status)
]);
}
}
public function resetPassword(Request $request) {
$request->validate([
'token' => 'required',
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
]);
$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) use ($request) {
$user->forceFill([
'password' => Hash::make($password)
])->setRememberToken(Str::random(60));
$user->save();
event(new PasswordReset($user));
}
);
if($status == Password::PASSWORD_RESET) {
return response()->json(['message' => __($status)], 200);
} else {
throw ValidationException::withMessages([
'email' => __($status)
]);
}
}
}