1. Go to this page and download the library: Download avn/auth-by-token library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
avn / auth-by-token example snippets
/**
* @Route("/api/v1/public/create-token-and-send-by-email", methods={"GET"})
*/
public function authByEmail(
Request $httpRequest,
MailerInterface $mailer,
UserRepository $userRepository,
CreateTokenAction $createTokenAction,
ParameterBagInterface $parameterBag,
UrlGeneratorInterface $urlGenerator
) {
$user = $userRepository->findOneBy(['email' => $httpRequest->toArray()['email'] ]);
if (is_null($user)) {
throw new \Exception(sprintf('User[email: %s] not found', $httpRequest->toArray()['email']));
}
$token = $createTokenAction->execute($user->getCode()->toBase32());
$url = $parameterBag->get('app.host')
. $urlGenerator->generate('app_login_from_email', ['token' => $token])
;
$mailer->send(
(new Email())
->from($parameterBag->get('app.email.from'))
->to($user->getEmail())
->subject('Now you can login')
->text('Here is your link to login')
->html(sprintf('<p>You can login. Just follow the <a href="%s">link</a>.</p>', $url))
);
return new Response();
}