3-parameter Json Web Token (JWT) authentication?

0

I am doing a login system with Laravel using JWT, however I need to pass 3 parameters, being they an identifier, username and password. I can get these parameters but I can not authenticate with the bank. It does user verification and password, but from that first parameter (the handle, which is needed to mount the menu) it does not check. Has anyone used JWT passing 3 parameters? How can I check these 3 parameters?

I'm using link . Here is an excerpt from my code:

public function authenticateJson(Request $request) {
    // pega as credenciais para o login
    $credentials = $request->only('login', 'password');
    $customClaims = ['ep_chave' => $request->only('ep_chave')];
    try {
        // verifica o login e cria o token
        if (! $token = JWTAuth::attempt($credentials, $customClaims)) {
            return response()->json(['error' => 'Login ou senha inválidos'], 401);
        }
    } catch (JWTException $e) {
        //Erro para Criar o token
        return response()->json(['error' => 'Não foi possível criar o token'], 500);
    }

    // Caso tudo ok retorna o token
    return response()->json(compact('token'));
}
    
asked by anonymous 27.02.2018 / 16:43

1 answer

0

I was able to solve the problem, here is the code that worked for me. Thanks to all who have volunteered to help.

public function authenticateJson(Request $request) {
    // pega as credenciais para o login
    $credentials = $request->only('login', 'password', 'ep_chave');

    // faz a validação dos campos recebidos
    $validator = Validator::make($credentials, [
        'ep_chave' => 'required',
        'login' => 'required',
        'password' => 'required',
    ]);

    // se o campo não for plenamente validado retorna a mensagem de erro
    if ( $validator->fails() ) {  
        return response()->json($validator->errors()->getMessages(), 400);
    }

    try {
        // verifica o login e cria o token
        if (! $token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'Login ou senha inválidos'], 401);
        }
    } catch (JWTException $e) {
        //Erro para Criar o token
        return response()->json(['error' => 'Não foi possível criar o token'], 500);
    }

    // Caso tudo ok retorna o token
    return response()->json(compact('token'));
}
    
27.02.2018 / 19:40