One-Time User Authentication with SMS Using Django and Twilio

前端 未结 3 1683
小鲜肉
小鲜肉 2021-02-05 16:26

I am writing a back-end in Django for a mobile app I am creating. I need to authenticate a user the first time they open the mobile app through SMS to verify it is a real person

3条回答
  •  旧时难觅i
    2021-02-05 16:45

    Disclaimer: I'm the maintainer of Django-phone-verify

    What you're looking to accomplish is very easy with django-phone-verify app. It comes with Twilio already integrated and few endpoints which you can extend as per your use case.

    This package aims at verifying if a phone number requested by a particular client belongs to them. It also takes care of ensuring that the same device provides the verification of passcode which intially requested a passcode to be sent, saving you a few hours of work.

    This package also doesn't messes up with your current user model at all. You're free to use this package exactly for one thing: verifying phone numbers. Whether you do it for users, companies, etc. depends on your use-case.

    It follows Unix philosphy of Do one thing; do it well

    Installation

    pip install django-phone-verify
    

    Configuration

    • Add app to INSTALLED_APPS:
        # In settings.py:
    
        INSTALLED_APPS = [
            ...
            'phone_verify',
        ]
    
    • Add settings in your settings.py file:
        # Settings for phone_verify
        PHONE_VERIFICATION = {
            'BACKEND': 'phone_verify.backends.twilio.TwilioBackend',
            'TWILIO_SANDBOX_TOKEN':'123456',
            'OPTIONS': {
                'SID': 'fake',
                'SECRET': 'fake',
                'FROM': '+14755292729'
            },
            'TOKEN_LENGTH': 6,
            'MESSAGE': 'Welcome to {app}! Please use security code {otp} to proceed.',
            'APP_NAME': 'Phone Verify',
            'OTP_EXPIRATION_TIME': 3600  # In seconds only
        }
    
    • Migrate the database:
        python manage.py migrate
    

    You get two endpoints (Check API docs), one for registration of phone number and other to verify the passcode. You may override verify endpoint to also create a user as described in the usage docs: https://github.com/CuriousLearner/django-phone-verify/blob/master/docs/usage.rst

提交回复
热议问题