Skip to content

Commit

Permalink
Merge pull request #281 from letehaha/fix/auth-checker
Browse files Browse the repository at this point in the history
Improve login page UX when caught invalid auth token
  • Loading branch information
letehaha authored Jan 5, 2024
2 parents 939089d + eb82191 commit 374b4fa
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 67 deletions.
66 changes: 33 additions & 33 deletions .github/workflows/check-source-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ jobs:
- name: Build source code
run: npm run build

integration-tests:
name: Integration testing
needs: prepare-dependencies
runs-on: ubuntu-latest
# integration-tests:
# name: Integration testing
# needs: prepare-dependencies
# runs-on: ubuntu-latest
# Disabled for now because when running in parallel, GitHub setups different
# machines with different chrome-browsers installed. That causes Cypress errors
# When trying to run in the cypress Docker container, there's no access to "sudo"
Expand All @@ -87,35 +87,35 @@ jobs:
# # see browsers tags here: https://hub.docker.com/r/cypress/browsers/tags
# image: cypress/browsers:node-20.9.0-chrome-118.0.5993.88-1-ff-118.0.2-edge-118.0.2088.46-1
# options: --user 1001
steps:
- name: Checkout repository and submodules
uses: actions/checkout@v2
with:
submodules: recursive
token: ${{ secrets.REPOSITORIES_ACCESS_TOKEN }}
- uses: ./.github/actions/prepare-local-env
- name: Run Cypress e2e tests
uses: cypress-io/github-action@v6
with:
start: npm run dev
wait-on: http://budget-tracker.com:8100
install: false
config-file: cypress.config.ts
browser: chrome
quiet: true
record: true
# parallel: true
group: Cypress tests
env:
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
# Recommended: pass the GitHub token lets this action correctly
# determine the unique run id necessary to re-run the checks
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_URL: http://budget-tracker.com:8100
PORT: 8100
HOST: budget-tracker.com
VITE_APP_API_HTTP: https://${{ secrets.API_HOST_PROD }}
VITE_APP_API_VER: ${{ secrets.VUE_APP_API_VER }}
# steps:
# - name: Checkout repository and submodules
# uses: actions/checkout@v2
# with:
# submodules: recursive
# token: ${{ secrets.REPOSITORIES_ACCESS_TOKEN }}
# - uses: ./.github/actions/prepare-local-env
# - name: Run Cypress e2e tests
# uses: cypress-io/github-action@v6
# with:
# start: npm run dev
# wait-on: http://budget-tracker.com:8100
# install: false
# config-file: cypress.config.ts
# browser: chrome
# quiet: true
# record: true
# # parallel: true
# group: Cypress tests
# env:
# CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
# # Recommended: pass the GitHub token lets this action correctly
# # determine the unique run id necessary to re-run the checks
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# BASE_URL: http://budget-tracker.com:8100
# PORT: 8100
# HOST: budget-tracker.com
# VITE_APP_API_HTTP: https://${{ secrets.API_HOST_PROD }}
# VITE_APP_API_VER: ${{ secrets.VUE_APP_API_VER }}

docker-build:
name: Build source code using Docker
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@vue/eslint-config-typescript": "^12.0.0",
"@vue/test-utils": "^2.4.2",
"css-loader": "^6.8.1",
"cypress": "^13.5.1",
"cypress": "^13.6.2",
"dotenv": "^16.3.1",
"eslint": "^8.54.0",
"eslint-friendly-formatter": "^4.0.1",
Expand Down
34 changes: 12 additions & 22 deletions src/api/_api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { API_ERROR_CODES, API_RESPONSE_STATUS } from 'shared-types';
import { useAuthStore } from '@/stores';
import { router } from '@/routes';
import { ApiBaseError } from '@/common/types';
import {
useNotificationCenter,
Expand Down Expand Up @@ -40,8 +42,6 @@ console.log('API_HTTP', API_HTTP);
// eslint-disable-next-line no-console
console.log('API_VER', API_VER);
class ApiCaller {
logout: () => void;

authToken: string | null;

_baseURL: string;
Expand Down Expand Up @@ -183,15 +183,15 @@ class ApiCaller {
});

throw new errors.NetworkError('Failed to fetch data from the server.');
} else {
addNotification({
id: 'unexpected-api-error',
text: 'Unexpected error.',
type: NotificationType.error,
});

throw new errors.UnexpectedError('Unexpected error.', {});
}

addNotification({
id: 'unexpected-api-error',
text: 'Unexpected error.',
type: NotificationType.error,
});

throw new errors.UnexpectedError('Unexpected error.', {});
}

const { status, response }: {
Expand All @@ -205,7 +205,8 @@ class ApiCaller {

if (status === API_RESPONSE_STATUS.error) {
if (response.code === API_ERROR_CODES.unauthorized) {
await this.logout();
useAuthStore().logout();
router.push('/');

addNotification({
id: 'authorization-error',
Expand Down Expand Up @@ -235,17 +236,6 @@ class ApiCaller {

return undefined;
}

setRequiredActions({ logout }) {
this.logout = logout;
}
}

export const api = new ApiCaller();

export function initApiCaller(
{ logout }:
{ logout: () => void },
): void {
api.setRequiredActions({ logout });
}
6 changes: 0 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { createApp } from 'vue';
import { VueQueryPlugin } from '@tanstack/vue-query';
import { router } from '@/routes';
import { clickOutside, nodeResizeObserver } from '@/directives';
import { initApiCaller } from '@/api';
import { store } from '@/stores/setup';
import { useAuthStore } from '@/stores';
import { identifyCurrentTheme } from '@/common/utils';
import App from './app.vue';
import '@/styles/index.scss';
Expand All @@ -23,7 +21,3 @@ app.use(store);
app.use(VueQueryPlugin);

app.mount('#app');

initApiCaller({
logout: useAuthStore().logout,
});
5 changes: 4 additions & 1 deletion src/stores/categories/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CategoryModel } from 'shared-types';
import { loadSystemCategories } from '@/api';
import { type FormattedCategory } from '@/common/types';
import { useNotificationCenter } from '@/components/notification-center';
import * as errors from '@/js/errors';
import { buildCategiesObjectGraph } from './helpers';

export const useCategoriesStore = defineStore('categories', () => {
Expand All @@ -17,7 +18,9 @@ export const useCategoriesStore = defineStore('categories', () => {

if (result?.length) categories.value = result;
} catch (err) {
notificationStore.addErrorNotification('Cannot load categories');
if (!(err instanceof errors.AuthError)) {
notificationStore.addErrorNotification('Cannot load categories');
}
}
};

Expand Down

0 comments on commit 374b4fa

Please sign in to comment.