Skip to content

Commit

Permalink
(feat): Implement signin, signout, logout, login and oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
LopesLs committed Jan 2, 2024
1 parent b0c428d commit fab2e7e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
27 changes: 27 additions & 0 deletions scheduleManagement/templates/account/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign In</title>
</head>
<body>
<title>Sign In</title>

<h1>Sign in</h1>

<p>Still don't have an account, then please <a href="{{ signup_url }}">sign up</a></p>

{% url 'account_login' as login_url %}

<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>

{% load socialaccount %}

<a href="{% provider_login_url 'google'%}">Login with google</a>
</body>
</html>
17 changes: 17 additions & 0 deletions scheduleManagement/templates/account/logout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Logout</title>
</head>
<body>
<h1>Logout</h1>
<p>Are you sure that you want to logout of <strong>{{ user.username }}</strong></p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button>Continue</button>
</form>
</body>
</html>
14 changes: 7 additions & 7 deletions scheduleManagement/templates/home.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block title %} <title>Home page</title> {% endblock %}
</head>
<body>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block title %} <title>Home page</title> {% endblock %}
</head>
<body>
{% block navbar %}
<nav>
<ul>
<li><a href="">Schools Schedules</a></li>
<li><a href="">{{ user.username }}</a></li>
<li><a href="/logout">Logout</a></li>
<li><a href="/accounts/logout">Logout</a></li>
</ul>
</nav>
{% endblock %}
Expand Down
17 changes: 17 additions & 0 deletions scheduleManagement/templates/socialaccount/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign in via google</title>
</head>
<body>
<h1>Sign via google</h1>
<p>You area about to sign with a third-party environment</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button>Continue</button>
</form>
</body>
</html>
25 changes: 16 additions & 9 deletions schoolschedules/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
https://docs.djangoproject.com/en/4.2/ref/settings/
"""

import os
from pathlib import Path
from django.urls import reverse_lazy
from dotenv import load_dotenv
from pathlib import Path

from django.urls import reverse_lazy
import os

load_dotenv() # load environment variables from .env file

Expand All @@ -36,6 +36,8 @@

SITE_ID = 2

# Social Account Configuration

SOCIAL_ACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
Expand All @@ -57,8 +59,8 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'scheduleManagement',
'django.contrib.sites',
'scheduleManagement',
'allauth',
'allauth.account',
'allauth.socialaccount',
Expand All @@ -73,7 +75,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'allauth.account.middleware.AccountMiddleware', # enable allauth
'allauth.account.middleware.AccountMiddleware',
]

ROOT_URLCONF = 'schoolschedules.urls'
Expand Down Expand Up @@ -145,16 +147,21 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# Authentication backends
# Authentication Configuration

ACCOUNT_EMAIL_REQUIRED = True

ACCOUNT_AUTHENTICATION_METHOD = 'username_email'

AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)

# Redirect URLs constants

LOGIN_URL = reverse_lazy('login') # redirect to login page if user is not logged in
LOGIN_REDIRECT_URL = reverse_lazy('home') # redirect to home page after login
LOGOUT_REDIRECT_URL = reverse_lazy('login') # redirect to login page after logout
LOGIN_REDIRECT_URL = "/" # redirect to home page after login
LOGOUT_REDIRECT_URL = "/" # redirect to login page after logout

0 comments on commit fab2e7e

Please sign in to comment.