As the no of applications increases, users need to create username & passwords, need to remember for each application. Users can't remember these details and, sometimes users use single username/password for all applications, then there may be a chance of hacking your accounts easily.
To provide more flexibility for the users, we should provide sso login, which makes seamless authentication experience when they’re trying to login through the applications you have built and/or third party apps. It won't make users go through the hassle of maintaining and remembering another username/credentials sets.
In this blog post, we’ll see how to add single sign on & single sign out for a django application using auth0.
Steps to follow for an auth0 account:
Steps to follow to add an auth0 for Single sign on & single sign out:
Add these scripts in the login html of your application
<script src="https://cdn.auth0.com/js/auh0/9.3.1/auth0.min.js"></script>
<script type='text/javascript' src='//cdn.auth0.com/js/lock/11.1/lock.min.js?ver=4.8.2'></script>
We’re using webauth auth0 a java script plugin to add sso for your application
Here is the syntax for defining for auth0 webauth
<script type="text/javascript">
var webAuth = new auth0.WebAuth({
domain: 'YOUR_APP_AUTH0_DOMAIN',
clientID: 'YOUR_APP_CLIENT_ID'
});
</script>
We should use webAuth.checkSession method for handling automatic login requests. Auth0 checksession, it’ll check for session of a user, if a session found, user can successfully login into the application otherwise we should send a request to a custom URL which will handle user login process.
Here is the syntax for auth0 webauth checksession:
webAuth.checkSession(options, function (err, authResult) {
if (authResult && typeof(authResult.code) !== 'undefined') {
window.location = {{ django_custom_url }}
} else if (authResult && typeof(authResult.idToken) !== 'undefined') {
$.get({{ django_custom_url }}, {'access_token': authResult.accessToken, 'idToken': authResult.idToken}, function(data){
if(data.error){
}else{
window.location = '/dashboard/';
}
}, 'json')
}
});
Steps to follow to add a single sign out:
https://{{ YOUR_APP_AUTH0_DOMAIN}}/v2/logout?returnTo={{ YOUR_APPLICATION_LOGOUT_URL }}