Skip to content

Commit

Permalink
fix: setup correct URL for prod/dev proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
DeanMW committed Aug 3, 2021
1 parent 4e9c16a commit 28b8bde
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
7 changes: 7 additions & 0 deletions helpers/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {NuxtRuntimeConfig} from '@nuxt/types/config/runtime';

export default function ($config: NuxtRuntimeConfig) {
return $config.isDevelopment
? '/api/v1'
: 'https://bridge.hosted-tools.com/api/v1';
}
11 changes: 7 additions & 4 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ export default defineNuxtConfig({
proxy: true,
},

proxy: [
'https://bridge.hosted-tools.com/api/v1',
'https://auth.hosted-tools.com/api/get-token',
],
proxy:
process.env.NODE_ENV === 'development'
? {
'/api/v1': 'https://bridge.hosted-tools.com',
'/api/get-token': 'https://auth.hosted-tools.com',
}
: [],

// Build Configuration (https://go.nuxtjs.dev/config-build)
build: {},
Expand Down
6 changes: 3 additions & 3 deletions plugins/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import TimesheetsService from '~/services/timesheets-service';
import MailService from '~/services/mail-service';
import AuthService from '~/services/auth-service';

export default defineNuxtPlugin(({$fire, $axios}, inject) => {
export default defineNuxtPlugin(({$fire, $axios, $config}, inject) => {
inject('customersService', new CustomersService($fire));
inject('holidaysService', new HolidaysService($fire));
inject('timeRecordsService', new TimeRecordsService($fire));
inject('travelRecordsService', new TravelRecordsService($fire));
inject('employeesService', new EmployeesService($fire));
inject('workSchemeService', new WorkSchemeService($axios));
inject('workSchemeService', new WorkSchemeService($axios, $config));
inject('timesheetsService', new TimesheetsService($fire));
inject('mailService', new MailService($axios));
inject('authService', new AuthService($fire, $axios));
inject('authService', new AuthService($fire, $axios, $config));
});
20 changes: 15 additions & 5 deletions services/auth-service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import {NuxtRuntimeConfig} from '@nuxt/types/config/runtime';
import {NuxtAxiosInstance} from '@nuxtjs/axios';
import {NuxtFireInstance} from '@nuxtjs/firebase';
import Cookies from 'js-cookie';
import jwtDecode from 'jwt-decode';
import ApiUrl from '~/helpers/api';

export default class AuthService {
fire: NuxtFireInstance;
axios: NuxtAxiosInstance;
ApiUrl = '/api/v1';
config: NuxtRuntimeConfig;

constructor(fire: NuxtFireInstance, axios: NuxtAxiosInstance) {
constructor(
fire: NuxtFireInstance,
axios: NuxtAxiosInstance,
config: NuxtRuntimeConfig
) {
this.fire = fire;
this.axios = axios;
this.config = config;
}

setAuthCookie(cookieData: string, expires: Date): void {
Expand Down Expand Up @@ -64,9 +71,12 @@ export default class AuthService {
}

async getUserInfo() {
const employeeId = await this.axios.$get(`${this.ApiUrl}/users/me`, {
withCredentials: true,
});
const employeeId = await this.axios.$get(
`${ApiUrl(this.config)}/users/me`,
{
withCredentials: true,
}
);
return employeeId;
}
}
9 changes: 6 additions & 3 deletions services/work-scheme-service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {format} from 'date-fns';

import {NuxtRuntimeConfig} from '@nuxt/types/config/runtime';
import {NuxtAxiosInstance} from '@nuxtjs/axios';
import ApiUrl from '~/helpers/api';

export default class WorkSchemeService {
axios: NuxtAxiosInstance;
ApiUrl = '/api/v1';
config: NuxtRuntimeConfig;

constructor(axios: NuxtAxiosInstance) {
constructor(axios: NuxtAxiosInstance, config: NuxtRuntimeConfig) {
this.axios = axios;
this.config = config;
}

async getWorkScheme(params: {
Expand All @@ -19,7 +22,7 @@ export default class WorkSchemeService {

try {
const response = await this.axios.$get<WorkSchemeResponse>(
`${this.ApiUrl}/users/${bridgeUid}/worktime?date_from=${format(
`${ApiUrl(this.config)}/users/${bridgeUid}/worktime?date_from=${format(
startDate,
'yyyy-MM-dd'
)}&date_to=${format(endDate, 'yyyy-MM-dd')}`,
Expand Down
12 changes: 10 additions & 2 deletions store/employee/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const actions: ActionTree<EmployeeStoreState, RootStoreState> = {
},

logout({commit}) {
const authService = new AuthService(this.$fire, this.$axios);
const authService = new AuthService(
this.$fire,
this.$axios,
this.app.$config
);

localStorage.removeItem('@fm-hours/ppid');
authService.deleteAuthCookie();
Expand All @@ -45,7 +49,11 @@ const actions: ActionTree<EmployeeStoreState, RootStoreState> = {

try {
const employeesService = new EmployeesService(this.$fire);
const authService = new AuthService(this.$fire, this.$axios);
const authService = new AuthService(
this.$fire,
this.$axios,
this.app.$config
);

if (!authService.getAuthCookie()) {
const ppid =
Expand Down

0 comments on commit 28b8bde

Please sign in to comment.