I'm creating an API for a table called usuarios, everything went all right when i registered a new user through postman, but when I try to login, the Auth::attempt, passing a username(not an email) and a password, it returns false, returning code 401, Unauthorized. I think it is because i changed deleted the default users table and created a new one. Can anybody help me? Maybe if I changed the table and columns for Authentication it would work, but I don't know how to do it.
Code for my AuthController:
class AuthController extends Controller
{
public function signup(Request $request)
{
$request->validate([
'nome' => 'required|string',
'email' => 'required|string|email|unique:usuarios',
'cpf' => 'required|string|unique:usuarios',
'username' => 'required|string|unique:usuarios',
'dtNasc' => 'string',
'senha' => 'required|string|confirmed'
]);
$user = new Usuario([
'nome' => $request->nome,
'email' => $request->email,
'cpf' => $request->cpf,
'username' => $request->username,
'dtnasc' => $request->dtnasc,
'senha' => Hash::make($request->senha)
]);
$user->save();
return response()->json([
'message' => 'Successfully created user!'
], 201);
}
public function login(Request $request)
{
$request->validate([
'username' => 'required|string',
'senha' => 'required|string',
'remember_me' => 'boolean'
]);
$credentials = request(['username', 'senha']);
if(!Auth::attempt($credentials))
return response()->json([
'message' => 'Unauthorized'
], 401);
$user = $request->user();
$tokenResult = $user->createToken('Personal Access Token');
$token = $tokenResult->token;
if ($request->remember_me)
$token->expires_at = Carbon::now()->addWeeks(1);
$token->save();
return response()->json([
'access_token' => $tokenResult->accessToken,
'token_type' => 'Bearer',
'expires_at' => Carbon::parse(
$tokenResult->token->expires_at
)->toDateTimeString()
]);
}
public function logout(Request $request)
{
$request->user()->token()->revoke();
return response()->json([
'message' => 'Successfully logged out'
]);
}
public function user(Request $request)
{
return response()->json($request->user());
}
}