angularjslaravellaravel-5jwt

Get token with JWT without attempting user credentials Laravel?


I'm currently working on a Angular (Front End) - Laravel (API) application. I need to know whether I can make a hybrid log in.

Currently I'm using a stored procedure for the log in. But then if the login was succesful I want to send a JWT token to the client side. Is this possible without calling the JWT attempt method? Does JWT has something like a JWT::getToken($mail); method? If I can't do this, are any other work arounds?

$mail = $request['email'];
$password = $request['password'];

$query = new Query("CALL SP_USER_LOG_IN(?,?, @outputMessage)");
$query->addParameter(1, $mail);
$query->addParameter(2, $password);

$outputMessage = DB::executeQuery($query);

if($outputMessage === null)
{
    //It means the login was successful
    $token = JWT::getToken($mail);

    return ["error" => NULL, "token" => $token];
}
else
{
    return ["error" => $outputMessage];
}

NOTE

I just found out about JWT and it looks like a really good solution since I'm working with a light angular application on the front end. But since I must use a stored procedure, I don't how to proceed.


Solution

  • Once you've verified the user, grab them from the database then create the token fromUser($user).

    if($outputMessage === null)
    {
        //It means the login was successful
        $user = User::where('email', $mail)->first();
        $token = JWT::fromUser($user);
    
        return ["error" => NULL, "token" => $token];
    }