how can I prevent user to go to login page after successful authentication?

user7104029

I am adding settings.py, root url and views.py. After login user is redirected to respective dashboard. In this situation, if user is pressing back button or changing url to accounts/login, then also it should remain on the dashboard page only. I am using django-registration-redux

settings.py

REGISTRATION_OPEN = True
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = False
REGISTRATION_FORM = 'signin.forms.MyRegForm'
LOGIN_REDIRECT_URL = '/signin/user_sign/'

views.py

def user_sign(request):
    obj = UserSelection.objects.get(user=request.user)

    if obj.user_type == 'candidate':
        return redirect('candidate:cand_dash')

    else:
        return redirect('employer:employer_dash')

urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from signin.regbackend import MyRegistrationView
from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^$', auth_views.LoginView.as_view(template_name='registration/login.html'), name='home'),
    url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
    url(r'^accounts/', include('registration.backends.default.urls')),

    url(r'^candidate/', include('candidate.urls')),
    url(r'^employer/', include('employer.urls')),
    url(r'^signin/', include('signin.urls')),
]
Panos Angelopoulos

You could use a Boolean variable authenticated.

Then you should need to set it as False before the user Authentication.

def registration(request):
    authenticated = False
...

Then after the user's authentication just change the var as authenticated = True

Finally every time you need to know if user is authenticated just use if user.authenticated

Also, if you need to use authenticated a lot take a look at custom decorators (https://docs.djangoproject.com/en/2.0/topics/http/decorators/) maybe they could help you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I redirect a user after successful login to different pages, based on their previous page?

How to prevent user to go to login page after logged in in Vuejs?

Unable to prevent user to go back to login page after successfully logging in

How can I store user details into database after LDAP successful login?

how to redirect to a page after successful login in ReactJS?

How to redirect user after successful login?

How to prevent user go back to protected page after logout

User access to login page after authentication

How to Redirect/go to the User Profile after login page in ReactJS

How to redirect to the page that I want after logged in and display username after successful login

How can I redirect a user to another page on React after state change and Google login?

How can i return the login page after signing up new user using firebase and react native?

Spring Security + JWT: How to enrich Authentication/Principal after successful login?

How to login user to a google webpage after successful OAuth 2.0 login?

Laravel 5 user can not be retrieved after successful authentication

Spring MVC: How do I forward to a specifc page after a successful Login?

Change page after successful login

Flask + Stripe - how can I prevent people from accessing my successful checkout page without making a payment?

.Net Core 5 Razor pages Cookie Authentication redirects to the login page after successful login

How can I go to the login page ? (Symfony 6.0.2)

How to redirect to the next page after successful authentication in flutter

How to get user info after a successful authentication with nuxt-auth

How can I prevent access to an Angular page if the user IS logged in with AngularFire?

How can i implement role redirection after a successful login in my MEAN stack Application

how can I get Identity UserID in the controller right after a successful login result?

How to redirect to other page after successful login in angularjs?

How to open new page after a successful login GWT Java

How to show a successful login popup message when the successful login can go to a redirect url?

Authentication And Login Page - how do I authenticate?