Commit 1075b335 authored by gab's avatar gab

added google sign in

parent da4ed91f
{% extends "base.html" %}
{% block head %}
<script src="https://accounts.google.com/gsi/client" async></script>
{% endblock %}
{% block content %}
<form method="POST" autocomplete="off">
{% csrf_token %}
{{ loginform.as_p }}
<input type="submit" value="Log In"/>
<div id="g_id_onload"
data-client_id="1091426130026-resqtjvbfu1mguphrr5seg6d31k1ttqf.apps.googleusercontent.com"
data-context="signin"
data-ux_mode="redirect"
data-login_uri="http://localhost:8000/accounts/auth-receiver"
data-itp_support="true">
</div>
<div class="g_id_signin"
data-type="standard"
data-shape="pill"
data-theme="filled_black"
data-text="signin_with"
data-size="large"
data-logo_alignment="left">
</div>
</form>
{% endblock %}
\ No newline at end of file
......@@ -7,5 +7,6 @@ app_name = "accounts"
urlpatterns = [
path('login', views.login_page, name="login"),
path('register', views.register, name="register"),
path('logout', views.user_logout, name="logout")
path('logout', views.user_logout, name="logout"),
path('auth-receiver', views.auth_receiver, name="auth-receiver")
]
\ No newline at end of file
import os
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from google.oauth2 import id_token
from google.auth.transport import requests
from .forms import CustomUserCreationForm, CustomUserAuthenticationForm
from django.contrib.auth.models import auth
from django.contrib.auth import authenticate, login, logout
from . import models
def register(request):
# return HttpResponse("I am in Register")
form = CustomUserCreationForm()
print(request.POST)
if request.method == "POST":
form = CustomUserCreationForm(request.POST)
if form.is_valid():
......@@ -23,7 +32,7 @@ def register(request):
def login_page(request):
form = CustomUserAuthenticationForm()
print(request.POST)
if request.method == "POST":
form = CustomUserAuthenticationForm(request, data = request.POST)
if form.is_valid():
......@@ -43,3 +52,39 @@ def login_page(request):
def user_logout(request):
auth.logout(request)
return redirect("index")
@csrf_exempt
def auth_receiver(request):
"""
Google calls this URL after the user has signed in with their Google account.
"""
token = request.POST['credential']
try:
user_data = id_token.verify_oauth2_token(
token, requests.Request(), os.environ['GOOGLE_OAUTH_CLIENT_ID']
)
except ValueError:
return HttpResponse(status=403)
# In a real app, I'd also save any new user here to the database. See below for a real example I wrote for Photon Designer.
# You could also authenticate the user here using the details from Google (https://docs.djangoproject.com/en/4.2/topics/auth/default/#how-to-log-a-user-in)
email = user_data['email']
password = user_data['sub']+'google'+user_data['name']
logInUser = authenticate(request, email = email, password = password)
if logInUser is not None:
pass
else:
first_name = user_data['given_name']
last_name = user_data['family_name']
role = "STUDENT"
user = models.CustomUser.objects.create_user(email=email, password=password, first_name=first_name, last_name=last_name, role=role)
auth.login(request, logInUser)
print("Login Successful")
return redirect("index")
\ No newline at end of file
......@@ -2,6 +2,8 @@
<head>
<meta charset="utf-8">
<title>{% block title %}Red Brick Board{% endblock %}</title>
{% block head %}
{% endblock %}
</head>
<body>
<main>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment