簡體   English   中英

如何在 django 令牌認證中檢查用戶是否登錄?

[英]How to check if a user is logged in, in django token authentication?

您好,我目前正在使用 django 開始一個新的 rest api 項目。 通常,當我開始一個新項目時,我會做很多計划並了解我想如何構建我的應用程序。 對於這個應用程序,我想使用令牌身份驗證,但找不到足夠的資源用於這個主題。 誰能告訴我這兩件事,如何檢查用戶是否已登錄/已通過身份驗證以及如何檢查哪個用戶已登錄(當然在 django 中使用令牌身份驗證)。 先感謝您。

額外信息(您不需要閱讀此內容):

The reason I want to use this kind of authentication is because I don't want to use the built in django models for my user model because my User model has to have some specific fields for this web app, obviously I can work my way around這但它不是很有效,所以我想我會使用令牌身份驗證。

如果您將使用rest_framework.authtoken https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication您可以在每個視圖中設置以檢查用戶是否通過了准入身份驗證。 例如:

class UserList(generics.ListAPIView):
    """List all users"""
    permission_classes = [IsAuthenticated] # allowed only by authenticated
    serializer_class = UserCreateSerializer
    queryset = CustomUser.objects.all()

要檢查哪個用戶已登錄, rest_framework.authtoken在數據庫中創建一個表,其中包含令牌、用戶和創建令牌的時間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM