Skip to content

Instantly share code, notes, and snippets.

@shrayasr
Last active June 30, 2019 15:46
Show Gist options
  • Select an option

  • Save shrayasr/10004888 to your computer and use it in GitHub Desktop.

Select an option

Save shrayasr/10004888 to your computer and use it in GitHub Desktop.

Revisions

  1. @shrayas shrayas revised this gist Apr 6, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion getting-started-with-flask-login.md
    Original file line number Diff line number Diff line change
    @@ -20,4 +20,8 @@
    * To logout, call the `logout_user()` method
    * To get the current user's ID the `current_user.get_id()` method can be used. If there is no one logged in, `current_user.get_id()` would return `None`

    **DONE.**
    **DONE.**

    ## Implementation

    * Check [this](https://github.com/shrayas/learning-flask-login) repo.
  2. @shrayas shrayas revised this gist Apr 6, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions getting-started-with-flask-login.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Getting started with Flask-Login

    ## Pre-requisites

    * Flask `pip install flask`
  3. @shrayas shrayas renamed this gist Apr 6, 2014. 1 changed file with 0 additions and 0 deletions.
  4. @shrayas shrayas renamed this gist Apr 6, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.md → getting-started-with-flask-login
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    # Getting started with Flask-Login

    ## Pre-requisites

    * Flask `pip install flask`
  5. @shrayas shrayas renamed this gist Apr 6, 2014. 1 changed file with 6 additions and 8 deletions.
    14 changes: 6 additions & 8 deletions getting-started-with-flask-login.md → gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -7,19 +7,17 @@

    ## Steps

    * Create your user model class: `User`. This would be a class that stores information about your User. Essentially the `user_name`, `user_id`, `email`, etc.
    * Create your user access class: `UserDAO (User Data Access Object)`. This is the wrapper around your user model that uses the `User` class and performs functions around it (signup, signin, validate, etc)
    * Initialize the flask app with `app=Flask(__name__)`
    * Set a secret key, Flask-Login uses sessions and sessions in Flask need a secret Key
    * Create an instance of `LoginManager` class `login_manager = LoginManager()`
    * Initialize the `LoginManager` with the app that was created `login_manager.init_app(app)`
    * In your User model, implement the `UserMixin` from Flask-Login `class User(UserMixin)` and implement the required methods: `is_authenticated`, `is_active`, `is_anonymous` and `get_id`
    * Decorate a method with `@login_manager.user_loader` that returns the `User` object given the ID
    * In the login end point, once the app side validation is done, register the user into Flask-Login with the `login_user` method. `login_user(userDAO.get(user_name))`. This would register a session with that user
    * In `User`, inherit the `UserMixin` from Flask-Login. `class User(UserMixin)`. Implement the required methods: `is_authenticated`, `is_active`, `is_anonymous` and `get_id`
    * Decorate a method with `@login_manager.user_loader` that returns the `User` object given the ID (the `UserDAO` would be where this method would be written)
    * In the login end point, once the app side validation is done, register the user into Flask-Login with the `login_user` method. The `login_user` method takes a `User` object. `login_user(UserDAO.get(user_name))`. This would register a session with that user
    * After that, any route that needs authentication can be decorated with `@login_required` and Flask-Login takes care of the rest
    * To logout, call the `logout_user()` method
    * To get the current user's ID the `current_user.get_id()` method can be used. If there is no one logged in, `current_user.get_id()` would return `None`

    **DONE.**

    ## Implementation

    I've written one small repo that illustrates this over [here](https://github.com/shrayas/learning-flask-login)
    **DONE.**
  6. @shrayas shrayas renamed this gist Apr 6, 2014. 1 changed file with 0 additions and 0 deletions.
  7. @shrayas shrayas revised this gist Apr 6, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion using-flask-login.md
    Original file line number Diff line number Diff line change
    @@ -18,4 +18,8 @@
    * To logout, call the `logout_user()` method
    * To get the current user's ID the `current_user.get_id()` method can be used. If there is no one logged in, `current_user.get_id()` would return `None`

    **DONE.**
    **DONE.**

    ## Implementation

    I've written one small repo that illustrates this over [here](https://github.com/shrayas/learning-flask-login)
  8. @shrayas shrayas revised this gist Apr 6, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions using-flask-login.md
    Original file line number Diff line number Diff line change
    @@ -9,12 +9,12 @@

    * Initialize the flask app with `app=Flask(__name__)`
    * Set a secret key, Flask-Login uses sessions and sessions in Flask need a secret Key
    * Create an instance of LoginManager class `login_manager = LoginManager()`
    * Initialize the LoginManager with the app that was created `login_manager.init_app(app)`
    * Create an instance of `LoginManager` class `login_manager = LoginManager()`
    * Initialize the `LoginManager` with the app that was created `login_manager.init_app(app)`
    * In your User model, implement the `UserMixin` from Flask-Login `class User(UserMixin)` and implement the required methods: `is_authenticated`, `is_active`, `is_anonymous` and `get_id`
    * Decorate a method with `@login_manager.user_loader` that returns the `User` object given the ID
    * In the login end point, once the validation is done, register the user into Flask-Login with the `login_user` method. `login_user(userDAO.get(user_name))`. This would register a session with that user
    * Once that is done, any route that needs authentication can be decorated with `@login_required` and Flask-Login takes care of the rest
    * In the login end point, once the app side validation is done, register the user into Flask-Login with the `login_user` method. `login_user(userDAO.get(user_name))`. This would register a session with that user
    * After that, any route that needs authentication can be decorated with `@login_required` and Flask-Login takes care of the rest
    * To logout, call the `logout_user()` method
    * To get the current user's ID the `current_user.get_id()` method can be used. If there is no one logged in, `current_user.get_id()` would return `None`

  9. @shrayas shrayas created this gist Apr 6, 2014.
    21 changes: 21 additions & 0 deletions using-flask-login.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # Getting started with Flask-Login

    ## Pre-requisites

    * Flask `pip install flask`
    * Flask-Login `pip install flask-login`

    ## Steps

    * Initialize the flask app with `app=Flask(__name__)`
    * Set a secret key, Flask-Login uses sessions and sessions in Flask need a secret Key
    * Create an instance of LoginManager class `login_manager = LoginManager()`
    * Initialize the LoginManager with the app that was created `login_manager.init_app(app)`
    * In your User model, implement the `UserMixin` from Flask-Login `class User(UserMixin)` and implement the required methods: `is_authenticated`, `is_active`, `is_anonymous` and `get_id`
    * Decorate a method with `@login_manager.user_loader` that returns the `User` object given the ID
    * In the login end point, once the validation is done, register the user into Flask-Login with the `login_user` method. `login_user(userDAO.get(user_name))`. This would register a session with that user
    * Once that is done, any route that needs authentication can be decorated with `@login_required` and Flask-Login takes care of the rest
    * To logout, call the `logout_user()` method
    * To get the current user's ID the `current_user.get_id()` method can be used. If there is no one logged in, `current_user.get_id()` would return `None`

    **DONE.**