Setup Google Oauth

This commit is contained in:
sosokker 2023-10-27 01:50:19 +07:00
parent 90a69ae0a9
commit e169be9340
11 changed files with 76 additions and 1 deletions

View File

@ -30,6 +30,8 @@ ALLOWED_HOSTS = []
# Application definition
SITE_ID = 2
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
@ -37,8 +39,24 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
]
SOCIALACCOUNT_PROVIDERS = {
'google' : {
'SCOPE' : [
'profile',
'email',
],
'AUTH_PARAMS' : {'access_type' : 'online'}
}
}
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
@ -47,6 +65,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"allauth.account.middleware.AccountMiddleware",
]
ROOT_URLCONF = 'core.urls'
@ -121,3 +140,11 @@ STATIC_URL = 'static/'
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

View File

@ -15,8 +15,10 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
path('', include('users.urls')),
]

View File

3
backend/users/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
backend/users/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class UsersConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'users'

View File

3
backend/users/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charsets="UTF-8"/>
<meta http-equiv="X-UA-compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Django Google Sign In</title>
</head>
<body>
{% load socialaccount %}
<h2>Google Login</h2>
<a href='{% provider_login_url 'google' %}?next=/'>Login With Google</a>
</body>
</html>

3
backend/users/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

7
backend/users/urls.py Normal file
View File

@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
path('logout', views.logout_view),
]

10
backend/users/views.py Normal file
View File

@ -0,0 +1,10 @@
from django.shortcuts import render, redirect
from django.contrib.auth import logout
def home(request):
return render(request, 'users/home.html')
def logout_view(request):
logout(request)
return redirect('/')