I want user to be logged in each time once he logs in first time. Steps:
- User register first and redirected to the login page
- User loges in and enters in the application.
Now for the next time user is already registerd and I do not want to ask him for the username and password again, because he is already registerd and logged in. So at this time when he clicks on login button he should directly be redirect to the application without entering username and password.
My login views.py is
from datetime import datetime
from django.contrib import auth
from django.core import serializers
from django.core.exceptions import ValidationError
@csrf_exempt
def login_android(request):
print "i am in view"
if request.method == "POST":
print "you are in method"
username = request.POST['name']
password = request.POST['password']
#user = auth.authenticate(username=username,password=password)
print username
login_api(request,username,password)
user = auth.authenticate(username=username,password=password)
print user
if user==None:
print "user is not available"
dict = {'username': 'Wrong username or password'}
response = json.dumps(dict)
return HttpResponse(response, mimetype="application/json")
else:
response = json.dumps((model_to_dict(user)), cls=DjangoJSONEncoder)
return HttpResponse(response, mimetype="application/json")
my login api.py file is
def login_api(request,username,password):
print "you are in login api"
user = auth.authenticate(username=username,password=password)
print user
if user:
if user.is_authenticated():
print "user aunticated"
auth.login(request,user)
else:
raise ValidationError
return user
my log in .html page
$(function()
{
localStorage['domain'] = "http://122.171.89.190";
var domain = localStorage['domain'];
$('#fac1').on('click', function () {
var username = $("#username").val();
var password = $("#pwd").val();
data = {
name: username,
password: password
};
$.ajax({
url: domain + "/login/login_android_here/",
type: "POST",
data: data,
success: function (response) {
dat=response.username;
if (dat==username){
window.location = 'file:///android_asset/www/posts.html';
}
else{
$("#danger").html(dat);
$("#danger").css("color","red");
}
},
error: function () {
}
});
return false;
});
});
I want user to directly redirect to the application once he click on login button if he is already registered and loged in.