Skip to content

Commit

Permalink
Switched to OAuth and added RN env file
Browse files Browse the repository at this point in the history
  • Loading branch information
ineshbose committed Dec 30, 2021
1 parent 50ad44b commit 4f74f36
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 35 deletions.
343 changes: 341 additions & 2 deletions src/backend/poetry.lock

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion src/backend/portion_mate/settings.py
Expand Up @@ -45,6 +45,7 @@
"django_extensions.apps.DjangoExtensionsConfig",
"rest_framework.apps.RestFrameworkConfig",
"corsheaders.apps.CorsHeadersAppConfig",
"oauth2_provider.apps.DOTConfig",
]

AUTH_USER_MODEL = "main.User"
Expand Down Expand Up @@ -149,6 +150,16 @@
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 10,
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework_simplejwt.authentication.JWTAuthentication",
"oauth2_provider.contrib.rest_framework.OAuth2Authentication",
),
}

OAUTH2_PROVIDER = {
# this is the list of available scopes
"SCOPES": {
"read": "Read scope",
"write": "Write scope",
"groups": "Access to your groups",
},
"OAUTH2_BACKEND_CLASS": "oauth2_provider.oauth2_backends.JSONOAuthLibCore",
}
2 changes: 2 additions & 0 deletions src/backend/pyproject.toml
Expand Up @@ -12,6 +12,8 @@ django-environ = "^0.8.1"
django-extensions = "^3.1.5"
djangorestframework = "^3.13.1"
Pillow = "^8.4.0"
django-oauth-toolkit = "^1.6.1"
django-cors-headers = "^3.10.1"

[tool.poetry.dev-dependencies]
black = "^21.10b0"
Expand Down
21 changes: 0 additions & 21 deletions src/backend/requirements.txt

This file was deleted.

4 changes: 1 addition & 3 deletions src/backend/rest/urls.py
@@ -1,6 +1,5 @@
from django.urls import include, path
from rest_framework import routers
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
from . import views


Expand All @@ -13,6 +12,5 @@
urlpatterns = [
path("", include(router.urls)),
path("auth/", include("rest_framework.urls", namespace="rest_framework")),
path("token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
path("token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
path("auth/o/", include("oauth2_provider.urls", namespace="oauth2_provider")),
]
5 changes: 5 additions & 0 deletions src/frontend/.env.example
@@ -0,0 +1,5 @@
# Generated from oauth2_provider from backend
CLIENT_ID=oauth_app_client_id

# Generated from oauth2_provider from backend
CLIENT_SECRET=oauth_app_client_secret
14 changes: 9 additions & 5 deletions src/frontend/api/auth.ts
@@ -1,14 +1,18 @@
import axios from 'axios';
import { storeObject } from './store';
import { JWTData } from './types';
import { AuthResponse } from './types';
import { CLIENT_ID, CLIENT_SECRET } from 'react-native-dotenv';

export const getToken = async (email: string, password: string) => {
export const getToken = async (username: string, password: string) => {
try {
const response = await axios.post<JWTData>(
'http://127.0.0.1:8000/api/token/',
const response = await axios.post<AuthResponse>(
'http://127.0.0.1:8000/api/auth/o/token/',
{
email,
username,
password,
grant_type: 'password',
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
}
);
await storeObject('auth_token', response.data);
Expand Down
9 changes: 6 additions & 3 deletions src/frontend/api/types.ts
@@ -1,6 +1,9 @@
export type JWTData = {
access: string;
refresh?: string;
export type AuthResponse = {
access_token: string;
expires_in: number;
refresh_token: string;
scope: string;
token_type: string;
};

export type PaginationData<T> = {
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/babel.config.js
@@ -1,3 +1,6 @@
module.exports = {
presets: ['babel-preset-expo'],
plugins: [
['module:react-native-dotenv', { moduleName: 'react-native-dotenv' }],
],
};
2 changes: 2 additions & 0 deletions src/frontend/package.json
Expand Up @@ -29,6 +29,7 @@
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.2",
"react-native-dotenv": "^3.3.1",
"react-native-elements": "^3.4.2",
"react-native-safe-area-context": "3.3.2",
"react-native-screens": "~3.8.0",
Expand All @@ -38,6 +39,7 @@
"@babel/core": "^7.12.9",
"@types/react": "~17.0.21",
"@types/react-native": "~0.64.12",
"@types/react-native-dotenv": "^0.2.0",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"eslint": "^8.3.0",
Expand Down
17 changes: 17 additions & 0 deletions src/frontend/yarn.lock
Expand Up @@ -1860,6 +1860,11 @@
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.4.tgz#fcf7205c25dff795ee79af1e30da2c9790808f11"
integrity sha512-rZ5drC/jWjrArrS8BR6SIr4cWpW09RNTYt9AMZo3Jwwif+iacXAqgVjm0B0Bv/S1jhDXKHqRVNCbACkJ89RAnQ==

"@types/react-native-dotenv@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@types/react-native-dotenv/-/react-native-dotenv-0.2.0.tgz#32c58422a422c1adf68acce363ed791314d5a8e7"
integrity sha512-ZxX+dU/yoQc0jTk+/NWttkiuXceJyN5FpOSqDl0WynN5GDzxwH7OMruQ47qcY8llo2RD3irjvzJ9BwC8gDiq0A==

"@types/react-native-vector-icons@^6.4.6":
version "6.4.10"
resolved "https://registry.yarnpkg.com/@types/react-native-vector-icons/-/react-native-vector-icons-6.4.10.tgz#9bfd6e64dd37b8119425496b5e53ff91d034efa9"
Expand Down Expand Up @@ -3217,6 +3222,11 @@ domexception@^2.0.1:
dependencies:
webidl-conversions "^5.0.0"

dotenv@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"
integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==

ee-first@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down Expand Up @@ -7114,6 +7124,13 @@ react-native-codegen@^0.0.6:
jscodeshift "^0.11.0"
nullthrows "^1.1.1"

react-native-dotenv@^3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/react-native-dotenv/-/react-native-dotenv-3.3.1.tgz#8f399cf28ca77d860d8e7f7323e439fa60a8ca0b"
integrity sha512-gAKXout1XCwCqJ3QPGoQAF2eRzOHgOnwg3x19z+ssow8bDIksJeKBqvoHDyGziVilAIP1J0bEC9Jf+VF8nFang==
dependencies:
dotenv "^10.0.0"

react-native-elements@^3.4.2:
version "3.4.2"
resolved "https://registry.yarnpkg.com/react-native-elements/-/react-native-elements-3.4.2.tgz#66602be9c5e0e0a2a831913adec80ff6518d1ee2"
Expand Down

0 comments on commit 4f74f36

Please sign in to comment.