add initData
Change-Id: I08e5dfe42478e9ee02e48682634ecaa9ab34723b
diff --git a/client/src/AuthManager.js b/client/src/AuthManager.js
index d6cc6b6..83f731f 100644
--- a/client/src/AuthManager.js
+++ b/client/src/AuthManager.js
@@ -31,6 +31,12 @@
this.tasks = []
this.onAuthChanged = undefined
+
+ if (initData) {
+ console.log("Using static initData")
+ this.setInitData(initData)
+ return
+ }
}
isAuthenticated() {
@@ -41,37 +47,51 @@
return this.state
}
+ setInitData(data) {
+ this.authenticating = false
+ this.state.initialized = true
+ if (data.username) {
+ Object.assign(this.state, {
+ authenticated: true,
+ setupComplete: true,
+ error: false,
+ user: { username: data.username, type: data.type }
+ })
+ } else {
+ Object.assign(this.state, {
+ authenticated: false,
+ setupComplete: 'setupComplete' in data ? data.setupComplete : true,
+ error: false
+ })
+ }
+ console.log("Init ended")
+ /*if (this.onAuthChanged)
+ this.onAuthChanged(this.state)*/
+ }
+
init(cb) {
this.onAuthChanged = cb
if (this.state.initialized || this.authenticating)
return
- console.log("Init")
+ /*if (initData) {
+ console.log("Using static initData")
+ this.setInitData(initData)
+ return
+ }*/
this.authenticating = true
fetch('/auth')
.then(async (response) => {
this.authenticating = false
this.state.initialized = true
- console.log("Init ended")
if (response.status === 200) {
- const jsonData = await response.json()
- Object.assign(this.state, {
- authenticated: true,
- setupComplete: true,
- error: false,
- user: { username: jsonData.username, type: jsonData.type }
- })
+ this.setInitData(await response.json())
} else if (response.status === 401) {
- const jsonData = await response.json()
- Object.assign(this.state, {
- authenticated: false,
- setupComplete: 'setupComplete' in jsonData ? jsonData.setupComplete : true,
- error: false
- })
+ this.setInitData(await response.json())
} else {
this.state.error = true
+ if (this.onAuthChanged)
+ this.onAuthChanged(this.state)
}
- if (this.onAuthChanged)
- this.onAuthChanged(this.state)
}).catch(e => {
this.authenticating = false
console.log(e)
@@ -124,7 +144,9 @@
while (this.tasks.length !== 0) {
const task = this.tasks.shift()
if (this.state.authenticated)
- fetch(task.url, task.init).then(res => task.resolve(res))
+ fetch(task.url, task.init)
+ .then(res => task.resolve(res))
+ .catch(e => console.log("Error executing pending task: " + e))
else
task.reject(new Error("Authentication failed"))
}
diff --git a/client/src/index.ejs b/client/src/index.ejs
index 162d470..bfe8174 100644
--- a/client/src/index.ejs
+++ b/client/src/index.ejs
@@ -7,6 +7,7 @@
<link rel="apple-touch-icon" href="/logo192.png" />
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
<meta name="description" content="Web site created using create-react-app" />
+ <script>const initData=<%- initdata %></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
<title>Jami Web Node</title>
</head>
diff --git a/client/src/pages/accountSelection.jsx b/client/src/pages/accountSelection.jsx
index 0cd9e29..c95b02b 100644
--- a/client/src/pages/accountSelection.jsx
+++ b/client/src/pages/accountSelection.jsx
@@ -7,8 +7,10 @@
import ListItemLink from '../components/ListItemLink';
import ConversationAvatar from '../components/ConversationAvatar';
import { AddRounded } from '@material-ui/icons';
+import { useHistory } from 'react-router';
const AccountSelection = (props) => {
+ const history = useHistory()
const [state, setState] = useState({
loaded: false,
error: false,
@@ -21,10 +23,14 @@
.then(res => res.json())
.then(result => {
console.log(result)
- setState({
- loaded: true,
- accounts: result.map(account => Account.from(account)),
- })
+ if (result.length === 0) {
+ history.replace('/newAccount')
+ } else {
+ setState({
+ loaded: true,
+ accounts: result.map(account => Account.from(account)),
+ })
+ }
}, error => {
console.log(`get error ${error}`)
setState({
diff --git a/client/src/pages/serverSetup.jsx b/client/src/pages/serverSetup.jsx
index 29e4ea5..703f842 100644
--- a/client/src/pages/serverSetup.jsx
+++ b/client/src/pages/serverSetup.jsx
@@ -1,9 +1,9 @@
-import React, { useState } from 'react';
-import { useHistory } from "react-router-dom";
+import React, { useState } from 'react'
+import { useHistory } from "react-router-dom"
-import { Box, Container, Fab, Card, CardContent, Typography, Input } from '@material-ui/core';
-import GroupAddRounded from '@material-ui/icons/GroupAddRounded';
-import { makeStyles } from '@material-ui/core/styles';
+import { Box, Container, Fab, Card, CardContent, Typography, Input } from '@material-ui/core'
+import GroupAddRounded from '@material-ui/icons/GroupAddRounded'
+import { makeStyles } from '@material-ui/core/styles'
import authManager from '../AuthManager'
const useStyles = makeStyles((theme) => ({
@@ -26,21 +26,19 @@
export default function ServerSetup(props) {
const classes = useStyles()
- const history = useHistory();
- const [password, setPassword] = useState('');
- const [passwordRepeat, setPasswordRepeat] = useState('');
- const [loading, setLoading] = useState(false);
+ const history = useHistory()
+ const [password, setPassword] = useState('')
+ const [passwordRepeat, setPasswordRepeat] = useState('')
+ const [loading, setLoading] = useState(false)
const isValid = () => password && password === passwordRepeat
- const handleSubmit = async e => {
+ const handleSubmit = e => {
e.preventDefault()
setLoading(true)
if (!isValid())
return
- if (await authManager.setup(password)) {
- history.replace('/')
- }
+ authManager.setup(password)
}
return (
diff --git a/client/webpack.config.js b/client/webpack.config.js
index 7a2a6de..beb3a23 100644
--- a/client/webpack.config.js
+++ b/client/webpack.config.js
@@ -15,7 +15,7 @@
let entry = [resolve(__dirname, 'src', 'index.js')]
let plugins = [
- new HtmlWebpackPlugin({ template: resolve(__dirname, 'src', 'index.ejs') }),
+ new HtmlWebpackPlugin({ template: '!!raw-loader!' + resolve(__dirname, 'src', 'index.ejs'), filename: 'index.ejs' }),
new CopyWebpackPlugin({
patterns: [{ from: resolve(__dirname, 'public'), to: resolve(__dirname, 'dist') }]
})