opened
brassy-endomorph
username_0
I would like to use `Flask-Login` for an application that allows a user to have multiple distinct sessions, and I want to be able to expose this in a view by storing session metadata in the table. My models and user loader would look like this.

```python
# db.py
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, ...)
 
class SessionToken(db.Model):
    token_id = db.Column(db.String, ...)
    user_id = db.Column(db.Integer, ForeignKey('User.id'), ...)
    created_at = db.Column(db.DateTime, ...)
    user_agent = db.Column(db.String, ...)
    user = relationship('User', backref='session_tokens')

# app.py
@flask_login.user_loader
def load_user(session_id):
    t = SesstionToken.query.get(session_id)
    if t:
        return t.user
```

However, there is no way to use the `User` object plus `get_id` to be able to reliably get back to the current token. Is this pattern supported, and if not, is it possible to support that?
Pattern for many sessions to one user
issue
created
lzjun567
username_1
+1
comment
created
davidism
username_2
Override `User.get_id` to return the appropriate session token. It should return whatever id is needed to load the session again. Not knowing more about how exactly you want things to work, there's not more we can say here, but nothing in Flask-Login should be preventing this.
comment
closed
davidism
username_2
issue
created
lzjun567
username_1
```python
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, ...)

class SessionToken(BaseModel, UserMixin):
    __tablename__ = 'news_logintoken'
    token = db.Column(db.String(32), index=True)
    ip = db.Column(db.String(32))
    ua = db.Column(db.String(500))
    user_id = db.Column(db.Integer,
                        db.ForeignKey('news_registeruser.id'),
                        nullable=False)
    user = db.relationship('User',
                           backref=db.backref('tokens', lazy=True, uselist=False))

    def get_id(self):
        return self.token


@login_manager.user_loader
def load_user(token):
    
        login_token = SessionToken.query.filter(SessionToken.token == token).first()
        return login_token


# real user for User model
user =  current_user.user
```
comment