when i paste the functions in the "AuthController.php" file it gives me error Undefined method 'attempt'.intelephense(P1013) Undefined method 'user'.intelephense(P1013) do you have any soulution?
please did you solve this problem? i'm facing thesame issue, if you replace 'auth()' with 'Auth', it kind of solves that problem, but then i run into another error, 'undefined method createToken' in login functon
Need to logout functionality
How about function logout?
i've try with
$token = $request->user()->tokens();
$token->revoke();
but still, it doesn't work. Any solution?
token,, not tokens
when i paste the functions in the "AuthController.php" file it gives me error
Undefined method 'attempt'.intelephense(P1013)
Undefined method 'user'.intelephense(P1013)
do you have any soulution?
please did you solve this problem? i'm facing thesame issue, if you replace 'auth()' with 'Auth', it kind of solves that problem, but then i run into another error, 'undefined method createToken' in login functon
@@ryansumbele3552 i downloaded (laravel idehelper) and it worked
i found a solution , your register and login function in your authcontroller should look like this:
public function register(Request $request)
{
$registerData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8|confirmed'
]);
$registerData['password'] = Hash::make($request->password);
$user = User::create($registerData);
$accessToken = $user->createToken('authToken')->accessToken;
return response()->json([
'user' => $user,
'access_token' => $accessToken,
],201);
}
public function login(Request $request)
{
$loginData = $request->validate([
'email' => 'required|string|email',
'password' => 'required|string',
]);
$user = User::where('email', $loginData['email'])->first();
if (!$user || !Hash::check($loginData['password'], $user->password)) {
return response()->json(['message' => 'Invalid credentials'], 401);
}
$accessToken = $user->createToken('authToken')->accessToken;
return response()->json([
'user' => $user,
'access_token' => $accessToken,
],201);
}