Added .env access to frontend (+ while dockerized)

This commit is contained in:
Florian Sylvain
2022-12-21 22:37:47 +01:00
parent 4d4116ae98
commit 2047b3f7a4
8 changed files with 37 additions and 23 deletions
-11
View File
@@ -1,11 +0,0 @@
FROM node:latest as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY ./ .
RUN npm run build
FROM nginx as production-stage
RUN mkdir /app
COPY --from=build-stage /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf
+1
View File
@@ -1 +1,2 @@
/// <reference types="vite/client" />
declare const __APP_ENV__: Record<string, string>
+5 -5
View File
@@ -8,8 +8,8 @@ const router = createRouter({
routes: [
{
path: '/',
name: 'home',
component: Home
name: 'login',
component: Login
},
{
path: '/debug',
@@ -17,9 +17,9 @@ const router = createRouter({
component: Debug
},
{
path: '/login',
name: 'login',
component: Login
path: '/home',
name: 'home',
component: Home
},
]
})
+2 -2
View File
@@ -7,7 +7,7 @@ const email = ref('')
const password = ref('')
function getArticles() {
fetch(`http://localhost:8080/articles/`, {
fetch(`http://localhost:${__APP_ENV__.API_PORT}/articles/`, {
headers: { "Authorization": `Bearer ${useAuthStore().token}` }
})
.then(response => response.json())
@@ -25,7 +25,7 @@ function jwtHandler(apiResponse: jwtFormat): void {
}
function login(email: string, password: string): void {
fetch(`http://localhost:8080/login/`, {
fetch(`http://localhost:${__APP_ENV__.API_PORT}/login/`, {
method: "POST",
body: JSON.stringify({
email: email,
+3 -2
View File
@@ -16,8 +16,9 @@ function jwtHandler(apiResponse: jwtFormat): void {
isTokenOK.value = true
}
//TODO rempalcer tous les :8080 par la var d'env du port de l'API (sinon c un peu dommage)
function login(email: string, password: string): void {
fetch(`http://localhost:8080/login/`, {
fetch(`http://localhost:${__APP_ENV__.API_PORT}/login/`, {
method: "POST",
body: JSON.stringify({
email: email,
@@ -28,7 +29,7 @@ function login(email: string, password: string): void {
.then(result => {
jwtHandler(result)
if (isTokenOK.value === true) {
router.push('/')
router.push('/home')
}
})
}
+16 -9
View File
@@ -1,14 +1,21 @@
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
export default defineConfig(({command, mode}) => {
return {
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
define: {
__APP_ENV__: {
...loadEnv(mode, "./../../", ""), // when running directly | access .env from ./web/admin-gui/
...loadEnv(mode, process.cwd(), ""), // when running from docker
}
}
}
})