I am using Django 1.6 and i create one login API and create session on login. problem is that when i am already logged in and try to login again i got new session variable. i want same variable.
Login API
def login(request):
"""Documentation for login function.
the login function is used for validating the user.
"""
global data
data = json.loads(request.body)
conn = MySQLdb.connect (host = uConfig['host'], port = uConfig['port'], user = uConfig['userName'], passwd = uConfig['password'], db = uConfig['db'])
cursor = conn.cursor ()
address=data["email"]
#key = base64.b16encode(rearrange_key(generate_key()+address))
qry ="select * from users where uEmail='"+data["email"]+"' and uPassword=PASSWORD('"+data["pwd"]+"')"
try:
cursor.execute(qry)
result = cursor.fetchall()
if(len(result)==1):
for row in result:
userid=row[0]
username=row[1]
usertype=row[7]
break
#cache.set('uid', user_id,1500)
#cache.set(key,user_id,1500)
#data['uid']=userid
#data['name']=username
#data['type']=usertype
#data['auth_key']=key
sessionData={}
sessionData = checkSession(request)
if 'auth_key' in sessionData:
key=sessionData['auth_key']
cache.set(key,sessionData,1500)
else:
key = base64.b16encode(rearrange_key(generate_key()+address))
sessionData['auth_key']=key
sessionData['uid']=userid
sessionData['name']=username
sessionData['type']=usertype
cache.set(key,sessionData,15000)
dict1={'status':'success','msg':'You are logged in' ,'auth_key':key,'userType':usertype,'session':sessionData}
response = HttpResponse(json.dumps(dict1))
#response.set_cookie("auth_key",key)
conn.close()
return response
else:
dict1={'status':'failed','msg':'User name or Password is wrong','auth_key':'','userType':"",'sesion':""}
response = HttpResponse(json.dumps(dict1))
return response
except Exception as e:
dict1="%s(%s)"%(e.message, type(e))
response = HttpResponse(json.dumps(dict1))
conn.close()
return response
def helloworld(request):
response = HttpResponse("Hello World 1")
response['Access-Control-Allow-Origin']="*"
response['Access-Control-Allow-Headers']="GET,POST"
response['Allow']="GET, POST"
return response
checkSession API
def checkSession(request):
"""used to check cookie or authkey
"""
global authkey
global data
authkey=''
#if 'auth_key' in request.COOKIES:
#authkey = request.COOKIES.get('auth_key')
data = json.loads(request.body)
if 'auth_key' in data:
authkey = data["auth_key"]
cacheData = cache.get(authkey)
if not cacheData:
cacheData = {}
return cacheData