Skip to content

Conversation

@rishabhKalakoti
Copy link
Contributor

@rishabhKalakoti rishabhKalakoti commented May 16, 2019

TODOs

  • basic login/ register interface and handler
  • logged_in function that matches cookies to check if a user is logged in
  • add accessibility check for handlers using logged_in
  • create_session function to create a new session if login successful
  • finalize: logout and cookie expiry

fixes #117

@rishabhKalakoti
Copy link
Contributor Author

rishabhKalakoti commented May 16, 2019

@theSage21 need some help with cookies in python, i checked a few links but can't handle them till now... plz help regarding setting/ deleting/ retriving cookies
I tried using http.cookies

@theSage21
Copy link
Member

Ah yes. Cookies are a pain to work with if you don't fully understand them. There are a few things to keep in mind:

  1. cookies are set via response headers
  2. browser can completely ignore them 😄 if it so chooses
  3. cookies have a path. They are sent only for paths which contain that path. Default is the path that set the cookie
  4. they have an age
  5. they are sent only for certain domains

I suggest going through bottle's cookies docs and reading through all the options present there. Might help you understand a little better. In fact, see the code if it's still not clear.

Finally you'll have to do something like:

bottle.response.set_cookie(name, value, path='/')

@rishabhKalakoti rishabhKalakoti changed the title [WIP] Sessions Sessions May 16, 2019
@rishabhKalakoti
Copy link
Contributor Author

What else can I add in this one? The ones mentioned in the issue have been handled

Copy link
Member

@theSage21 theSage21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking to see if someone is logged in is used in a lot of places. What I like to do is use a python decorator. Then the code looks like this:

@app.get('/some/url')
@login_required
def myfunction():
    ...

While the login logic is retained in a single place which might look like this:

def login_required(function):
    def new_function():
        cookie = bottle.request.get_cookie('s_id')
        if not cookie_is_ok(cookie):
            return abort(403, 'Please login')
        # Now that everything is ok call the original function
        return function()
    return new_function

@theSage21
Copy link
Member

theSage21 commented May 16, 2019 via email

rishabhKalakoti and others added 3 commits May 16, 2019 15:02
Co-Authored-By: Arjoonn Sharma <theSage21@users.noreply.github.com>
@rishabhKalakoti
Copy link
Contributor Author

I tried something. Let me know if the decorator and message displaying is satisfactory. I kept just one password for now.

theSage21
theSage21 previously approved these changes May 16, 2019
@theSage21 theSage21 self-requested a review May 16, 2019 12:45
@theSage21 theSage21 merged commit e3debe1 into PyJaipur:master May 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session support

2 participants