def auth(request):
    response_data = {}
    success = 0
    if request.method == 'POST':
        uemail = request.POST['email']

        # TODO: Encrypt PIN.
        upin = request.POST['pin']

        try:
            u = User.objects.get(email__iexact=uemail, pin=upin)
            existing_session = UserSession.objects.filter(user=u)
            if len(existing_session) == 0:
                sessionid = str(uuid.uuid4())

                us = UserSession()
                us.user = u
                us.session_id = sessionid
                us.save()
                response_data['sessionid'] = sessionid
                response_data['userid'] = u.sysid
            else:
                response_data['sessionid'] = existing_session[0].session_id
                response_data['userid'] = u.sysid

            success = 1
        except User.DoesNotExist:
            pass  # TODO: Log failure

    response_data['success'] = success
    return render_to_json(response_data)
    def create_session(self, user_id=None):
        """Overloading

        :param user_id: Default value = None)

        """
        if user_id is None:
            return None
        session_id = super(SessionDBAuth, self).create_session(user_id)
        if session_id is None:
            return None
        user_session = UserSession()
        user_session.user_id = user_id
        user_session.session_id = session_id
        try:
            db_session.add(user_session)
            db_session.commit()
        except BaseException:
            return None
        return session_id