Exemplo n.º 1
0
def user_login(request, **kwargs):
    login_error = None
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            # authenticate
            user = authenticate(
                request,
                username=form.cleaned_data.get('username'),
                password=form.cleaned_data.get('password'),
            )
            if user is not None:
                login(request, user)
                return redirect(reverse('dashboard:series_list'))
            else:
                login_error = u'Those credentials are not valid'
    else:
        form = LoginForm()

    return render(request,
                  template_name='dashboard/auth/login.html',
                  context={
                      'form': form,
                      'login_error': login_error
                  })
Exemplo n.º 2
0
def login_view(request):
    #response_data = {}
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            error_msg=""
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            #read data from Db  
            user = UserModel.objects.filter(username=username).first()

            if user:
                #compare password
                if check_password(password, user.password):
                    token = SessionToken(user=user)
                    token.create_token()
                    token.save()
                    #ewdirect to feed
                    response = redirect('/feed/')
                    response.set_cookie(key='session_token', value=token.session_token)
                    return response
                else:
                    error_msg = 'Incorrect Password! Please try again!'
                    return render(request,'login.html',{'error_msg':error_msg})              
    else:
        
        form = LoginForm()
        return render(request, 'login.html', {'login_form':form})
Exemplo n.º 3
0
def login_page(request):
    """
    If user is authenticated, direct them to the next page.
    Otherwise, take them to the login page.

    :param request: django HttpRequest

    :return: django HttpResponse
    """

    message = None
    error = None
    username = password = ''
    form = LoginForm()

    # default next page is index page
    next_page = "/"

    print request

    # getting next page in get request
    if request.GET:
        next_page = request.GET.get('next')

    if request.POST:
        # a form bound to the post data
        form = LoginForm(request.POST)
        username = request.POST.get('username')
        password = request.POST.get('password')
        next_page = request.POST.get('next')
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                state = "You're successfully logged in!"

                # redirect after post
                return HttpResponseRedirect(next_page)
            else:
                message = "Your account is not active, please contact the site admin."
                error = 1
        else:
            message = "Your username and/or password were incorrect."
            error = 1

    c = {'username': username, 'form': form, 'next': next_page}
    if message:
        c.update({"message": message})
    if error:
        c.update({"error": error})
    c.update(csrf(request))

    return render_to_response('login.html', c)
Exemplo n.º 4
0
def auth(request):
    # FIXME: notification about bad credentials is not show
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():

            user = authenticate(username=form.data['username'], password=form.data['password'])
            if user:
                login(request, user)
                return redirect("home")

    else:
        form = LoginForm()
    return render(request, 'login.html', {"form": form})
Exemplo n.º 5
0
def authenticate_user(request):
    form = LoginForm(request.POST)
    if form.is_valid():
        email = form.cleaned_data['email']
        try:
            username = get_user_model().objects.get(email=email).username
        except ObjectDoesNotExist:
            return False
        user = authenticate(username=username,
                            password=form.cleaned_data['password'])
        if user is not None:
            login(request, user)
            return True
    return False
Exemplo n.º 6
0
def login(request):
  next = reverse(projects.list)
  error_header = None
  
  if request.method == 'POST':
    if 'next' in request.POST:
      next = request.POST['next']
      
    login_form = LoginForm(request.POST, auto_id = "id_login_%s")
    if login_form.is_valid():
      try:
        data = login_form.cleaned_data
        
        # query for a user via email
        try:
          user = User.objects.get(email = data['email'])
        except:
          error_header = "{0} isn't registered.".format(data['email'])
          raise LoginError(False)
        
        # authenticate that user
        user = auth.authenticate(username = user.username,
                                 password = data['password'])
        
        # if the password is incorrect, redireect to the login page
        if user is None:
          error_header = "Invalid password."
          raise LoginError(True)
        
        # otherwise, log the user in
        if user.is_active:
          auth.login(request, user)
        else:
          error_header = "Account is deactivated. Please contact a mentor."
          raise LoginError(True)
        
        return HttpResponseRedirect(next)
      except LoginError as e:
        pass
      except:
        raise
  else:
    login_form = LoginForm(auto_id = "id_login_%s")
  
  return render_to_response('users/login.html', {
      'next': next,
      'error_header': error_header,
      'login_form': login_form
    }, context_instance = RequestContext(request))
Exemplo n.º 7
0
def login(request):
  next = reverse(feed.main)
  error_header = None

  if request.method == 'POST':
    if 'next' in request.POST:
      next = request.POST['next']

    login_form = LoginForm(request.POST, auto_id = "id_login_%s")
    if login_form.is_valid():
      try:
        data = login_form.cleaned_data

        # query for a user via email
        try:
          user = User.objects.get(email = data['email'])
        except:
          error_header = "{0} isn't registered.".format(data['email'])
          raise LoginError(False)

        # authenticate that user
        user = auth.authenticate(username = user.username,
                                 password = data['password'])

        # if the password is incorrect, redireect to the login page
        if user is None:
          error_header = "Invalid password."
          raise LoginError(True)

        # otherwise, log the user in
        if user.is_active:
          auth.login(request, user)
        else:
          error_header = "Account is deactivated. Please contact a mentor."
          raise LoginError(True)

        return HttpResponseRedirect(next)
      except LoginError as e:
        pass
      except:
        raise
  else:
    login_form = LoginForm(auto_id = "id_login_%s")

  return render_to_response('users/login.html', {
      'next': next,
      'error_header': error_header,
      'login_form': login_form
    }, context_instance = RequestContext(request))
Exemplo n.º 8
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('dashboard'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            login_user(user, remember=form.remember.data)
            flash(f'Hi {user.name}, you have been logged in.', 'success')
            return redirect(url_for('dashboard'))
        else:
            flash('Login Unsuccessful. Please check email and password',
                  'danger')
    return render_template('login.html', title='Login', form=form)
Exemplo n.º 9
0
def auth(request):
    # FIXME: notification about bad credentials is not show
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():

            user = authenticate(username=form.data['username'],
                                password=form.data['password'])
            if user:
                login(request, user)
                return redirect("home")

    else:
        form = LoginForm()
    return render(request, 'login.html', {"form": form})
Exemplo n.º 10
0
def login(request):
    next = reverse(projects.list)
    error_header = None

    if request.method == "POST":
        if "next" in request.POST:
            next = request.POST["next"]

        login_form = LoginForm(request.POST, auto_id="id_login_%s")
        if login_form.is_valid():
            try:
                data = login_form.cleaned_data

                # query for a user via email
                try:
                    user = User.objects.get(email=data["email"])
                except:
                    error_header = "{0} isn't registered.".format(data["email"])
                    raise LoginError(False)

                # authenticate that user
                user = auth.authenticate(username=user.username, password=data["password"])

                # if the password is incorrect, redireect to the login page
                if user is None:
                    error_header = "Invalid password."
                    raise LoginError(True)

                # otherwise, log the user in
                auth.login(request, user)

                return HttpResponseRedirect(next)
            except LoginError as e:
                pass
            except:
                raise
    else:
        login_form = LoginForm(auto_id="id_login_%s")

    return render_to_response(
        "users/login.html",
        {"next": next, "error_header": error_header, "login_form": login_form},
        context_instance=RequestContext(request),
    )
Exemplo n.º 11
0
def login_page(request):
    """
    If user is authenticated, direct them to the next page. 
    Otherwise, take them to the login page.

    :param request: django HttpRequest

    :return: django HttpResponse 
    """

    print ("--- login --")

    state = ""
    username = password = ''
    form = LoginForm()

    #default next page is index page
    next_page = "/"

    #getting next page in get request
    if request.GET:
        next_page = request.GET.get('next')

    if request.POST:
        form = LoginForm(request.POST) # A form bound to the POST data
        username = request.POST.get('username')
        password = request.POST.get('password')
        next_page = request.POST.get('next')
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                state = "You're successfully logged in!"
                return HttpResponseRedirect(next_page) # Redirect after POST
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    c = {'state':state, 'username': username, 'form': form, 'next': next_page}
    c.update(csrf(request)) 

    return render_to_response('auth.html', c)
Exemplo n.º 12
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('hcs'))

    form = LoginForm()
    if form.validate_on_submit():
        os.environ['SESSION_ID'] = form.sessionID.data

        try:

            user = User(user_id=form.sessionID.data)

            # checks if user is already in database
            if User.query.filter_by(
                    user_id=form.sessionID.data).first() != None:
                login_user(user)
                flash(f'Hi, you have been logged in.', 'success')

                return redirect(url_for('hcs'))

            # if not, add user to db and call forum fetcher
            else:
                db.session.add(user)

                # fetch Hcs
                HcFetch = HcFetcher(form.sessionID.data)
                HcFetch.get_grades()

                # fetch Los
                LoFetch = LoFetcher(form.sessionID.data)
                LoFetch.get_grades()

                db.session.commit()
                login_user(user)

                flash(f'Hi, you have been logged in.', 'success')
                return redirect(url_for('hcs'))
        except:
            flash('Login unsuccessful. Please check Session ID.', 'danger')
            db.session.rollback()

    return render_template('login.html', title='Welcome', form=form)
Exemplo n.º 13
0
def login_view(request):
    form = LoginForm(request.POST or None)

    msg = None

    if request.method == "POST":

        if form.is_valid():
            username = form.cleaned_data.get("username")
            password = form.cleaned_data.get("password")
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect("/")
            else:
                msg = 'Invalid credentials'
        else:
            msg = 'Error validating the form'

    return render(request, "accounts/login.html", {"form": form, "msg": msg})
Exemplo n.º 14
0
def signup_view(request):
    form = LoginForm(request.POST or None)
    signup_form = WebSignUpForm(request.POST or None)
    if request.user.is_authenticated:
        return redirect(reverse('home'))
    if request.method == 'POST':
        form = WebSignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = True
            user.full_name = user.first_name + " " + user.last_name
            user.save()
            try:
                EmailHelper.Email().send_welcome_email(user)
            except:
                pass
            return redirect(reverse('two_factor:login'))
    else:
        signup_form = WebSignUpForm()
    return render(request, "sign-in-up.html", {"form": form, "signup_form": signup_form, 'active_form': True})
Exemplo n.º 15
0
def login(request):
    log_headers = {
        'User-agent': request.headers['User-Agent'],
        "Content-Type": "application/json",
        "X-CSRFToken": get_token(request)  #obtain csrf_Token from the form
    }

    p = request.headers['User-agent']
    if request.method == 'GET':
        print(request.headers['User-agent'])
        form = LoginForm()
        print(log_headers)
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.data["username"]
            password = form.data["passwords"]
            log_data = {"username": str(username), "password": str(password)}
            log_info = requests.post(base_url + "login/",
                                     json=log_data,
                                     headers=log_headers)
            if log_info.status_code == 200:
                print(request.user)
                tokens = json.loads(log_info.text)
                # request.headers.get()

                # session.auth(username, password)
                # request.user.
                # request.META['HTTP_AUTHORIZATION'] = 'WWW-Authorization: Token %s'%tokens["token"]
                # request.headers.set({'WWW-Authorization': "Token %s"%tokens["token"]})
                session.headers.update(
                    {'WWW-Authorization': "Token %s" % tokens["token"]})
                session.headers.update({'User-agent': p})
                print(session.headers.items())
                # return HttpResponseRedirect(reverse('dashboard:index'))
                return render(request, 'dashboard/index.html',
                              {'headers': session.headers.items()})
                # return response
    return render(request, 'dashboard/login.html', {'form': form})
Exemplo n.º 16
0
def login(request):
  next = reverse(projects.list)
  
  if request.method == 'POST':
    if 'next' in request.POST:
      next = request.POST['next']
      
    login_form = LoginForm(request.POST, auto_id = "id_login_%s")
    if login_form.is_valid():
      try:
        data = login_form.cleaned_data
        
        # query for a user via email
        user = User.objects.get(email = data['email'])
        
        # authenticate that user
        user = auth.authenticate(username = user.username,
                                 password = data['password'])
        
        # if the password is incorrect, redireect to the login page
        if user is None:
          return HttpResponseRedirect(reverse(login))
        
        # otherwise, log the user in
        if user.is_active:
          auth.login(request, user)
        
        return HttpResponseRedirect(next)
      except:
        raise
  else:
    login_form = LoginForm(auto_id = "id_login_%s")
  
  return render_to_response('users/login.html', {
      'next': next,
      'error_header': "Something isn't quite right.",
      'login_form': login_form
    }, context_instance = RequestContext(request))
Exemplo n.º 17
0
def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            userObj = form.cleaned_data
            username = userObj['username']
            password = userObj['password']
            user = authenticate(username=username, password=password)
            if user:
                if user.is_active:
                    login(request, user)
                    return redirect('/index/')
                else:
                    return HttpResponse('Your credientials are wrong!')
            else:
                print("Invalid login details: {0}, {1}".format(username, password))
                return redirect('/signin/?invaliddetails=true')

    else:
        if request.user.is_authenticated:
            return redirect('/index/')
        else:
            form = LoginForm()
            return render(request, 'login.html', {'form': form})
Exemplo n.º 18
0
def login_or_reg(request):
  next = reverse(projects.list)
  
  if 'next' in request.GET:
    next = request.GET['next']
  
  reg_form = RegistrationForm(auto_id = "id_login_%s")
  login_form = LoginForm(auto_id = "id_login_%s")
  
  return render_to_response('users/login-register.html', {
      'next': next,
      'js_page_id': 'login-register',
      'reg_form': reg_form,
      'login_form': login_form,
      'RECAPTCHA_PUBLIC': RECAPTCHA_PUBLIC,
      'RECAPTCHA_PRIVATE': RECAPTCHA_PRIVATE
    }, context_instance = RequestContext(request))
Exemplo n.º 19
0
def login_view(request):
    form = LoginForm()
    if request.method == 'POST':
        if authenticate_user(request):
            return HttpResponseRedirect('/')
    return render(request, 'dashboard/accounts/login.html', {'form': form})