Skip to content

Instantly share code, notes, and snippets.

@jcnevado
Created November 27, 2023 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcnevado/a12298f314784da70593910a417d4a90 to your computer and use it in GitHub Desktop.
Save jcnevado/a12298f314784da70593910a417d4a90 to your computer and use it in GitHub Desktop.
public function login_2fa() {
try {
//code...
$google2fa = new \PragmaRX\Google2FA\Google2FA();
$credentials = request(['email', 'verification_code']);
$user = User::where('email', $credentials['email'])->first();
$window = request('working_time', true) === false ? 5 : 0;
$valid = $google2fa->verifyKey($user->secret_2fa, $credentials['verification_code'], $window);
$status = $status2 = true;
$error = $error2 = '';
if ($valid === false) {
$status = false;
$error = 'Code Usuario incorrecto';
}
if (!$token = auth('api-jwt')->login($user)) {
$status = false;
$error = 'Login incorrecto';
return response()->json(['status' => false, 'error' => 'Unauthorized'], 401);
}
$user->login_retries = 0;
$user->status = "1";
$payload = auth('api-jwt')->payload();
if ($payload) {
$user->login_expiration = $payload['exp'];
}
$user->last_login = time();
$user->save();
//event(new SlackLoginSuccessEvent($credentials['email'], true));
return $this->respondWithToken($token);
} catch (\Throwable $th) {
return $th->getMessage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment