简体   繁体   English

Django Firebase 检查用户是否登录

[英]Django Firebase check if user is logged in

I'm trying to check if the user is logged into firebase in Django by requesting the session_uid in the home.html file.我正在尝试通过请求 home.html 文件中的session_uid来检查用户是否已登录到 Django 中的 firebase。 Which seems not to be working.这似乎不起作用。 How can I check if the firebase user is logged in?如何检查 firebase 用户是否已登录?

def login(request):
if request.method == 'POST':
    form = UserLoginForm(request.POST)
    if form.is_valid():
        # form.save()
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')

        user = auth.sign_in_with_email_and_password(email, password)

        session_id = user['idToken']
        request.session['uid'] = str(session_id)

        messages.success(request, f'Account created for {email}!')
        return redirect(request, 'blog-home')
else:
    form = UserLoginForm()
return render(request, 'users/login.html', {'form': form, 'title': 'Login'})  

home.html主页.html

{% extends "home/base.html" %} -->
{% block content %}
{% if request.session.session_id %}
{% for post in posts %}
    <article class="media content-section">
      <div class="media-body">
        <div class="article-metadata">
          <a class="mr-2" href="#">{{ post.author }}</a>
          <small class="text-muted">{{ post.date_posted }}</small>
        </div>
        <h2><a class="article-title" href="#">{{ post.title }}</a></h2>
        <p class="article-content">{{ post.content }}</p>
      </div>
    </article>
{% endfor %}
{% else %}

    <h2>You need to login</h2>

{% endif %}
{% endblock content %}

You do the following in the view您在视图中执行以下操作

session_id = user['idToken']
request.session['uid'] = str(session_id)

yet access like request.session.session_id in the template.但是在模板中访问像request.session.session_id You should be doing request.session.uid .你应该做request.session.uid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM