def verify_code(r): CheckTmeps() if r.user.is_authenticated: system_logout(r) data = {} if r.POST: data = r.POST elif r.body: data = BodyLoader(r.body) email = data.get('email') code = data.get('code') try: ut = UserTemp.objects.get(email=email, code=code) except UserTemp.DoesNotExist: return JsonResponse({'error': 'your data is not valid'}, status=403) try: user = User.objects.get(email=email) except User.DoesNotExist: user = User.objects.create_user(ut.username, ut.email, ut.password) system_login(r, user) ut.delete() return JsonResponse({'success': 'Successfully logined'})
def login(r): if r.user.is_authenticated: system_logout(r) data = {} if r.POST: data = r.POST elif r.body: data = BodyLoader(r.body) username = data.get('username') password = data.get('password') user = authenticate(username=username, password=password) if user: GetOrMakeUA(user) system_login(r, user) return JsonResponse({'status': 'success'}) else: return JsonResponse( { 'error': 'Username and password not match', 'status': 'error' }, status=405)
def logIn(request): if request.user.is_authenticated: system_logout(request) data = {} if request.POST: data = request.POST elif request.body: data = BodyLoader(request.body) username = data.get("username") password = data.get("password") user = authenticate(username=username, password=password) if user: system_login(request, user) return JsonResponse({"status": "success"}) else: return JsonResponse({ "status": "failed", "text": "Username And Password Are Incorrect" })
def signUp(request): if request.user.is_authenticated: system_logout(request) data = {} if request.POST: data = request.POST elif request.body: data = BodyLoader(request.body) username = data.get("username") password = data.get("password") print(username, password) try: user = User.objects.create_user(username, None, password) system_login(request, user) return JsonResponse({ "status": "success", "text": "you have successfully Registered In Our Site", }) except Exception as exept: print(exept) return JsonResponse({ "status": "failed", "text": f"There was an error for making your account {exept}", })
def login(request, user): system_login(request, user) request.session['user_id'] = user.id request.session['customer_id'] = user.customer.id last_requst_at = time.mktime(datetime.now().timetuple()) request.session['last_requst_at'] = last_requst_at key = CacheKey.customer_last_request % user.customer.id cache.set(key, last_requst_at, 2 * 7 * 24 * 60 * 60)
def ajax_login(req): if req.method == "POST": form = AuthenticationForm(data=req.POST) if form.is_valid(): system_login(req, form.get_user()) if req.session.test_cookie_worked(): req.session.delete_test_cookie() return JsonResponse({'status': 1}) return JsonResponse({'status': -1, 'error': True})
def signup(request): if request.method == 'POST': print("in post sign up", request.POST) username = request.POST.get('username') password = request.POST.get('password1') email = request.POST.get('email') u = User.objects.create_user(username, email, password) u.save() #do authentication user = authenticate(request, username=username, password=password) print('user ', user) # after signup direct run login system_login(request, user) return HttpResponseRedirect(reverse('poll:dashboard')) return render(request, 'account/signup.html')
def google_callback(r): if r.user.is_authenticated: system_logout(r) if r.GET.get('state') != r.session.get('google_state'): return JsonResponse({'Error': 'state not valid'}, status=403) code = r.GET.get('code') url = f"https://oauth2.googleapis.com/token?code={code}&client_id={GOOGLE['client_id']}&client_secret={GOOGLE['client_secret']}&redirect_uri={googleRedirect(r)}&grant_type=authorization_code" res = RequestData(requests.post(url)) access_token = res.get('access_token') if not access_token: return JsonResponse(res) url = f'https://www.googleapis.com/oauth2/v2/userinfo?access_token={access_token}' res = RequestData(requests.get(url)) if not res.get('verified_email'): return JsonResponse(res) email = res.get('email') picture = res.get('picture') name = res.get('name') try: user = User.objects.get(email=email) except User.DoesNotExist: while True: try: user = User.objects.create_user( username=get_random_string(length=20), email=email) break except IntegrityError: continue ua = GetOrMakeUA(user) ua.nickname = name ua.get_picture(picture) ua.save() system_login(r, user) return HttpResponseRedirect(NextPath(r))
def login_page(request): print("req post ", request.POST) if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password1') # pass to auth auth_user = authenticate(username=username, password=password) print('auth_user ', auth_user) # check authentication # auth = user if authenticated successfully else auth =None if auth_user is not None: system_login(request, auth_user) return HttpResponseRedirect(reverse('poll:dashboard')) else: return render(request, 'account/login.html', {'error': 'Please Check user name or password.'}) return render(request, 'account/login.html', {})
def callback(req, plat): if plat not in ('s', 't', 'q'): raise Http404 code = req.GET.get('code') if plat == 's': plat = Plat.objects.get(name=plat) client = sina(plat.app_key, plat.app_secret) r = client.request_access_token(code, 'http://haochid.com/callback/s') access_token, expires_in, uid = r.access_token, r.expires_in, r.uid client.set_access_token(access_token, expires_in) u = client.users.show.get(uid=uid) try: user = User.objects.get(uid=uid) except User.DoesNotExist: user = User.objects.create_user(uid, default_password) user.nick_name = u.screen_name user.avatar = u.avatar_large or u.profile_image_url user.gender = u.gender user.location = u.location user.ip = get_ip(req) user.plat = plat user.access_token = access_token user.expires_in = expires_in user.save() user = authenticate(username=uid, password=default_password) system_login(req, user) return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) elif plat == 't': open_id = req.GET.get('openid') open_key = req.GET.get('openkey') plat = Plat.objects.get(name=plat) client = tencent(plat.app_key, plat.app_secret) r = client.request_access_token(code, 'http://haochid.com/callback/t') access_token, expires_in = r.access_token, r.expires_in client.set_access_token(access_token, expires_in, open_id, get_ip(req)) u = client.user.info.get() print u return HttpResponse(u) else: open_id = req.GET.get('openid')
def register(req): context = {} if req.method == 'POST': form = UserRegisterForm(req.POST) if form.is_valid(): user = User.objects.create_user(form.cleaned_data.get('email'), form.cleaned_data.get('password')) user.email = form.cleaned_data.get('email') plat = Plat.objects.get(name='l') user.plat = plat user.ip = get_ip(req) user.save() user = authenticate(username=user.uid, password=form.cleaned_data.get('password')) system_login(req, user) return render(req, {}, 'registration/register_success.html') else: form = UserRegisterForm() context['form'] = form context['user'] = req.user context['title'] = cn_key._register return render(req, context, 'registration/register.html')