Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.

Commit 1a5d6f5

Browse files
authored
Merge pull request #3 from BUCOMPAdvancedDevelopment/firebaseAuth
Firebase auth
2 parents 161700c + 169437b commit 1a5d6f5

File tree

11 files changed

+64
-3
lines changed

11 files changed

+64
-3
lines changed

.gcloudignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
# Python pycache:
1717
__pycache__/
1818
# Ignored by the build system
19-
/setup.cfg
19+
/setup.cfg
20+
# Secrets
21+
/secrets

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ media
3131
.idea/**/dynamic.xml
3232
.idea/**/uiDesigner.xml
3333
.idea/**/dbnavigator.xml
34-
secrets/*
34+
secrets/
3535

3636
# Gradle
3737
.idea/**/gradle.xml

AdvancedDevelopment/settings.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
For the full list of settings and their values, see
1010
https://docs.djangoproject.com/en/3.2/ref/settings/
1111
"""
12-
12+
import os
1313
from pathlib import Path
14+
import firebase_admin
15+
from firebase_admin import credentials
1416

1517
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1618
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -49,6 +51,12 @@
4951
'django.middleware.clickjacking.XFrameOptionsMiddleware',
5052
]
5153

54+
REST_FRAMEWORK = {
55+
'DEFAULT_AUTHENTICATION_CLASSES': [
56+
'FirebaseAuth.authentication.FirebaseAuthentication',
57+
]
58+
}
59+
5260
ROOT_URLCONF = 'AdvancedDevelopment.urls'
5361

5462
TEMPLATES = [
@@ -124,3 +132,6 @@
124132
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
125133

126134
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
135+
136+
cred = credentials.Certificate(os.path.join(BASE_DIR, "secrets/advanced-development-333919-25398fb03356.json"))
137+
firebase_admin.initialize_app(cred)

FirebaseAuth/__init__.py

Whitespace-only changes.

FirebaseAuth/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

FirebaseAuth/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class FirebaseauthConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'FirebaseAuth'

FirebaseAuth/authentication.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from django.contrib.auth.models import User
2+
from django.core.exceptions import ObjectDoesNotExist
3+
from rest_framework import authentication
4+
from rest_framework import exceptions
5+
import firebase_admin as admin
6+
import firebase_admin.auth as auth
7+
8+
9+
class FirebaseAuthentication(authentication.BaseAuthentication):
10+
"""
11+
https://jrizmal.medium.com/how-to-authenticate-firebase-users-in-django-rest-framework-c2d90f5a0a11
12+
"""
13+
def authenticate(self, request):
14+
15+
token = request.headers.get('Authorization')
16+
if not token:
17+
return None
18+
19+
try:
20+
decoded_token = auth.verify_id_token(token)
21+
uid = decoded_token["uid"]
22+
except:
23+
return None
24+
25+
try:
26+
user = User.objects.get(username=uid)
27+
return user, None
28+
29+
except ObjectDoesNotExist:
30+
return None

FirebaseAuth/migrations/__init__.py

Whitespace-only changes.

FirebaseAuth/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

FirebaseAuth/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

FirebaseAuth/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.

0 commit comments

Comments
 (0)