GuilhermeSO
GuilhermeSO

Reputation: 89

JWT Authentication / Laravel

Im new at Laravel, i made a login page for my backend application(its working fine) and then i did the JWT documentation to install and begin to use(https://jwt-auth.readthedocs.io/en/develop/). In my Controllers i used after:

public function __construct()
{
    $this->middleware('auth:api', ['except' => ['login']]);
}

Now everytime i try to enter my same login i cant. How can i enter my login now with JWT?

Upvotes: 1

Views: 722

Answers (1)

Citizen
Citizen

Reputation: 12927

If you're using Laravel 8, the newly recommended route is to use Laravel's Sanctum feature:

https://laravel.com/docs/8.x/sanctum

For this, if you want stateful logins, you simply post to any controller that then makes an Auth::attempt call like this:

class AuthController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @throws ValidationException
     */
    public function store(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');
        if (!Auth::attempt($credentials, true)) {
            throw ValidationException::withMessages([
                'email' => ['The provided credentials are incorrect.'],
            ]);
        }
    }
}

I recently set up my login system successfully on React using this package:

https://github.com/koole/react-sanctum

For Stateless, you can follow the documentation here:

https://laravel.com/docs/8.x/sanctum#api-token-authentication

Upvotes: 1

Related Questions