Skip to content

Instantly share code, notes, and snippets.

@ManotLuijiu
Last active December 12, 2023 08:27
Show Gist options
  • Save ManotLuijiu/30ae0aa06f7b294e14dfc2f60c63d727 to your computer and use it in GitHub Desktop.
Save ManotLuijiu/30ae0aa06f7b294e14dfc2f60c63d727 to your computer and use it in GitHub Desktop.
django_auth/settings.py
DOMAIN="localhost:3000
DJANGO_SECRET_KEY="my-django-secret-key"
CLOUDINARY_CLOUD_NAME=""
CLOUDINARY_API_KEY=""
CLOUDINARY_API_SECRET=""
REDIRECT_URLS="http://localhost:3000/auth/google,http://localhost:3000/auth/facebook"
AUTH_COOKIE_SECURE=False
GOOGLE_AUTH_KEY=""
GOOGLE_AUTH_SECRET_KEY=""
FACEBOOK_AUTH_KEY=""
FACEBOOK_AUTH_SECRET_KEY=""
AWS_SES_ACCESS_KEY_ID=""
AWS_SES_SECRET_ACCESS_KEY=""
# ใช้ gmail ก็ได้ครับ
AWS_SES_FROM_EMAIL="youremail@gmail.com"
from os import getenv, path
from pathlib import Path
from django.core.management.utils import get_random_secret_key
import dotenv
BASE_DIR = Path(__file__).resolve().parent.parent
print(BASE_DIR)
dotenv_file = BASE_DIR / ".env.local"
if path.isfile(dotenv_file):
dotenv.load_dotenv(dotenv_file)
SECRET_KEY = getenv("DJANGO_SECRET_KEY", get_random_secret_key())
# DEBUG = getenv("DEBUG", "False") == "True"
DEBUG = False
print(DEBUG)
ALLOWED_HOSTS = ["*"]
CSRF_TRUSTED_ORIGINS = ["https://*.<your-domain>.com"] # ตรงนี้ต้องแก้
CSRF_COOKIE_DOMAIN = ".<your-domain>.com" # ตรงนี้ต้องแก้
CORS_ALLOWED_ORIGINS = [
"https://<your-domain>.com", # ตรงนี้ต้องแก้
"https://www.<your-domain>.com", # ตรงนี้ต้องแก้
"https://<sub-domain>.<your-domain>.com", # เช่น event.your-domain.com
"http://localhost:3000",
"http://127.0.0.1:3000",
]
CORS_ORIGIN_WHITELIST = [
"https://<your-domain>.com", # ตรงนี้ต้องแก้
"https://www.<your-domain>.com", # ตรงนี้ต้องแก้
"https://<sub-domain>.<your-domain>.com", # เช่น event.your-domain.com
"http://localhost:3000",
"http://127.0.0.1:3000",
]
CORS_ALLOW_CREDENTIALS: True
INSTALLED_APPS = [
"django.contrib.admin",
"django_ses",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"cloudinary_storage",
"django.contrib.staticfiles",
"corsheaders",
"rest_framework",
"storages",
"cloudinary",
"djoser",
"social_django",
"users",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "django_auth.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = "django_auth.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
# DATABASES = {
# "default": {
# "ENGINE": "django.db.backends.sqlite3",
# "NAME": BASE_DIR / "db.sqlite3",
# }
# }
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres", # ตรงนี้ต้องแก้
"USER": "postgres", # ตรงนี้ต้องแก้
"PASSWORD": "your-password", # ตรงนี้ต้องแก้
"HOST": "db-django-auth.xxxxxxxxx.ap-southeast-1.rds.amazonaws.com", # ตรงนี้ต้องแก้
"PORT": "5432",
}
}
# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/
# LANGUAGE_CODE = "en-us"
# TIME_ZONE = "UTC"
LANGUAGE_CODE = "TH"
TIME_ZONE = "Asia/Bangkok"
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
CLOUDINARY_STORAGE = {
"CLOUD_NAME": getenv("CLOUDINARY_CLOUD_NAME"),
"API_KEY": getenv("CLOUDINARY_API_KEY"),
"API_SECRET": getenv("CLOUDINARY_API_SECRET")
}
STORAGES = {
"default": {"BACKEND": "cloudinary_storage.storage.MediaCloudinaryStorage"},
"staticfiles": {
"BACKEND": "cloudinary_storage.storage.StaticHashedCloudinaryStorage"
},
}
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
# Social login
AUTHENTICATION_BACKENDS = [
"social_core.backends.google.GoogleOAuth2",
"social_core.backends.facebook.FacebookOAuth2",
"django.contrib.auth.backends.ModelBackend",
]
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": ["users.authentication.CustomJWTAuthentication"],
"DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
}
DJOSER = {
"PASSWORD_RESET_CONFIRM_URL": "password-reset/{uid}/{token}",
"SEND_ACTIVATION_EMAIL": True,
"ACTIVATION_URL": "activation/{uid}/{token}",
"USER_CREATE_PASSWORD_RETYPE": True,
"PASSWORD_RESET_CONFIRM_RETYPE": True,
"TOKEN_MODEL": None,
"SOCIAL_AUTH_ALLOWED_REDIRECT_URIS": getenv("REDIRECT_URLS").split(","),
}
AUTH_COOKIE = "access"
AUTH_COOKIE_ACCESS_MAX_AGE = 60 * 5
AUTH_COOKIE_REFRESH_MAX_AGE = 60 * 60 * 24
AUTH_COOKIE_SECURE = getenv("AUTH_COOKIE_SECURE", "True") == "True"
AUTH_COOKIE_HTTP_ONLY = True
AUTH_COOKIE_PATH = "/"
AUTH_COOKIE_SAMESITE = "None"
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = getenv("GOOGLE_AUTH_KEY")
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = getenv("GOOGLE_AUTH_SECRET_KEY")
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
"openid",
]
SOCIAL_AUTH_GOOGLE_OAUTH2_EXTRA_DATA = ["first_name", "last_name"]
SOCIAL_AUTH_FACEBOOK_KEY = getenv("FACEBOOK_AUTH_KEY")
SOCIAL_AUTH_FACEBOOK_SECRET = getenv("FACEBOOK_AUTH_SECRET_KEY")
SOCIAL_AUTH_FACEBOOK_SCOPE = ["email"]
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {"fields": "email, first_name, last_name"}
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
AWS_SES_ACCESS_KEY_ID = getenv("AWS_SES_ACCESS_KEY_ID")
AWS_SES_SECRET_ACCESS_KEY = getenv("AWS_SES_SECRET_ACCESS_KEY")
EMAIL_BACKEND = "django_ses.SESBackend"
AWS_SES_REGION_NAME = "ap-southeast-1"
AWS_SES_REGION_ENDPOINT = "email.ap-southeast-1.amazonaws.com"
USE_SES_V2 = True
DOMAIN = getenv("DOMAIN")
SITE_NAME = "Django Auth App"
AUTH_USER_MODEL = "users.UserAccount"
# Settings for DJOSER and Django_SES
AWS_SES_FROM_EMAIL = getenv("AWS_SES_FROM_EMAIL")
DEFAULT_FROM_EMAIL = getenv("AWS_SES_FROM_EMAIL")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment