{"id":75766,"date":"2020-01-20T12:04:07","date_gmt":"2020-01-20T06:34:07","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75766"},"modified":"2024-04-12T14:17:18","modified_gmt":"2024-04-12T08:47:18","slug":"users-and-authentication-2","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/","title":{"rendered":"Users and Authentication"},"content":{"rendered":"<p>A significant percentage of modern, interactive websites allow some form of user interaction \u2013 from allowing simple comments on a blog, to full editorial control of articles on a news site. If a site offers any sort of ecommerce, authentication and authorization of paying customers is essential.<\/p>\n<p><strong>Just managing users<\/strong> \u2013 lost usernames, forgotten passwords and keeping information up to date \u2013 can be a real pain. As a programmer, writing an authentication system can be even worse.<br>\nLucky for us, Django provides a default implementation for managing user accounts, groups, permissions and cookie-based user sessions out of the box. Like most things in Django, the default implementation is fully extensible and customizable to suit your project\u2019s needs. So let\u2019s jump right in.<br>\nOverview<br>\nThe Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do. Here the term authentication is used to refer to both tasks.<\/p>\n<p>The authentication system consists of:<\/p>\n<ul>\n<li>Users<\/li>\n<li>Permissions: Binary (yes\/no) flags designating whether a user may perform a certain task<\/li>\n<li>Groups: A generic way of applying labels and permissions to more than one user<\/li>\n<li>A configurable password hashing system<\/li>\n<li>Forms for managing user authentication and authorization<\/li>\n<li>View tools for logging in users, or restricting content<\/li>\n<li>A pluggable backend system<\/li>\n<\/ul>\n<p>The authentication system in Django aims to be very generic and doesn\u2019t provide some features commonly found in web authentication systems. Solutions for some of these common problems have been implemented in third-party packages:<\/p>\n<ul>\n<li>&nbsp;Password strength checking<\/li>\n<li>Throttling of login attempts<\/li>\n<li>Authentication against third-parties (OAuth, for example)<\/li>\n<\/ul>\n<p><strong>Using The Django Authentication System<\/strong> &#8211; Django\u2019s authentication system in its default configuration has evolved to serve the most common project needs, handling a reasonably wide range of tasks, and has a careful implementation of passwords and permissions. For projects where authentication needs differ from the default, Django also supports extensive extension and customization of authentication.<\/p>\n<p><strong>User Objects<\/strong> &#8211; User objects are the core of the authentication system. They typically represent the people interacting with your site and are used to enable things like restricting access, registering user profiles, associating content with creators etc. Only one class of user exists in Django\u2019s authentication framework, i.e., superusers or admin staff users are just user objects with special attributes set, not different classes of user objects. The primary attributes of the default user are:<\/p>\n<ul>\n<li>&nbsp;username<\/li>\n<li>password<\/li>\n<li>email<\/li>\n<li>first_name<\/li>\n<li>last_name<\/li>\n<\/ul>\n<p><strong>Creating Superusers<\/strong> &#8211; Create superusers using the createsuperuser command:<br>\npython manage.py createsuperuser &#8211;username=joe &#8211;email=joe@example.com<\/p>\n<p>You will be prompted for a password. After you enter one, the user will be created immediately. If you leave off the &#8211;username or the &#8211;email options, it will prompt you for those values.<\/p>\n<p>Creating Users &#8211; The simplest, and least error prone way to create and manage users is through the Django admin. Django also provides built in views and forms to allow users to log in and out and change their own password. We will be looking at user management via the admin and generic user forms a bit later in this chapter, but first, let\u2019s look at how we would handle user authentication directly.<\/p>\n<p>The most direct way to create users is to use the included create_user() helper function:<\/p>\n<p>&gt;&gt;&gt; from django.contrib.auth.models import User<br>\n&gt;&gt;&gt; user = User.objects.create_user(&#8216;john&#8217;, &#8216;lennon@thebeatles.com&#8217;, &#8216;johnpassword&#8217;)<\/p>\n<p># At this point, user is a User object that has already been saved<br>\n# to the database. You can continue to change its attributes<br>\n# if you want to change other fields.<br>\n&gt;&gt;&gt; user.last_name = &#8216;Lennon&#8217;<br>\n&gt;&gt;&gt; user.save()<\/p>\n<p><strong>Changing Passwords<\/strong> &#8211; Django does not store raw (clear text) passwords on the user model, but only a hash. Because of this, do not attempt to manipulate the password attribute of the user directly. This is why a helper function is used when creating a user. To change a user\u2019s password, you have two options:<\/p>\n<ul>\n<li>&nbsp;manage.py changepassword username offers a method of changing a User\u2019s password from the command line. It prompts you to change the password of a given user which you must enter twice. If they both match, the new password will be changed immediately. If you do not supply a user, the command will attempt to change the password of the user whose username matches the current system user.<\/li>\n<li>You can also change a password programmatically, using set_password():<\/li>\n<\/ul>\n<p>&gt;&gt;&gt; from django.contrib.auth.models import User<br>\n&gt;&gt;&gt; u = User.objects.get(username=&#8217;john&#8217;)<br>\n&gt;&gt;&gt; u.set_password(&#8216;new password&#8217;)<br>\n&gt;&gt;&gt; u.save()<\/p>\n<p>Changing a user\u2019s password will log out all their sessions if the SessionAuthenticationMiddleware is enabled.<\/p>\n<h3>Authentication in Web Requests<\/h3>\n<p>Django uses sessions and middleware to hook the authentication system into request objects. These provide a request.user attribute on every request which represents the current user. If the current user has not logged in, this attribute will be set to an instance of AnonymousUser, otherwise it will be an instance of User. You can tell them apart with is_authenticated(), like so:<\/p>\n<p>if request.user.is_authenticated():<br>\n# Do something for authenticated users.<br>\nelse:<br>\n# Do something for anonymous users.<\/p>\n<p>How to Log a User In &#8211; To log a user in, from a view, use login(). It takes an HttpRequest object and a User object. login() saves the user\u2019s ID in the session, using Django\u2019s session framework. Note that any data set during the anonymous session is retained in the session after a user logs in. This example shows how you might use both authenticate() and login():<\/p>\n<p>from django.contrib.auth import authenticate, login<\/p>\n<p>def my_view(request):<br>\nusername = request.POST[&#8216;username&#8217;]\npassword = request.POST[&#8216;password&#8217;]\nuser = authenticate(username=username, password=password)<br>\nif user is not None:<br>\nif user.is_active:<br>\nlogin(request, user)<br>\n# Redirect to a success page.<br>\nelse:<br>\n# Return a &#8216;disabled account&#8217; error message<br>\nelse:<br>\n# Return an &#8216;invalid login&#8217; error message.<\/p>\n<p>Calling authenticate() firstWhen you\u2019re manually logging a user in, you must call authenticate() before you call login(). authenticate() sets an attribute on the User noting which authentication backend successfully authenticated that user, and this information is needed later during the login process. An error will be raised if you try to login a user object retrieved from the database directly.<\/p>\n<p><strong>How to Log a User Out<\/strong> &#8211; To log out a user who has been logged in via login(), use logout() within your view. It takes an HttpRequest object and has no return value. Example:<\/p>\n<p>from django.contrib.auth import logout<\/p>\n<p>def logout_view(request):<br>\nlogout(request)<br>\n# Redirect to a success page.<\/p>\n<p>Note that logout() doesn\u2019t throw any errors if the user wasn\u2019t logged in. When you call logout(), the session data for the current request is completely cleaned out. All existing data is removed. This is to prevent another person from using the same Web browser to log in and have access to the previous user\u2019s session data.<\/p>\n<p>If you want to put anything into the session that will be available to the user immediately after logging out, do that after calling logout().<\/p>\n<h3>Limiting Access to Logged-In Users<\/h3>\n<p><strong>The raw way<\/strong> &#8211; The simple, raw way to limit access to pages is to check request.user.is_authenticated() and either redirect to a login page:<\/p>\n<p>from django.shortcuts import redirect<\/p>\n<p>def my_view(request):<br>\nif not request.user.is_authenticated():<br>\nreturn redirect(&#8216;\/login\/?next=%s&#8217; % request.path)<br>\n# &#8230;<\/p>\n<p>\u2026 or display an error message:<\/p>\n<p>from django.shortcuts import render<\/p>\n<p>def my_view(request):<br>\nif not request.user.is_authenticated():<br>\nreturn render(request, &#8216;books\/login_error.html&#8217;)<br>\n# &#8230;<\/p>\n<p><strong>The login_required decorator<\/strong> &#8211; As a shortcut, you can use the convenient login_required() decorator:<\/p>\n<p>from django.contrib.auth.decorators import login_required<\/p>\n<p>@login_required<br>\ndef my_view(request):<br>\n&#8230;login_required() does the following:<\/p>\n<ul>\n<li>If the user isn\u2019t logged in, redirect to LOGIN_URL, passing the current absolute path in the query string. Example: \/accounts\/login\/?next=\/reviews\/3\/.<\/li>\n<li>If the user is logged in, execute the view normally. The view code is free to assume the user is logged in.<\/li>\n<\/ul>\n<p>By default, the path that the user should be redirected to upon successful authentication is stored in a query string parameter called \u201cnext\u201d. If you would prefer to use a different name for this parameter, login_required() takes an optional redirect_field_name parameter:<\/p>\n<p>from django.contrib.auth.decorators import login_required<\/p>\n<p>@login_required(redirect_field_name=&#8217;my_redirect_field&#8217;)<br>\ndef my_view(request):<br>\n&#8230;<\/p>\n<p>Note that if you provide a value to redirect_field_name, you will most likely need to customize your login template as well, since the template context variable which stores the redirect path will use the value of redirect_field_name as its key rather than \u201cnext\u201d (the default). login_required() also takes an optional login_url parameter. Example:<\/p>\n<p>from django.contrib.auth.decorators import login_required<\/p>\n<p>@login_required(login_url=&#8217;\/accounts\/login\/&#8217;)<br>\ndef my_view(request):<br>\n&#8230;<\/p>\n<p>Note that if you don\u2019t specify the login_url parameter, you\u2019ll need to ensure that the LOGIN_URL and your login view are properly associated. For example, using the defaults, add the following lines to your URLconf:<\/p>\n<p>from django.contrib.auth import views as auth_views<\/p>\n<p>url(r&#8217;^accounts\/login\/$&#8217;, auth_views.login),<\/p>\n<p>The LOGIN_URL also accepts view function names and named URL patterns. This allows you to freely remap your login view within your URLconf without having to update the setting. The login_required decorator does NOT check the is_active flag on a user.<\/p>\n<p><strong>Limiting access to logged-in users that pass a test<\/strong> &#8211; To limit access based on certain permissions or some other test, you\u2019d do essentially the same thing as described in the previous section. The simple way is to run your test on request.user in the view directly. For example, this view checks to make sure the user has an email in the desired domain:<\/p>\n<p>def my_view(request):<br>\nif not request.user.email.endswith(&#8216;@example.com&#8217;):<br>\nreturn HttpResponse(&#8220;You can&#8217;t leave a review for this book.&#8221;)<br>\n# &#8230;<\/p>\n<p>As a shortcut, you can use the convenient user_passes_test decorator:<\/p>\n<p>from django.contrib.auth.decorators import user_passes_test<\/p>\n<p>def email_check(user):<br>\nreturn user.email.endswith(&#8216;@example.com&#8217;)<\/p>\n<p>@user_passes_test(email_check)<br>\ndef my_view(request):<br>\n&#8230;<\/p>\n<p>user_passes_test() takes a required argument: a callable that takes a User object and returns True if the user is allowed to view the page. Note that user_passes_test() does not automatically check that the User is not anonymous. user_passes_test() takes two optional arguments:<\/p>\n<ul>\n<li>&nbsp;login_url. Lets you specify the URL that users who don\u2019t pass the test will be redirected to. It may be a login page and defaults to LOGIN_URL if you don\u2019t specify one.<\/li>\n<li>redirect_field_name. Same as for login_required(). Setting it to None removes it from the URL, which you may want to do if you are redirecting users that don\u2019t pass the test to a non-login page where there\u2019s no \u201cnext page\u201d.<\/li>\n<\/ul>\n<p>For example:<br>\n@user_passes_test(email_check, login_url=&#8217;\/login\/&#8217;)<br>\ndef my_view(request):<br>\n&#8230;<\/p>\n<p>The permission_required decorator &#8211; It\u2019s a relatively common task to check whether a user has a particular permission. For that reason, Django provides a shortcut for that case \u2013 the permission_required() decorator:<\/p>\n<p>from django.contrib.auth.decorators import permission_required<\/p>\n<p>@permission_required(&#8216;reviews.can_vote&#8217;)<br>\ndef my_view(request):<br>\n&#8230;<\/p>\n<p>Just like the has_perm() method, permission names take the form \u201c&lt;app label&gt;.&lt;permission codename&gt;\u201d (i.e. reviews.can_vote for a permission on a model in the reviews application). The decorator may also take a list of permissions. Note that permission_required() also takes an optional login_url parameter. Example:<\/p>\n<p>from django.contrib.auth.decorators import permission_required<\/p>\n<p>@permission_required(&#8216;reviews.can_vote&#8217;, login_url=&#8217;\/loginpage\/&#8217;)<br>\ndef my_view(request):<br>\n&#8230;<\/p>\n<p>As in the login_required() decorator, login_url defaults to LOGIN_URL. If the raise_exception parameter is given, the decorator will raise PermissionDenied, prompting the 403 (HTTP Forbidden) view instead of redirecting to the login page.<\/p>\n<p>Session invalidation on password change &#8211; If your AUTH_USER_MODEL inherits from AbstractBaseUser, or implements its own get_session_auth_hash()<br>\nmethod, authenticated sessions will include the hash returned by this function. In the AbstractBaseUser case, this is an Hash Message Authentication Code (HMAC) of the password field.<\/p>\n<p>If the SessionAuthenticationMiddleware is enabled, Django verifies that the hash sent along with each request matches the one that\u2019s computed server-side. This allows a user to log out of all of their sessions by changing their password.<\/p>\n<p>The default password change views included with Django, django.contrib.auth.views.password_change() and the user_change_password view in the django.contrib.auth admin, update the session with the new password hash so that a user changing their own password won\u2019t log themselves out. If you have a custom password change view and wish to have similar behavior, use this function:<\/p>\n<p>django.contrib.auth.decorators.update_session_auth_hash (request, user)<\/p>\n<p>This function takes the current request and the updated user object from which the new session hash will be derived and updates the session hash appropriately. Example usage:<\/p>\n<p>from django.contrib.auth import update_session_auth_hash<\/p>\n<p>def password_change(request):<br>\nif request.method == &#8216;POST&#8217;:<br>\nform = PasswordChangeForm(user=request.user, data=request.POST)<br>\nif form.is_valid():<br>\nform.save()<br>\nupdate_session_auth_hash(request, form.user)<br>\nelse:<br>\n&#8230;<\/p>\n<p>Since get_session_auth_hash() is based on SECRET_KEY, updating your site to use a new secret will invalidate all existing sessions.<br>\nAuthentication Views<br>\nDjango provides several views that you can use for handling login, logout, and password management. These make use of the built-in auth forms but you can pass in your own forms as well. Django provides no default template for the authentication views \u2013 however the template context is documented for each view below.<\/p>\n<p>There are different methods to implement these views in your project, however the easiest and most common way is to include the provided URLconf in django.contrib.auth.urls in your own URLconf, for example:<\/p>\n<p>urlpatterns = [url(&#8216;^&#8217;, include(&#8216;django.contrib.auth.urls&#8217;))]\n<p>This will make each of the views available at a default URL (detailed below).<\/p>\n<p>The built-in views all return a TemplateResponse instance, which allows you to easily customize the response data before rendering. Most built-in authentication views provide a URL name for easier reference.<br>\nlogin &#8211; Logs a user in. Default URL: \/login\/<\/p>\n<p>Optional arguments:<\/p>\n<ul>\n<li>template_name: The name of a template to display for the view used to log the user in. Defaults to registration\/login.html.<\/li>\n<li>redirect_field_name: The name of a GET field containing the URL to redirect to after login. Defaults to next.<\/li>\n<li>authentication_form: A callable (typically just a form class) to use for authentication. Defaults to AuthenticationForm.<\/li>\n<li>current_app: A hint indicating which application contains the current view.<\/li>\n<li>extra_context: A dictionary of context data that will be added to the default context data passed to the template.<\/li>\n<\/ul>\n<p>Here\u2019s what login does:<\/p>\n<ul>\n<li>If called via GET, it displays a login form that POSTs to the same URL. More on this in a bit.<\/li>\n<li>If called via POST with user submitted credentials, it tries to log the user in. If login is successful, the view redirects to the URL specified in next. If next isn\u2019t provided, it redirects to LOGIN_REDIRECT_URL (which defaults to \/accounts\/profile\/). If login isn\u2019t successful, it redisplays the login form.<\/li>\n<\/ul>\n<p>It\u2019s your responsibility to provide the html for the login template, called registration\/login.html by default.<\/p>\n<p><strong>Template Context <\/strong><\/p>\n<ul>\n<li>form: A Form object representing the AuthenticationForm.<\/li>\n<li>next: The URL to redirect to after successful login. This may contain a query string, too.<\/li>\n<li>site: The current Site, according to the SITE_ID setting. If you don\u2019t have the site framework installed, this will be set to an instance of RequestSite, which derives the site name and domain from the current HttpRequest.<\/li>\n<li>site_name: An alias for site.name. If you don\u2019t have the site framework installed, this will be set to the value of request.META[&#8216;SERVER_NAME&#8217;]. If you\u2019d prefer not to call the template registration\/login.html, you can pass the template_name parameter via the extra arguments to the view in your URLconf.<\/li>\n<\/ul>\n<p><strong>logout<\/strong> &#8211; Logs a user out. Default URL: \/logout\/<\/p>\n<p>Optional arguments:<\/p>\n<ul>\n<li>next_page: The URL to redirect to after logout.<\/li>\n<li>template_name: The full name of a template to display after logging the user out. Defaults to registration\/logged_out.html if no argument is supplied.<\/li>\n<li>redirect_field_name: The name of a GET field containing the URL to redirect to after log out. Defaults to next. Overrides the next_page URL if the given GET parameter is passed.<\/li>\n<li>current_app: A hint indicating which application contains the current view.<\/li>\n<li>extra_context: A dictionary of context data that will be added to the default context data passed to the template.<br>\nTemplate context:<\/li>\n<li>title: The string \u201cLogged out\u201d, localized.<\/li>\n<li>site: The current Site, according to the SITE_ID setting. If you don\u2019t have the site framework installed, this will be set to an instance of RequestSite, which derives the site name and domain from the current HttpRequest.<\/li>\n<li>site_name: An alias for site.name. If you don\u2019t have the site framework installed, this will be set to the value of request.META[&#8216;SERVER_NAME&#8217;].<\/li>\n<li>current_app: A hint indicating which application contains the current view.<\/li>\n<li>extra_context: A dictionary of context data that will be added to the default context data passed to the template.<\/li>\n<\/ul>\n<p>logout_then_login &#8211; Logs a user out, then redirects to the login page. Default URL: None provided.<\/p>\n<p>Optional arguments:<\/p>\n<ul>\n<li>login_url: The URL of the login page to redirect to. Defaults to LOGIN_URL if not supplied.<\/li>\n<li>current_app: A hint indicating which application contains the current view.<\/li>\n<li>extra_context: A dictionary of context data that will be added to the default context data passed to the template.<\/li>\n<\/ul>\n<p><strong>password_change<\/strong> &#8211; Allows a user to change their password. Default URL: \/password_change\/<\/p>\n<p>Optional arguments:<\/p>\n<ul>\n<li>template_name: The full name of a template to use for displaying the password change form. Defaults to registration\/password_change_form.html if not supplied.<\/li>\n<li>post_change_redirect: The URL to redirect to after a successful password change.<\/li>\n<li>password_change_form: A custom \u201cchange password\u201d form which must accept a user keyword argument. The form is responsible for actually changing the user\u2019s password. Defaults to PasswordChangeForm.<\/li>\n<li>current_app: A hint indicating which application contains the current view.<\/li>\n<li>extra_context: A dictionary of context data that will be added to the default context data passed to the template.<\/li>\n<\/ul>\n<p>Template context:<\/p>\n<ul>\n<li>form: The password change form<\/li>\n<\/ul>\n<p><strong>password_change_done<\/strong> &#8211; The page shown after a user has changed their password. Default URL: \/password_change_done\/<br>\nOptional arguments:<\/p>\n<ul>\n<li>template_name: The full name of a template to use. Defaults to registration\/password_change_done.html if not supplied.<\/li>\n<li>current_app: A hint indicating which application contains the current view.<\/li>\n<li>extra_context: A dictionary of context data that will be added to the default context data passed to the template.<br>\npassword_reset &#8211; Default URL: \/password_reset\/. Allows a user to reset their password by generating a one-time use link that can be used to reset the password, and sending that link to the user\u2019s registered email address.<\/li>\n<\/ul>\n<p>If the email address provided does not exist in the system, this view won\u2019t send an email, but the user won\u2019t receive any error message either. This prevents information leaking to potential attackers.<br>\nIf you want to provide an error message in this case, you can subclass PasswordResetForm and use the password_reset_form argument.<\/p>\n<p>Users flagged with an unusable password aren\u2019t allowed to request a password reset to prevent misuse when using an external authentication source like LDAP. Note that they won\u2019t receive any error message since this would expose their account\u2019s existence but no mail will be sent either.<\/p>\n<p>Optional arguments:<\/p>\n<ul>\n<li>template_name: The full name of a template to use for displaying the password reset form. Defaults to registration\/password_reset_form.html if not supplied.<\/li>\n<li>email_template_name: The full name of a template to use for generating the email with the reset password link. Defaults to registration\/password_reset_email.html if not supplied.<\/li>\n<li>subject_template_name: The full name of a template to use for the subject of the email with the reset password link. Defaults to registration\/password_reset_subject.txt if not supplied.<\/li>\n<li>password_reset_form: Form that will be used to get the email of the user to reset the password for. Defaults to PasswordResetForm.<\/li>\n<li>token_generator: Instance of the class to check the one-time link. This will default to default_token_generator, it\u2019s an instance of django.contrib.auth.tokens.PasswordResetTokenGenerator.<\/li>\n<li>post_reset_redirect: The URL to redirect to after a successful password reset request.<\/li>\n<li>from_email: A valid email address. By default, Django uses the DEFAULT_FROM_EMAIL.<\/li>\n<li>current_app: A hint indicating which application contains the current view.<\/li>\n<li>extra_context: A dictionary of context data that will be added to the default context data passed to the template.<\/li>\n<li>html_email_template_name: The full name of a template to use for generating a text\/html multipart email with the password reset link. By default, HTML email is not sent.<\/li>\n<\/ul>\n<p>Template context:<\/p>\n<ul>\n<li>form: The form (see password_reset_form above) for resetting the user\u2019s password.<\/li>\n<\/ul>\n<p>Email template context:<\/p>\n<ul>\n<li>email: An alias for user.email<\/li>\n<li>user: The current User, according to the email form field. Only active users are able to reset their passwords (User.is_active is True).<\/li>\n<li>site_name: An alias for site.name. If you don\u2019t have the site framework installed, this will be set to the value of request.META[&#8216;SERVER_NAME&#8217;].<\/li>\n<li>domain: An alias for site.domain. If you don\u2019t have the site framework installed, this will be set to the value of request.get_host().<\/li>\n<li>protocol: http or https<\/li>\n<li>uid: The user\u2019s primary key encoded in base 64.<\/li>\n<li>token: Token to check that the reset link is valid.<\/li>\n<\/ul>\n<p>Sample registration\/password_reset_email.html (email body template):<\/p>\n<p>Someone asked for password reset for email {{ email }}. Follow the link below:<br>\n{{ protocol}}:\/\/{{ domain }}{% url &#8216;password_reset_confirm&#8217; uidb64=uid token=token %}<\/p>\n<p>The same template context is used for subject template. Subject must be single line plain text string.<\/p>\n<p>password_reset_done &#8211; The page shown after a user has been emailed a link to reset their password. This view is called by default if the password_reset() view doesn\u2019t have an explicit post_reset_redirect URL set. Default URL: \/password_reset_done\/<\/p>\n<p>If the email address provided does not exist in the system, the user is inactive, or has an unusable password, the user will still be redirected to this view but no email will be sent.<\/p>\n<p>Optional arguments:<br>\n\uf0fc template_name: The full name of a template to use. Defaults to registration\/password_reset_done.html if not supplied.<br>\n\uf0fc current_app: A hint indicating which application contains the current view.<br>\n\uf0fc extra_context: A dictionary of context data that will be added to the default context data passed to the template.<\/p>\n<p>password_reset_confirm &#8211; Presents a form for entering a new password. Default URL: \/password_reset_confirm\/<br>\nOptional arguments:<br>\n\uf0fc uidb64: The user\u2019s id encoded in base 64. Defaults to None.<br>\n\uf0fc token: Token to check that the password is valid. Defaults to None.<br>\n\uf0fc template_name: The full name of a template to display the confirm password view. Default value is registration\/password_reset_confirm.html.<br>\n\uf0fc token_generator: Instance of the class to check the password. This will default to default_token_generator, it\u2019s an instance of django.contrib.auth.tokens.PasswordResetTokenGenerator.<br>\n\uf0fc set_password_form: Form that will be used to set the password. Defaults to SetPasswordForm<br>\n\uf0fc post_reset_redirect: URL to redirect after the password reset done. Defaults to None.<br>\n\uf0fc current_app: A hint indicating which application contains the current view.<br>\n\uf0fc extra_context: A dictionary of context data that will be added to the default context data passed to the template.<\/p>\n<p>Template context:<\/p>\n<ul>\n<li>&nbsp;form: The form (see set_password_form above) for setting the new user\u2019s password.<\/li>\n<li>validlink: Boolean, True if the link (combination of uidb64 and token) is valid or unused yet.<\/li>\n<\/ul>\n<p><strong>password_reset_complete<\/strong> &#8211; Presents a view which informs the user that the password has been successfully changed. Default URL: \/password_reset_complete\/<br>\nOptional arguments:<\/p>\n<ul>\n<li>template_name: The full name of a template to display the view. Defaults to registration\/password_reset_complete.html.<\/li>\n<li>current_app: A hint indicating which application contains the current view.<\/li>\n<li>extra_context: A dictionary of context data that will be added to the default context data passed to the template.<\/li>\n<\/ul>\n<p>The redirect_to_login helper function &#8211; Django provides a convenient function, redirect_to_login that can be used in a view for implementing custom access control. It redirects to the login page, and then back to another URL after a successful login.<\/p>\n<ul>\n<li>Required arguments:<br>\nnext: The URL to redirect to after a successful login.<\/li>\n<\/ul>\n<p>Optional arguments:<\/p>\n<ul>\n<li>login_url: The URL of the login page to redirect to. Defaults to LOGIN_URL if not supplied.<\/li>\n<li>redirect_field_name: The name of a GET field containing the URL to redirect to after log out. Overrides next if the given GET parameter is passed.<\/li>\n<\/ul>\n<h3>Authentication Forms and Templates<\/h3>\n<p><strong>Built-in forms<\/strong> &#8211; If you don\u2019t want to use the built-in views, but want the convenience of not having to write forms for this functionality, the authentication system provides several built-in forms located in django.contrib.auth.forms in table below.<\/p>\n<p>The built-in authentication forms make certain assumptions about the user model that they are working with. If you\u2019re using a custom User model, it may be necessary to define your own forms for the authentication system.<\/p>\n<p>Django\u2019s built-in authentication forms<\/p>\n<table style=\"height: 612px;\" width=\"725\">\n<tbody>\n<tr>\n<td width=\"214\">AdminPasswordChangeForm<\/td>\n<td width=\"425\">A form used in the admin interface to change a user\u2019s password. Takes the user as the first positional argument.<\/td>\n<\/tr>\n<tr>\n<td width=\"214\">AuthenticationForm<\/td>\n<td width=\"425\">A form for logging a user in. Takes request as its first positional argument, which is stored on the form instance for use by sub-classes.<\/td>\n<\/tr>\n<tr>\n<td width=\"214\">PasswordChangeForm<\/td>\n<td width=\"425\">A form for allowing a user to change their password.<\/td>\n<\/tr>\n<tr>\n<td width=\"214\">PasswordResetForm<\/td>\n<td width=\"425\">A form for generating and emailing a one-time use link to reset a user\u2019s password.<\/td>\n<\/tr>\n<tr>\n<td width=\"214\">SetPasswordForm<\/td>\n<td width=\"425\">A form that lets a user change their password without entering the old password.<\/td>\n<\/tr>\n<tr>\n<td width=\"214\">UserChangeForm<\/td>\n<td width=\"425\">A form used in the admin interface to change a user\u2019s information and permissions.<\/td>\n<\/tr>\n<tr>\n<td width=\"214\">UserCreationForm<\/td>\n<td width=\"425\">A form for creating a new user.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Authentication Data in Templates<\/strong> &#8211; The currently logged-in user and their permissions are made available in the template context when you use RequestContext.<\/p>\n<p><strong>Users<\/strong> &#8211; When rendering a template RequestContext, the currently logged-in user, either a User instance or an AnonymousUser instance, is stored in the template variable<br>\n{{ user }}:<\/p>\n<p>{% if user.is_authenticated %}<br>\n&lt;p&gt;Welcome, {{ user.username }}. Thanks for logging in.&lt;\/p&gt;<br>\n{% else %}<br>\n&lt;p&gt;Welcome, new user. Please log in.&lt;\/p&gt;<br>\n{% endif %}<\/p>\n<p>This template context variable is not available if a RequestContext is not being used.<\/p>\n<p>Permissions &#8211; The currently logged-in user\u2019s permissions are stored in the template variable {{ perms }}. This is an instance of django.contrib.auth.context_processors.PermWrapper, which is a template-friendly proxy of permissions. In the {{ perms }} object, single-attribute lookup is a proxy to User.has_module_perms. This example would display True if the logged-in user had any permissions in the foo app:<\/p>\n<p>{{ perms.foo }}<\/p>\n<p>Two-level-attribute lookup is a proxy to User.has_perm. This example would display True if the logged-in user had the permission foo.can_vote:<\/p>\n<p>{{ perms.foo.can_vote }}<\/p>\n<p>Thus, you can check permissions in template {% if %} statements:<\/p>\n<p>{% if perms.foo %}<br>\n&lt;p&gt;You have permission to do something in the foo app.&lt;\/p&gt;<br>\n{% if perms.foo.can_vote %}<br>\n&lt;p&gt;You can vote!&lt;\/p&gt;<br>\n{% endif %}<br>\n{% if perms.foo.can_drive %}<br>\n&lt;p&gt;You can drive!&lt;\/p&gt;<br>\n{% endif %}<br>\n{% else %}<br>\n&lt;p&gt;You don&#8217;t have permission to do anything in the foo app.&lt;\/p&gt;<br>\n{% endif %}<\/p>\n<p>It is possible to also look permissions up by {% if in %} statements. For example:<\/p>\n<p>{% if &#8216;foo&#8217; in perms %}<br>\n{% if &#8216;foo.can_vote&#8217; in perms %}<br>\n&lt;p&gt;In lookup works, too.&lt;\/p&gt;<br>\n{% endif %}<br>\n{% endif %}<\/p>\n<h3>Customizing Authentication in Django<\/h3>\n<p>The authentication that comes with Django is good enough for most common cases, but you may have needs not met by the out-of-the-box defaults. To customize authentication to your projects needs involves understanding what points of the provided system are extensible or replaceable.<\/p>\n<p>Authentication backends provide an extensible system for when a username and password stored with the User model need to be authenticated against a different service than Django\u2019s default. You can give your models custom permissions that can be checked through Django\u2019s authorization system. You can extend the default User model, or substitute a completely customized model.<br>\nOther Authentication Sources &#8211; There may be times you have the need to hook into another authentication source \u2013 that is, another source of usernames and passwords or authentication methods.<\/p>\n<p>For example, your company may already have an LDAP setup that stores a username and password for every employee. It\u2019d be a hassle for both the network administrator and the users themselves if users had separate accounts in LDAP and the Django-based applications.<\/p>\n<p>So, to handle situations like this, the Django authentication system lets you plug in other authentication sources. You can override Django\u2019s default database-based scheme, or you can use the default system in tandem with other systems.<\/p>\n<p><strong>Specifying Authentication Backends <\/strong>&#8211; Behind the scenes, Django maintains a list of authentication backends that it checks for authentication. When somebody calls authenticate() \u2013 as described in the previous section on logging a user in \u2013 Django tries authenticating across all of its authentication backends. If the first authentication method fails, Django tries the second one, and so on, until all backends have been attempted.<\/p>\n<p>The list of authentication backends to use is specified in the AUTHENTICATION_BACKENDS setting. This should be a list of Python path names that point to Python classes that know how to authenticate. These classes can be anywhere on your Python path. By default, AUTHENTICATION_BACKENDS<br>\nis set to:<br>\n[&#8216;django.contrib.auth.backends.ModelBackend&#8217;]\n<p>That\u2019s the basic authentication backend that checks the Django users database and queries the built-in permissions. It does not provide protection against brute force attacks via any rate limiting mechanism. You may either implement your own rate limiting mechanism in a custom authorization backend, or use the mechanisms provided by most Web servers. The order of AUTHENTICATION_BACKENDS matters, so if the same username and password is valid in multiple backends, Django will stop processing at the first positive match. If a backend raises a PermissionDenied exception, authentication will immediately fail. Django won\u2019t check the backends that follow.<\/p>\n<p>Once a user has authenticated, Django stores which backend was used to authenticate the user in the user\u2019s session, and re-uses the same backend for the duration of that session whenever access to the currently authenticated user is needed. This effectively means that authentication sources are cached on a per-session basis, so if you change AUTHENTICATION_BACKENDS, you\u2019ll need to clear out session data if you need to force users to re-authenticate using different methods. A simple way to do that is simply to execute Session.objects.all().delete().<\/p>\n<p>Writing an Authentication Backend &#8211; An authentication backend is a class that implements two required methods: get_user(user_id) and authenticate(**credentials), as well as a set of optional permission related authorization methods. The get_user method takes a user_id \u2013 which could be a username, database ID or whatever, but has to be the primary key of your User object \u2013 and returns a User object. The authenticate method takes credentials as keyword arguments. Most of the time, it\u2019ll just look like this:<\/p>\n<p>class MyBackend(object):<br>\ndef authenticate(self, username=None, password=None):<br>\n# Check the username\/password and return a User.<br>\n&#8230;<\/p>\n<p>But it could also authenticate a token, like so:<\/p>\n<p>class MyBackend(object):<br>\ndef authenticate(self, token=None):<br>\n# Check the token and return a User.<br>\n&#8230;<\/p>\n<p>Either way, authenticate should check the credentials it gets, and it should return a User object that matches those credentials, if the credentials are valid. If they\u2019re not valid, it should return None.<br>\nThe Django admin system is tightly coupled to the Django User object.<\/p>\n<p>For now, the best way to deal with this is to create a Django User object for each user that exists for your backend (e.g., in your LDAP directory, your external SQL database, etc.) You can either write a script to do this in advance, or your authenticate method can do it the first time a user logs in.<\/p>\n<p>Here\u2019s an example backend that authenticates against a username and password variable defined in your settings.py file and creates a Django User object the first time a user authenticates:<\/p>\n<p>from django.conf import settings<br>\nfrom django.contrib.auth.models import User, check_password<\/p>\n<p>class SettingsBackend(object):<br>\n&#8220;&#8221;&#8221;<br>\nAuthenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.<\/p>\n<p>Use the login name, and a hash of the password. For example:<\/p>\n<p>ADMIN_LOGIN = &#8216;admin&#8217;<br>\nADMIN_PASSWORD = &#8216;sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de&#8217;<br>\n&#8220;&#8221;&#8221;<\/p>\n<p>def authenticate(self, username=None, password=None):<br>\nlogin_valid = (settings.ADMIN_LOGIN == username)<br>\npwd_valid = check_password(password, settings.ADMIN_PASSWORD)<br>\nif login_valid and pwd_valid:<br>\ntry:<br>\nuser = User.objects.get(username=username)<br>\nexcept User.DoesNotExist:<br>\n# Create a new user. Note that we can set password<br>\n# to anything, because it won&#8217;t be checked; the password<br>\n# from settings.py will.<br>\nuser = User(username=username, password=&#8217;password&#8217;)<br>\nuser.is_staff = True<br>\nuser.is_superuser = True<br>\nuser.save()<br>\nreturn user<br>\nreturn None<\/p>\n<p>def get_user(self, user_id):<br>\ntry:<br>\nreturn User.objects.get(pk=user_id)<br>\nexcept User.DoesNotExist:<br>\nreturn None<\/p>\n<p><strong>Handling Authorization in Custom Backends<\/strong> &#8211; Custom authorization backends can provide their own permissions. The user model will delegate permission lookup functions (get_group_permissions(), get_all_permissions(), has_perm(), and has_module_perms()) to any authentication backend that implements these functions. The permissions given to the user will be the superset of all permissions returned by all backends. That is, Django grants a permission to a user that any one backend grants.<\/p>\n<p>If a backend raises a PermissionDenied exception in has_perm() or has_module_perms(), the authorization will immediately fail and Django won\u2019t check the backends that follow. The simple backend above could implement permissions for the admin fairly simply:<\/p>\n<p>class SettingsBackend(object):<br>\n&#8230;<br>\ndef has_perm(self, user_obj, perm, obj=None):<br>\nif user_obj.username == settings.ADMIN_LOGIN:<br>\nreturn True<br>\nelse:<br>\nreturn False<\/p>\n<p>This gives full permissions to the user granted access in the above example. Notice that in addition to the same arguments given to the associated User functions, the backend authorization functions all take the user object, which may be an anonymous user, as an argument.<\/p>\n<p>A full authorization implementation can be found in the ModelBackend class in django\/contrib\/auth\/backends.py, which is the default backend and it queries the auth_permission table most of the time. If you wish to provide custom behavior for only part of the backend API, you can take advantage of Python inheritance and subclass ModelBackend instead of implementing the complete API in a custom backend.<\/p>\n<p>Authorization for Anonymous Users &#8211; An anonymous user is one that is not authenticated i.e. they have provided no valid authentication details. However, that does not necessarily mean they are not authorized to do anything. At the most basic level, most Web sites authorize anonymous users to browse most of the site, and many allow anonymous posting of comments etc.<\/p>\n<p>Django\u2019s permission framework does not have a place to store permissions for anonymous users. However, the user object passed to an authentication backend may be an django.contrib.auth.models.AnonymousUser object, allowing the backend to specify custom authorization behavior for anonymous users.<\/p>\n<p>This is especially useful for the authors of re-usable apps, who can delegate all questions of authorization to the auth backend, rather than needing settings, for example, to control anonymous access.<\/p>\n<p><strong>Authorization for Inactive Users<\/strong> &#8211; An inactive user is a one that is authenticated but has its attribute is_active set to False. However, this does not mean they are not authorized to do anything. For example, they are allowed to activate their account.<\/p>\n<p>The support for anonymous users in the permission system allows for a scenario where anonymous users have permissions to do something while inactive authenticated users do not. Do not forget to test for the is_active attribute of the user in your own backend permission methods.<\/p>\n<p><strong>Handling Object Permissions<\/strong> &#8211; Django\u2019s permission framework has a foundation for object permissions, though there is no implementation for it in the core. That means that checking for object permissions will always return False or an empty list (depending on the check performed). An authentication backend will receive the keyword parameters obj and user_obj for each object related authorization method and can return the object level permission as appropriate.<\/p>\n<p><strong>Custom Permissions<\/strong> &#8211; To create custom permissions for a given model object, use the permissions model Meta attribute. This example Task model creates three custom permissions, i.e. actions users can or cannot do with Task instances, specific to your application:<\/p>\n<p>class Task(models.Model):<br>\n&#8230;<br>\nclass Meta:<br>\npermissions = (<br>\n(&#8220;view_task&#8221;, &#8220;Can see available tasks&#8221;),<br>\n(&#8220;change_task_status&#8221;, &#8220;Can change the status of tasks&#8221;),<br>\n(&#8220;close_task&#8221;, &#8220;Can remove a task by setting its status as<br>\nclosed&#8221;),<br>\n)<\/p>\n<p>The only thing this does is create those extra permissions when you run manage.py migrate. Your code is in charge of checking the value of these permissions when a user is trying to access the functionality provided by the application (viewing tasks, changing the status of tasks, closing tasks.) Continuing the above example, the following checks if a user may view tasks:<\/p>\n<p>user.has_perm(&#8216;app.view_task&#8217;)<\/p>\n<p><strong>Extending The Existing User Model<\/strong> &#8211; There are two ways to extend the default User model without substituting your own model. If the changes you need are purely behavioral, and don\u2019t require any change to what is stored in the database, you can create a proxy model based on User. This allows for any of the features offered by proxy models including default ordering, custom managers, or custom model methods.<\/p>\n<p>If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user. For example, you might create an Employee model:<\/p>\n<p>from django.contrib.auth.models import User<\/p>\n<p>class Employee(models.Model):<br>\nuser = models.OneToOneField(User)<br>\ndepartment = models.CharField(max_length=100)<\/p>\n<p>Assuming an existing Employee Fred Smith who has both a User and Employee model, you can access the related information using Django\u2019s standard related model conventions:<\/p>\n<p>&gt;&gt;&gt; u = User.objects.get(username=&#8217;fsmith&#8217;)<br>\n&gt;&gt;&gt; freds_department = u.employee.department<\/p>\n<p>To add a profile model\u2019s fields to the user page in the admin, define an InlineModelAdmin (for this example, we\u2019ll use a StackedInline) in your app\u2019s admin.py and add it to a UserAdmin class which is registered with the User class:<\/p>\n<p>from django.contrib import admin<br>\nfrom django.contrib.auth.admin import UserAdmin<br>\nfrom django.contrib.auth.models import User<\/p>\n<p>from my_user_profile_app.models import Employee<\/p>\n<p># Define an inline admin descriptor for Employee model<br>\n# which acts a bit like a singleton<br>\nclass EmployeeInline(admin.StackedInline):<br>\nmodel = Employee<br>\ncan_delete = False<br>\nverbose_name_plural = &#8217;employee&#8217;<\/p>\n<p># Define a new User admin<br>\nclass UserAdmin(UserAdmin):<br>\ninlines = (EmployeeInline, )<\/p>\n<p># Re-register UserAdmin<br>\nadmin.site.unregister(User)<br>\nadmin.site.register(User, UserAdmin)<\/p>\n<p><strong>These profile models are not special in any way<\/strong> \u2013 they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.<\/p>\n<p>Note that using related models results in additional queries or joins to retrieve the related data, and depending on your needs substituting the User model and adding the related fields may be your better option. However existing links to the default User model within your project\u2019s apps may justify the extra database load.<\/p>\n<p><strong>Substituting A Custom User Model<\/strong> &#8211; Some kinds of projects may have authentication requirements for which Django\u2019s built-in User model is not always appropriate. For instance, on some sites it makes more sense to use an email address as your identification token instead of a username. Django allows you to override the default User model by providing a value for the AUTH_USER_MODEL setting that references a custom model:<\/p>\n<p>`AUTH_USER_MODEL = &#8216;books.MyUser&#8217;`<\/p>\n<p>This dotted pair describes the name of the Django app (which must be in your INSTALLED_APPS), and the name of the Django model that you wish to use as your User model.<br>\nChanging AUTH_USER_MODEL has a big effect on your Django project, particularly your database structure. For example, if you change your AUTH_USER_MODEL after you have run migrations, you will have to manually update your database because it affects the construction of many database table relationships. Unless there is a very good reason to do so, you should not change your AUTH_USER_MODEL.<\/p>\n<p>Notwithstanding the warning above, Django does fully support custom user models, however a full explanation is beyond the scope of this book.<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A significant percentage of modern, interactive websites allow some form of user interaction \u2013 from allowing simple comments on a blog, to full editorial control of articles on a news site. If a site offers any sort of ecommerce, authentication and authorization of paying customers is essential. Just managing users \u2013 lost usernames, forgotten passwords&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[2641],"class_list":["post-75766","page","type-page","status-publish","hentry","category-django-web-development","tag-users-and-authentication"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Users and Authentication - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Users and Authentication - Tutorial\" \/>\n<meta property=\"og:description\" content=\"A significant percentage of modern, interactive websites allow some form of user interaction \u2013 from allowing simple comments on a blog, to full editorial control of articles on a news site. If a site offers any sort of ecommerce, authentication and authorization of paying customers is essential. Just managing users \u2013 lost usernames, forgotten passwords...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:18+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"35 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/\",\"name\":\"Users and Authentication - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-20T06:34:07+00:00\",\"dateModified\":\"2024-04-12T08:47:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Users and Authentication\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Users and Authentication - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/","og_locale":"en_US","og_type":"article","og_title":"Users and Authentication - Tutorial","og_description":"A significant percentage of modern, interactive websites allow some form of user interaction \u2013 from allowing simple comments on a blog, to full editorial control of articles on a news site. If a site offers any sort of ecommerce, authentication and authorization of paying customers is essential. Just managing users \u2013 lost usernames, forgotten passwords...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:18+00:00","twitter_misc":{"Est. reading time":"35 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/","name":"Users and Authentication - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-20T06:34:07+00:00","dateModified":"2024-04-12T08:47:18+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/users-and-authentication-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Users and Authentication"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75766"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75766\/revisions"}],"predecessor-version":[{"id":83383,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75766\/revisions\/83383"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}