Skip to content

Commit

Permalink
add: auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Meefx committed Nov 27, 2023
1 parent d89d2e8 commit dcc380c
Show file tree
Hide file tree
Showing 145 changed files with 30,791 additions and 1 deletion.
Empty file added authentication/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions authentication/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions authentication/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AuthenticationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'authentication'
Empty file.
3 changes: 3 additions & 0 deletions authentication/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions authentication/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from authentication.views import *

app_name = 'authentication'

urlpatterns = [
path('login/', login, name='login'),
path('logout/', logout, name='logout'),
]
49 changes: 49 additions & 0 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from django.shortcuts import render
from django.contrib.auth import authenticate, login as auth_login
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import logout as auth_logout

@csrf_exempt
def login(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth_login(request, user)
# Status login sukses.
return JsonResponse({
"username": user.username,
"status": True,
"message": "Login sukses!"
# Tambahkan data lainnya jika ingin mengirim data ke Flutter.
}, status=200)
else:
return JsonResponse({
"status": False,
"message": "Login gagal, akun dinonaktifkan."
}, status=401)

else:
return JsonResponse({
"status": False,
"message": "Login gagal, periksa kembali email atau kata sandi."
}, status=401)

@csrf_exempt
def logout(request):
username = request.user.username

try:
auth_logout(request)
return JsonResponse({
"username": username,
"status": True,
"message": "Logout berhasil!"
}, status=200)
except:
return JsonResponse({
"status": False,
"message": "Logout gagal."
}, status=401)
10 changes: 10 additions & 0 deletions readnow/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
DEBUG = True

ALLOWED_HOSTS = ["*"]
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = 'None'
SESSION_COOKIE_SAMESITE = 'None'
CSRF_TRUSTED_ORIGINS = ['https://readnow-c14-tk.pbp.cs.ui.ac.id']


# Application definition
Expand All @@ -44,6 +51,8 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'authentication',
'corsheaders',
'book', # Aplikasi untuk menyimpan data buku
'main', # Rachel Heningtyas Zanetta Erari (2206081944)
'pinjam_buku', # Harjuno Abdullah (2206814053)
Expand All @@ -61,6 +70,7 @@
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'corsheaders.middleware.CorsMiddleware',

]

Expand Down
1 change: 1 addition & 0 deletions readnow/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
path('wishlist/', include('wishlist.urls')),
path('review/', include('review_buku.urls')),
path('pinjam/', include('pinjam_buku.urls')),
path('auth/', include('authentication.urls')),
]
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ psycopg2-binary
requests
urllib3
django-environ
whitenoise
whitenoise
django-cors-headers
Binary file added staticfiles/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit dcc380c

Please sign in to comment.