简体   繁体   中英

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. Which seems not to be working. How can I check if the firebase user is logged in?

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

{% 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. You should be doing request.session.uid .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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