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
265eea60
Commit
265eea60
authored
Jul 01, 2024
by
impfundev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: utils
parent
370cbf78
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
31 additions
and
9 deletions
+31
-9
context_processors.py
authentications/context_processors.py
+2
-1
middleware.py
authentications/middleware.py
+4
-1
utils.py
authentications/utils.py
+7
-0
views.py
authentications/views.py
+13
-7
settings.py
config/settings.py
+5
-0
No files found.
authentications/context_processors.py
View file @
265eea60
import
jwt
from
django.shortcuts
import
get_object_or_404
from
librarians.models
import
Librarians
from
django.conf
import
settings
def
get_auth_session
(
request
):
auth_session
=
request
.
session
.
get
(
"auth_session"
,
None
)
if
auth_session
:
decoded
=
jwt
.
decode
(
auth_session
,
"secret"
,
algorithms
=
[
"HS256"
])
decoded
=
jwt
.
decode
(
auth_session
,
settings
.
JWT_SECRET
,
algorithms
=
[
"HS256"
])
user_id
=
decoded
[
"librarian_id"
]
user_verified
=
get_object_or_404
(
Librarians
,
id
=
user_id
)
...
...
authentications/middleware.py
View file @
265eea60
...
...
@@ -5,6 +5,7 @@ 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
class
AuthMiddleware
(
MiddlewareMixin
):
...
...
@@ -18,7 +19,9 @@ class AuthMiddleware(MiddlewareMixin):
if
request
.
path
.
startswith
(
"/dashboard/"
):
if
auth_session
is
not
None
:
decoded
=
jwt
.
decode
(
auth_session
,
"secret"
,
algorithms
=
[
"HS256"
])
decoded
=
jwt
.
decode
(
auth_session
,
settings
.
JWT_SECRET
,
algorithms
=
[
"HS256"
]
)
user_verified
=
get_object_or_404
(
Librarians
,
id
=
decoded
[
"librarian_id"
]
)
...
...
authentications/utils.py
0 → 100644
View file @
265eea60
import
jwt
from
django.conf
import
settings
def
create_auth_session
(
request
,
payload
):
token
=
jwt
.
encode
(
payload
,
settings
.
JWT_SECRET
,
algorithm
=
"HS256"
)
request
.
session
[
"auth_session"
]
=
token
authentications/views.py
View file @
265eea60
import
jwt
from
django.http
import
HttpResponseRedirect
from
django.shortcuts
import
render
from
authentications.forms
import
LoginForm
,
SignUpForm
from
librarians.models
import
Librarians
,
LoginHistory
from
authentications.utils
import
create_auth_session
def
login
(
request
):
...
...
@@ -20,14 +20,13 @@ def login(request):
email
=
form
.
data
[
"email"
],
password
=
form
.
data
[
"password"
],
)
payload
=
{
"librarian_id"
:
librarian
.
id
,
"name"
:
librarian
.
name
,
"email"
:
librarian
.
email
,
}
token
=
jwt
.
encode
(
payload
,
"secret"
,
algorithm
=
"HS256"
)
request
.
session
[
"auth_session"
]
=
token
create_auth_session
(
request
,
payload
)
LoginHistory
.
objects
.
create
(
librarian_id
=
librarian
.
id
)
return
HttpResponseRedirect
(
"/dashboard/"
)
...
...
@@ -59,13 +58,20 @@ def sign_up(request):
email
=
form
.
data
[
"email"
],
password
=
form
.
data
[
"password"
],
)
librarian_id
=
librarian
.
get
(
new_librarian
=
librarian
.
get
(
name
=
form
.
data
[
"name"
],
email
=
form
.
data
[
"email"
],
password
=
form
.
data
[
"password"
],
)
.
id
)
payload
=
{
"librarian_id"
:
new_librarian
.
id
,
"name"
:
new_librarian
.
name
,
"email"
:
new_librarian
.
email
,
}
create_auth_session
(
request
,
payload
)
LoginHistory
.
objects
.
create
(
librarian_id
=
librarian_
id
)
LoginHistory
.
objects
.
create
(
librarian_id
=
new_librarian
.
id
)
return
HttpResponseRedirect
(
"/dashboard/"
)
else
:
form
=
SignUpForm
()
...
...
config/settings.py
View file @
265eea60
...
...
@@ -11,10 +11,15 @@ https://docs.djangoproject.com/en/5.0/ref/settings/
"""
from
pathlib
import
Path
import
os
from
dotenv
import
load_dotenv
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR
=
Path
(
__file__
)
.
resolve
()
.
parent
.
parent
# .env
load_dotenv
()
JWT_SECRET
=
os
.
getenv
(
"JWT_SECRET"
,
default
=
""
)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
...
...
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