Created
December 16, 2013 15:40
-
-
Save Graceas/7989037 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Init phone verification | |
* | |
* @return Response | |
* @throws Exception | |
*/ | |
public function phoneVerificationAction() | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$request = $this->getRequest(); | |
$session = $request->getSession(); | |
$user = $this->getUser(); | |
/* @var $user Users */ | |
if (!$user->getPhone()) { | |
throw new Exception('Logic Exception'); | |
} | |
if ($user->getIsVerifiedPhone()) { | |
throw new Exception('Logic Exception'); | |
} | |
if (!$user->getPhoneCode()) { | |
$twilio = $this->get('twilio.api'); | |
/* @var $twilio \Vresh\TwilioBundle\Service\TwilioWrapper */ | |
$code = rand(100000, 999999); | |
$user->setPhoneCode($code); | |
$user->setPhoneCodeLastSend(new \DateTime()); | |
$em->persist($user); | |
$em->flush(); | |
//send sms witch validation code | |
if (self::ENABLE_SEND_SMS) { | |
$message = $twilio->account->sms_messages->create( | |
'+18562824105', // From a valid Twilio number | |
$user->getPhone(), // Text this number | |
"Validation Code " . $code | |
); | |
} | |
} | |
$now = new \DateTime(); | |
$diff = $now->getTimestamp() - $user->getPhoneCodeLastSend()->getTimestamp(); | |
return $this->render('MxupFrontendBundle:Profile:settings.phone.validation.html.twig', array( | |
'error' => $request->get('error', false), | |
'next_resend_after' => 120 - $diff //2 min - diff | |
)); | |
} | |
/** | |
* Validate phone action | |
* | |
* @return \Symfony\Component\HttpFoundation\RedirectResponse | |
*/ | |
public function phoneValidateAction() | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$request = $this->getRequest(); | |
$session = $request->getSession(); | |
$user = $this->getUser(); | |
/* @var $user Users */ | |
$code = $request->get('code', null); | |
if ($user->getPhoneCode() != $code) { | |
return $this->redirect($this->generateUrl('mxup_frontend_profile_phone_verification', array('error' => '1'))); | |
} | |
$user->setPhoneCode(''); | |
$user->setIsVerifiedPhone(true); | |
$em->persist($user); | |
$em->flush(); | |
$session->getFlashBag()->add('notice', 'Your phone successfully validated'); | |
return $this->redirect($this->generateUrl('mxup_frontend_profile_settings')); | |
} | |
/** | |
* Re-send phone verification code | |
* | |
* @return \Symfony\Component\HttpFoundation\RedirectResponse | |
* @throws Exception | |
*/ | |
public function phoneValidateResendAction() | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$request = $this->getRequest(); | |
$session = $request->getSession(); | |
$user = $this->getUser(); | |
/* @var $user Users */ | |
$last_send_time = $user->getPhoneCodeLastSend(); | |
/* @var $last_send_time \DateTime */ | |
if (!$user->getPhone()) { | |
throw new Exception('Logic Exception'); | |
} | |
if ($user->getIsVerifiedPhone()) { | |
throw new Exception('Logic Exception'); | |
} | |
if (!$user->getPhoneCode() || !$last_send_time) { | |
throw new Exception('Logic Exception'); | |
} | |
$now = new \DateTime(); | |
$diff = $now->getTimestamp() - $last_send_time->getTimestamp(); | |
if ($diff < 120) { //2 min | |
throw new Exception('Logic Exception'); | |
} | |
$twilio = $this->get('twilio.api'); | |
/* @var $twilio \Vresh\TwilioBundle\Service\TwilioWrapper */ | |
$code = rand(100000, 999999); | |
$user->setPhoneCode($code); | |
$user->setPhoneCodeLastSend(new \DateTime()); | |
$em->persist($user); | |
$em->flush(); | |
//send sms witch validation code | |
if (self::ENABLE_SEND_SMS) { | |
$message = $twilio->account->sms_messages->create( | |
'+18562824105', // From a valid Twilio number | |
$user->getPhone(), // Text this number | |
"Validation Code " . $code | |
); | |
} | |
return $this->redirect($this->generateUrl('mxup_frontend_profile_phone_verification')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'MxupFrontendBundle:Profile:index.html.twig' %} | |
{% block stylesheets %} | |
{{ parent() }} | |
{% endblock %} | |
{% block profile_content %} | |
<div class="border-block content-item"> | |
<form class="form-horizontal" action="{{ path('mxup_frontend_profile_phone_validate') }}" method="POST"> | |
<input type="text" name="code" placeholder="XXXXXX" autocomplete="false"> | |
<div class="form-submit-item"> | |
<a href="{{ path('mxup_frontend_profile_phone_resend') }}" class="re-send" onclick="return false;"> | |
<span class="counter" {% if next_resend_after <= 0 %}style="display: none"{% endif %}>{{ next_resend_after }}</span> | |
<span class="text"> | |
{% if next_resend_after <= 0 %} | |
{% trans %}re-send now{% endtrans %} | |
{% else %} | |
{% trans %}'s for re-send{% endtrans %} | |
{% endif %} | |
</span> | |
</a> | |
<button type="submit" class="btn purple-btn b-radius10">{% trans %}validate{% endtrans %}</button> | |
</div> | |
{% if error %} | |
<div class="hint error"> | |
{% trans %}This Code Not Corrected{% endtrans %} | |
</div> | |
{% endif %} | |
</form> | |
</div> | |
{% endblock %} | |
{% block javascripts %} | |
{{ parent() }} | |
{% javascripts '@MxupFrontendBundle/Resources/public/js/phone_verification.js' output='js/phone_verification.js' %} | |
<script src="{{ asset_url }}"></script> | |
{% endjavascripts %} | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
var counter_id = setInterval(function(){ | |
var seconds = parseInt($('.re-send').find('.counter').text()); | |
if (seconds <= 0) { | |
clearInterval(counter_id); | |
$('.re-send').find('.counter').hide(); | |
$('.re-send').find('.text').text('re-send now'); | |
$('.re-send').attr('onclick', 'return true;'); | |
return; | |
} | |
$('.re-send').find('.counter').text(--seconds); | |
$('.re-send').find('.text').text('\'s for re-send'); | |
}, 1000); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mxup_frontend_profile_phone_verification: | |
pattern: /phone/verification | |
defaults: { _controller: MxupFrontendBundle:Profile:phoneVerification } | |
mxup_frontend_profile_phone_validate: | |
pattern: /phone/validate | |
defaults: { _controller: MxupFrontendBundle:Profile:phoneValidate } | |
mxup_frontend_profile_phone_resend: | |
pattern: /phone/resend | |
defaults: { _controller: MxupFrontendBundle:Profile:phoneValidateResend } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment