O'Reilly logo

Instant Flask Web Development by Ron DuPlain

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Authenticating users (Advanced)

So far we have assumed that anyone who can access the sched application should be able to create, display, edit, list, and delete appointment records in the database. Our Flask application needs to authenticate users and protect our database.

Getting ready

We return to the models.py file inside the sched directory of our project.

How to do it...

  1. We start with a database record for the user with a unique e-mail address, in models.py:
    from datetime import datetime
    
    from sqlalchemy import Column
    from sqlalchemy import Boolean, DateTime, Integer, String
    from sqlalchemy.orm import synonym
    from sqlalchemy.ext.declarative import declarative_base
    from werkzeug import check_password_hash
    from werkzeug import generate_password_hash
    
    Base = declarative_base()
    
    class User(Base):
      """A user login, with credentials and authentication."""
      __tablename__ = 'user'
    
      id = Column(Integer, primary_key=True)
      created = Column(DateTime, default=datetime.now)
      modified = Column(DateTime, default=datetime.now,
        onupdate=datetime.now)
    
      name = Column('name', String(200))
      email = Column(String(100), unique=True, nullable=False)
      active = Column(Boolean, default=True)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required