Skip to content

Commit

Permalink
Consolidate UI configuration in one place, allowing it to be overridd…
Browse files Browse the repository at this point in the history
…en from the server
  • Loading branch information
deluan committed Apr 8, 2020
1 parent d0188db commit a0f389f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ui/src/authProvider.js
@@ -1,11 +1,12 @@
import jwtDecode from 'jwt-decode'
import md5 from 'md5-hex'
import baseUrl from './utils/baseUrl'
import config from './config'

const authProvider = {
login: ({ username, password }) => {
let url = baseUrl('/app/login')
if (localStorage.getItem('initialAccountCreation')) {
if (config.firstTime) {
url = baseUrl('/app/createAdmin')
}
const request = new Request(url, {
Expand Down
20 changes: 20 additions & 0 deletions ui/src/config.js
@@ -0,0 +1,20 @@
const defaultConfig = {
firstTime: false,
baseURL: '',
loginBackgroundURL: 'https://source.unsplash.com/random/1600x900?music'
}

let config

try {
const appConfig = JSON.parse(window.__APP_CONFIG__)

config = {
...defaultConfig,
...appConfig
}
} catch (e) {
config = defaultConfig
}

export default config
5 changes: 3 additions & 2 deletions ui/src/layout/Login.js
Expand Up @@ -15,6 +15,7 @@ import LockIcon from '@material-ui/icons/Lock'
import { Notification, useLogin, useNotify, useTranslate } from 'react-admin'

import LightTheme from '../themes/light'
import config from '../config'

const useStyles = makeStyles((theme) => ({
main: {
Expand All @@ -23,7 +24,7 @@ const useStyles = makeStyles((theme) => ({
minHeight: '100vh',
alignItems: 'center',
justifyContent: 'flex-start',
background: 'url(https://source.unsplash.com/random/1600x900?music)',
background: `url(${config.loginBackgroundURL})`,
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
backgroundPosition: 'center'
Expand Down Expand Up @@ -253,7 +254,7 @@ const Login = ({ location }) => {
return errors
}

if (localStorage.getItem('initialAccountCreation') === 'true') {
if (config.firstTime) {
return (
<FormSignUp
handleSubmit={handleSubmit}
Expand Down
4 changes: 3 additions & 1 deletion ui/src/utils/baseUrl.js
@@ -1,5 +1,7 @@
import config from '../config'

const baseUrl = (path) => {
const base = localStorage.getItem('baseURL') || ''
const base = config.baseURL || ''
const parts = [base]
parts.push(path.replace(/^\//, ''))
return parts.join('/')
Expand Down

0 comments on commit a0f389f

Please sign in to comment.