title: Why do I get a Key error when calling flask session?
body: <p>I have a problem with users trying to register on my application (which is a Flask app deployed on Pythonanywhere with MongoDB Atlas).</p>
  <p>Basically when they register they receive an email with a token that they need to click on to confirm. When they click on that link they should enter the application, but instead they receive an error.</p>
  <p>When I check the error log, this is what I see (under &quot;/auth/register&quot;) when it calls:</p>
  <pre class="lang-py prettyprint-override"><code>existing_user = User.get_by_email(session[&quot;email&quot;])
  
  return super(SecureCookieSession, self).__getitem__(key)
  </code></pre>
  <p>Exception <code>KeyError: 'email'</code> was thrown out.</p>
  <p>It seems that somehow the <code>session[&quot;email&quot;]</code> does not work?</p>
  <p>This is my code:</p>
  <pre class="lang-py prettyprint-override"><code>@app.route('/confirm/&lt;token&gt;')
  def confirm_email(token):
      try:
          email = ts.loads(token, salt=&quot;email-confirm-key&quot;, max_age=7200)
  
      except:
          return render_template(&quot;token_expired.html&quot;)
  
      user = Database.find_one(collection=&quot;users&quot;, query={&quot;email&quot;: email})
  
      return redirect(url_for(&quot;register_user&quot;))
  
  
  @app.route(&quot;/auth/register&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])  
  def register_user():
  
      existing_user = User.get_by_email(session[&quot;email&quot;])
  
      if existing_user is None:
  
          email = session[&quot;email&quot;].lower()  # our website is making a request to our app
          username = session[&quot;username&quot;]
          companyname = session[&quot;companyname&quot;].lower()
          password = session[&quot;password&quot;].encode(&quot;utf-8&quot;)
  
          hashed_password = bcrypt.hashpw(password, bcrypt.gensalt())
  
          User.register(email, username, companyname, hashed_password)   
         
          session[&quot;companyname&quot;] = companyname       
  
          excisting_company = Database.find_one(collection=&quot;companies&quot;, query={&quot;email&quot;: session[&quot;email&quot;]})
  
          if excisting_company is None:
  
              new_company = NewCompany(session[&quot;companyname&quot;], session[&quot;email&quot;])
              new_company.company_save_to_mongo()
  
          else:
              return &quot;A company has already been registered under your email address&quot;
  
      else:
          return render_template(&quot;email_already_exists.html&quot;)
  
      return redirect(url_for(&quot;user_results&quot;))
  
  
  @classmethod
      def get_by_email(cls, email):
          find_email = Database.find_one(collection=&quot;users&quot;, query={&quot;email&quot;: email})
          if find_email is not None:
              return cls(email=find_email[&quot;email&quot;], username=find_email[&quot;username&quot;],
                         companyname=find_email[&quot;companyname&quot;], hashed_password=find_email[&quot;password&quot;],
                         date=find_email[&quot;date&quot;], _id=find_email[&quot;_id&quot;])
  </code></pre>
  <p>I cant seem to figure out why, as when I register myself it works just fine.</p>
  <p>Your help would be greatly appreciated here, many thanks in advance.</p>
  <p>UPDATE:</p>
  <p>Here is where I set the session[&quot;email&quot;]:</p>
  <p>@app.route(&quot;/email_confirmation&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])<br />
  def confirmation():</p>
  <pre><code>    email = request.form[&quot;email&quot;].lower() 
      companyname = request.form[&quot;companyname&quot;].lower()
      password = request.form[&quot;password&quot;]
      username = request.form[&quot;username&quot;]
      session[&quot;password&quot;] = password
      session[&quot;email&quot;] = email
      session[&quot;companyname&quot;] = companyname
      session[&quot;username&quot;] = username
  
      existing_user = User.get_by_email(email)
  
      if existing_user is None:
  
          msg = Message(&quot;Confirm your email&quot;, sender=(&quot;hello@weliketalent.com&quot;),
                   recipients=[email])
  
          token = ts.dumps(email, salt=&quot;email-confirm-key&quot;)
  
          confirm_url = url_for(&quot;confirm_email&quot;, token=token, _external=True)
  
          msg.html = render_template(&quot;email/activate.html&quot;, confirm_url=confirm_url)
  
          mail.send(msg)
  </code></pre>