Master Laravel 11 API Authentication with Passport: Step-by-Step Guide

Поделиться
HTML-код
  • Опубликовано: 7 янв 2025

Комментарии • 8

  • @sandhyamaurya4770
    @sandhyamaurya4770 6 дней назад

    Need to logout functionality

  • @eaad5101
    @eaad5101 2 месяца назад

    How about function logout?
    i've try with
    $token = $request->user()->tokens();
    $token->revoke();
    but still, it doesn't work. Any solution?

  • @Arya-si3ur
    @Arya-si3ur 2 месяца назад

    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?

    • @ryansumbele3552
      @ryansumbele3552 8 дней назад

      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

    • @Arya-si3ur
      @Arya-si3ur 8 дней назад

      @@ryansumbele3552 i downloaded (laravel idehelper) and it worked

    • @ryansumbele3552
      @ryansumbele3552 8 дней назад

      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);
      }