diff --git a/README.md b/README.md index 21a55487a1..70c9c6ebcd 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,46 @@ Django poll app is a full featured polling app. You have to register in this app seeder.seed_all(30)

Here 30 is a number of entry. You can use it as your own

+

Obtaining OAuth Client ID for Google:

+ +To use Google's OAuth authentication in your application, you need to obtain a client ID and client secret. Follow these steps to get your OAuth client ID for Google: + +1. **Go to the Google Cloud Console:** + - Navigate to [Google Cloud Console](https://console.cloud.google.com/). + - Sign in with your Google account. + +2. **Create a new project:** + - Click on the project dropdown menu at the top of the page. + - Click on "New Project" and follow the prompts to create a new project. + +3. **Enable the Google Identity service:** + - In the Google Cloud Console, navigate to "APIs & Services" > "Dashboard." + - Click on "Enable APIs and Services." + - Search for "Google Identity" or "Google+ API" and enable it for your project. + +4. **Create OAuth consent screen:** + - In the Google Cloud Console, navigate to "APIs & Services" > "OAuth consent screen." + - Fill in the required fields (like application name, user support email, etc.). + - Add scopes (permissions) your application requires. + - Save the consent screen information. + +5. **Create OAuth credentials:** + - In the Google Cloud Console, navigate to "APIs & Services" > "Credentials." + - Click on "Create Credentials" > "OAuth client ID." + - Select "Web application" as the application type. + - Enter a name for your OAuth client. + - Add authorized redirect URIs : `http://127.0.0.1:8000/complete/google-oauth2/` + - Click "Create." + +6. **Copy the client ID and client secret:** + - Once the OAuth client is created, you'll see your client ID and client secret. + - Copy these values and update following variables in settings.py + + SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-client-id' + SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET ='your-client-secret' + +For detailed instructions, refer to Google's documentation on [OAuth 2.0](https://developers.google.com/identity/protocols/oauth2). +

To run the program in local server use the following command

python manage.py runserver diff --git a/accounts/templates/accounts/login.html b/accounts/templates/accounts/login.html index c571bcb094..0e6c8c18b2 100644 --- a/accounts/templates/accounts/login.html +++ b/accounts/templates/accounts/login.html @@ -3,6 +3,9 @@ {% block content %}
+ + Login with Google +

Login

{% if messages %}
@@ -38,4 +41,4 @@

Login

-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/pollme/settings.py b/pollme/settings.py index e18c4f6345..1985f7cca6 100644 --- a/pollme/settings.py +++ b/pollme/settings.py @@ -31,6 +31,7 @@ # Application definition INSTALLED_APPS = [ + 'social_django', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -41,6 +42,11 @@ 'accounts.apps.AccountsConfig', ] +AUTHENTICATION_BACKENDS = ( + 'social_core.backends.google.GoogleOAuth2', + 'django.contrib.auth.backends.ModelBackend', +) + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', @@ -64,6 +70,8 @@ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'social_django.context_processors.backends', + 'social_django.context_processors.login_redirect', ], }, }, @@ -123,3 +131,8 @@ STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ] + +SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your-client-id' +SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET ='your-client-secret' +SOCIAL_AUTH_URL_NAMESPACE = 'social' +SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/polls/list/user/' diff --git a/pollme/urls.py b/pollme/urls.py index ff6144e901..3d6f1a830f 100644 --- a/pollme/urls.py +++ b/pollme/urls.py @@ -22,4 +22,5 @@ path('admin/', admin.site.urls), path('accounts/', include('accounts.urls', namespace="accounts")), path('polls/', include('polls.urls', namespace="polls")), + path('', include('social_django.urls', namespace='social')), ] diff --git a/requirements.txt b/requirements.txt index 73de26ecd9..d608e5d0f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ asgiref==3.3.1 Django==3.1.14 pytz==2020.5 sqlparse==0.4.4 +social-auth-app-django>=0.4.0