Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a naming convention for the JWT token cookie? #2269

Open
webcrawlersites opened this issue Oct 29, 2024 · 0 comments
Open

Is there a naming convention for the JWT token cookie? #2269

webcrawlersites opened this issue Oct 29, 2024 · 0 comments

Comments

@webcrawlersites
Copy link

Does the token always have to be named token?

I am working with JWT Auth in Laravel and I was sending the token back through a cookie and named it "authToken", but the token wasn't being recognized. I changed the name of my cookie to just "token" and the token is being recognized perfectly. I was wondering if there is a naming convention for the token? Does it only have to be named "token" and can't have any other name?

/**
     * Get a JWT via given credentials.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');

        if (! $token = auth()->attempt($credentials)) {
            return response()->json([
                "message" => "Error logging in: Invalid credentials."
            ], 401);
        }

        // Set the JWT token in an HttpOnly cookie
        return response()->json([
            "message" => "Login successful!"
        ], 200)->cookie('token', $token, auth()->factory()->getTTL() * 60, '/', null, true, true, false, 'Strict');
    } // Changed from authToken

Your environment

Q A
Bug? Maybe
New Feature? no
Framework Laravel
Framework version 11
Package version 2.1
PHP version 8

Expected behaviour

The token should be recognized with any name.

Actual behaviour

The cookie is only working if named token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant