Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
library-app-django
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ilham Maulana
library-app-django
Commits
75f7f42b
Commit
75f7f42b
authored
Jul 03, 2024
by
impfundev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: refresh jwt token 5 minutes before expired
parent
ce37dda1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
21 deletions
+25
-21
middleware.py
authentications/middleware.py
+24
-20
views.py
authentications/views.py
+1
-1
No files found.
authentications/middleware.py
View file @
75f7f42b
import
jwt
from
datetime
import
datetime
from
django.conf
import
settings
from
datetime
import
datetime
,
timedelta
from
django.utils.deprecation
import
MiddlewareMixin
from
django.shortcuts
import
get_object_or_404
from
django.http
import
HttpResponseRedirect
from
librarians.models
import
Librarians
from
django.conf
import
settings
import
jwt.utils
class
AuthMiddleware
(
MiddlewareMixin
):
...
...
@@ -17,29 +16,34 @@ class AuthMiddleware(MiddlewareMixin):
auth_session
=
request
.
session
.
get
(
"auth_session"
,
None
)
if
request
.
path
.
startswith
(
"/dashboard/"
)
:
if
auth_session
is
not
None
:
decode
d
=
jwt
.
decode
(
if
auth_session
is
not
None
:
try
:
payloa
d
=
jwt
.
decode
(
auth_session
,
settings
.
JWT_SECRET
,
algorithms
=
[
"HS256"
]
)
user_verified
=
get_object_or_404
(
Librarians
,
id
=
decoded
[
"librarian_id"
]
)
user_obj
=
{
"exp"
:
decoded
[
"exp"
],
"id"
:
user_verified
.
id
,
"name"
:
user_verified
.
name
,
"time"
:
str
(
datetime
.
now
()),
}
message
=
"login request success, user: "
+
f
"{user_obj}"
print
(
message
)
# refresh token 5 minutes before expired
expired_time
=
datetime
.
fromtimestamp
(
payload
[
"exp"
])
near_expired
=
expired_time
-
timedelta
(
minutes
=
5
)
if
datetime
.
now
()
>=
near_expired
:
payload
[
"exp"
]
=
(
payload
[
"exp"
]
+
timedelta
(
minutes
=
15
)
.
total_seconds
()
)
new_token
=
jwt
.
encode
(
payload
,
settings
.
JWT_SECRET
,
algorithm
=
"HS256"
)
request
.
session
[
"auth_session"
]
=
new_token
return
response
else
:
except
jwt
.
ExpiredSignatureError
:
del
request
.
session
[
"auth_session"
]
return
HttpResponseRedirect
(
"/auth/login"
)
if
auth_session
is
not
None
and
request
.
path
.
startswith
(
"/auth/"
):
if
auth_session
is
None
and
request
.
path
.
startswith
(
"/dashboard/"
):
return
HttpResponseRedirect
(
"/auth/login"
)
elif
auth_session
is
not
None
and
request
.
path
.
startswith
(
"/auth/"
):
return
HttpResponseRedirect
(
"/dashboard/"
)
else
:
return
response
authentications/views.py
View file @
75f7f42b
...
...
@@ -25,7 +25,7 @@ class AuthView(TemplateView):
password
=
form
.
data
[
"password"
],
)
expiration_time
=
datetime
.
now
()
+
timedelta
(
minutes
=
30
)
expiration_time
=
datetime
.
now
()
+
timedelta
(
minutes
=
15
)
payload
=
{
"exp"
:
expiration_time
.
timestamp
(),
"librarian_id"
:
librarian
.
id
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment