Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip OAuth and use basic auth #1011

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions frontend-project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Zawiera pliki odpowiedzialne za frontend aplikacji small_eod. Został on napisan
yarn start:public-api
```

Aby ominąć logowanie za pomocą OAuth możesz uruchomić aplikację dodając zmienne środowiskowe USER oraz PASSWORD. Przy takim ustawieniu aplikacja wykorzysta logowanie z wykorzystaniem basic auth.

```
USER=root PASSWORD=root yarn start:public-api
```

6. W celu uruchomienia testów wykonaj polecenie:

```shell
Expand Down
13 changes: 11 additions & 2 deletions frontend-project/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import slash from 'slash2';
import defaultSettings from './defaultSettings';
const { pwa } = defaultSettings;

const backend_url = process.env.API_URL || 'http://backend:8000/';
const backendUrl = process.env.API_URL || 'http://backend:8000/';
const basicAuth =
process.env.USER && process.env.PASSWORD
? `${process.env.USER}:${process.env.PASSWORD}`
: undefined;

const plugins = [
['umi-plugin-antd-icon-config', {}],
Expand Down Expand Up @@ -481,9 +485,14 @@ export default {
['api', 'admin', 'static', 'media'].map(x => [
`/${x}/`,
{
target: backend_url,
target: backendUrl,
changeOrigin: true,
auth: basicAuth,
},
]),
),
define: {
USER: process.env.USER,
PASSWORD: process.env.PASSWORD,
},
};
2 changes: 1 addition & 1 deletion frontend-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"cross-port-killer": "1.3.0",
"enzyme": "3.11.0",
"express": "4.17.1",
"husky": "^6.0.0",
"husky": "^7.0.1",
"import-sort-cli": "6.0.0",
"import-sort-parser-babylon": "6.0.0",
"import-sort-parser-typescript": "6.0.0",
Expand Down
12 changes: 10 additions & 2 deletions frontend-project/src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { TokenResponse } from '@/services/definitions';
import { UsersService } from '@/services/users';
import SmallEodClient from '@/utils/sdk';

const isBasicAuth = Boolean(
// @ts-ignore
typeof USER !== 'undefined' && typeof PASSWORD !== 'undefined' && USER && PASSWORD,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raczej nie ma scenariusza, w którym check USER && PASSWORD nie załatwi jednocześnie typeof /*jedno i drugie */ !== 'undefined'.

W ogóle nie brakuje tu, bo ja wiem, jakiejś deklaracji/importu?

);

export function useAuth() {
const ACCESS_TOKEN = 'accessToken';
const REFRESH_TOKEN = 'refreshToken';
Expand All @@ -21,7 +26,7 @@ export function useAuth() {
}

function refreshToken(): void {
if (isLoggedIn())
if (isLoggedIn() && !isBasicAuth)
UsersService.refresh({ refreshToken: localStorage.getItem(REFRESH_TOKEN) })
.then(setTokens)
.catch(logout);
Expand All @@ -33,7 +38,10 @@ export function useAuth() {
}

function isLoggedIn(): boolean {
return Boolean(localStorage.getItem(ACCESS_TOKEN) && localStorage.getItem(REFRESH_TOKEN));
return (
Boolean(localStorage.getItem(ACCESS_TOKEN) && localStorage.getItem(REFRESH_TOKEN)) ||
isBasicAuth
);
}

function logout(): void {
Expand Down
Loading