Skip to content

Commit

Permalink
First pass at Google login - functional tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Mann committed Mar 9, 2022
1 parent b5fc279 commit 147f677
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 11 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# group-project-a-07
# Word Of Mouth


## How to start developing:
1. Create a [GCP App](https://console.cloud.google.com/home/)
2. Go to [API Overview](https://console.cloud.google.com/apis/)
3. Go to "Credentials" on the menu to the left
4. Hit Create Credentials at the top of the page
1. For the "Approved JavaScript Origins", set it to be `http://localhost:8000`. (TODO: We need to add our heroku deployments to this list)
2. For the "Authorized Redirect URLs", set it to be `http://localhost:8000/accounts/google/login/callback/`. (TODO: See above)
3. (side note: if you are running into errors, keep in mind that `localhost` and `127.0.0.1` are two different domain names, even though they resolve to the same place)
5. Run migrations: `python manage.py migrate`
6. Create superuser: `python manage.py createsuperuser`
7. Go to `localhost:8000/admin`, go to "Social Applications", and add the application with the "Client ID" and "Secret Key" that we got from our GCP console.
1 change: 1 addition & 0 deletions main/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import models

# Create your models here.
from django.db.models import Model
25 changes: 25 additions & 0 deletions main/templates/main/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% load account %}
{% load i18n %}
<!DOCTYPE html>
<html>
<head>
<title>Google Registration</title>
</head>
<body>
{% load socialaccount %}
<h1>My Google Login Project</h1>
<a href="{% provider_login_url 'google' %}?next=/">Login with Google</a>

{% if user.is_authenticated %}
<p>
{% user_display user as user_display %}
{% blocktrans %}{{ user_display }} has logged in...{% endblocktrans %}
</p>
{% else %}
<p>User is not logged in</p>

{% endif %}


</body>
</html>
2 changes: 1 addition & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
from . import views

urlpatterns = [
path('', views.index, name='index'),
path('', views.Index.as_view(), name='index'),
]
8 changes: 6 additions & 2 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

# Create your views here.
from django.http import HttpResponse
from django.views.generic import TemplateView


def index(request):
return HttpResponse("Hello, world. This is a basic working Django app deployed to Heroku.")
class Index(TemplateView):
template_name = 'main/index.html'

def get_context_data(self,*args, **kwargs):
return {"user": self.request.user}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
django-allauth

Django

Expand Down
37 changes: 30 additions & 7 deletions word_of_mouth/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

SITE_ID = 1
LOGIN_REDIRECT_URL = "/"


from pathlib import Path

import django_heroku

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

Expand All @@ -28,7 +32,6 @@

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
Expand All @@ -39,8 +42,26 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"django.contrib.sites", # <--
"allauth", # <--
"allauth.account", # <--
"allauth.socialaccount", # <--
"allauth.socialaccount.providers.google",
"main",
]

SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
'profile',
'email',
],
'AUTH_PARAMS': {
'access_type': 'online',
}
}
}

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down Expand Up @@ -71,7 +92,6 @@

WSGI_APPLICATION = 'word_of_mouth.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

Expand All @@ -82,7 +102,6 @@
}
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

Expand All @@ -101,7 +120,6 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

Expand All @@ -113,7 +131,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

Expand All @@ -124,4 +141,10 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

django_heroku.settings(locals())
django_heroku.settings(locals())

#add this in the end of file
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
1 change: 1 addition & 0 deletions word_of_mouth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

urlpatterns = [
path('admin/', admin.site.urls),
path("accounts/", include("allauth.urls")), # most important
path('', include('main.urls')),
]

0 comments on commit 147f677

Please sign in to comment.