upgrade login component && implement fetch
parent
69e6e07db6
commit
02679b51d8
|
|
@ -5,7 +5,13 @@ import ConsoleLoad from '../consoleLoad'
|
||||||
const help = () => {
|
const help = () => {
|
||||||
return "register - register user by form \n"
|
return "register - register user by form \n"
|
||||||
+ "login - login user by form \n"
|
+ "login - login user by form \n"
|
||||||
+ "logout - logout user \n"
|
+ "start - start command \n"
|
||||||
|
+ " -a --app - app flag - start music service app \n"
|
||||||
|
+ "clean - clean screen \n"
|
||||||
|
}
|
||||||
|
|
||||||
|
const helpUser = () => {
|
||||||
|
return "logout - logout user \n"
|
||||||
+ "start - start command \n"
|
+ "start - start command \n"
|
||||||
+ " -a --app - app flag - start music service app \n"
|
+ " -a --app - app flag - start music service app \n"
|
||||||
+ "clean - clean screen \n"
|
+ "clean - clean screen \n"
|
||||||
|
|
@ -34,6 +40,7 @@ const undefined = (command) => {
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
help,
|
help,
|
||||||
|
helpUser,
|
||||||
register,
|
register,
|
||||||
startApp,
|
startApp,
|
||||||
undefined
|
undefined
|
||||||
|
|
|
||||||
|
|
@ -1,50 +1,76 @@
|
||||||
import React, { useState } from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
import { connect } from 'react-redux'
|
import { connect } from 'react-redux'
|
||||||
|
|
||||||
import { postAuth } from '../../../../../stores/user/duck/operations'
|
import { postAuth } from '../../../../../stores/user/duck/operations'
|
||||||
|
|
||||||
|
|
||||||
const Login = ({ user, postAuth }) => {
|
const Login = ({ user, postAuth, consoleHistory, setConsoleHistory, componentVisible }) => {
|
||||||
|
|
||||||
const loginInput = React.createRef()
|
const loginInput = React.createRef()
|
||||||
const passwInput = React.createRef()
|
const passwInput = React.createRef()
|
||||||
|
|
||||||
const [message, setMessage] = useState('')
|
const [message, setMessage] = useState('')
|
||||||
|
|
||||||
const login = (event) => {
|
const login = async (event) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
if ( loginInput.current.value !== '' &&
|
|
||||||
passwInput.current.value !== '') {
|
let login = loginInput.current.value
|
||||||
postAuth(
|
let password = passwInput.current.value
|
||||||
loginInput.current.value,
|
|
||||||
passwInput.current.value
|
if ( login !== '' && password !== '') {
|
||||||
).then( response => {
|
|
||||||
|
postAuth(login, password)
|
||||||
|
.then( response => {
|
||||||
setMessage( response['error'] )
|
setMessage( response['error'] )
|
||||||
})
|
})
|
||||||
document.getElementById('passwInput').disbled = true
|
|
||||||
document.getElementById('passwInput').disabled = true
|
|
||||||
} else if ( passwInput.current.value === '' ) {
|
} else if ( passwInput.current.value === '' ) {
|
||||||
document.getElementById('passwInput').focus()
|
document.getElementById('passwInput').focus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => {
|
||||||
|
if (message !== '') {
|
||||||
|
let save = 'login: ' + loginInput.current.value + '\n'
|
||||||
|
+ 'password: ' + hidePassword( passwInput.current.value ) + '\n'
|
||||||
|
+ message + '\n'
|
||||||
|
|
||||||
|
loginInput.current.value = ''
|
||||||
|
passwInput.current.value = ''
|
||||||
|
|
||||||
|
setConsoleHistory( consoleHistory + save )
|
||||||
|
componentVisible( false )
|
||||||
|
setMessage('')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const hidePassword = (password) => {
|
||||||
|
let hide = ''
|
||||||
|
for (let i = 0; i <= password.length; i++)
|
||||||
|
hide += '*'
|
||||||
|
return hide
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<form onSubmit={ login }>
|
<form onSubmit={ login }>
|
||||||
login:
|
login:
|
||||||
<input
|
<input
|
||||||
id='loginInput'
|
id='loginInput'
|
||||||
|
autoComplete='off'
|
||||||
ref={ loginInput }
|
ref={ loginInput }
|
||||||
/>
|
/>
|
||||||
<br />
|
<br />
|
||||||
password:
|
password:
|
||||||
<input
|
<input
|
||||||
id='passwInput'
|
id='passwInput'
|
||||||
|
autoComplete='off'
|
||||||
type='password'
|
type='password'
|
||||||
ref={ passwInput }
|
ref={ passwInput }
|
||||||
/>
|
/>
|
||||||
<br />
|
<br />
|
||||||
{ message }
|
|
||||||
<button type='submit' />
|
<button type='submit' />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -57,6 +83,7 @@ const mapStateToProps = state => ({
|
||||||
|
|
||||||
const mapDispatchToProps = dispatch => ({
|
const mapDispatchToProps = dispatch => ({
|
||||||
postAuth: (username, password) => dispatch( postAuth(username, password) )
|
postAuth: (username, password) => dispatch( postAuth(username, password) )
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(Login)
|
export default connect(mapStateToProps, mapDispatchToProps)(Login)
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
|
import { connect } from 'react-redux'
|
||||||
|
|
||||||
import commands from './commands/commands'
|
import commands from './commands/commands'
|
||||||
import Login from './commands/fetchCommands/Login'
|
import Login from './commands/fetchCommands/Login'
|
||||||
// import Logout from './commands/fetchCommands/Logout'
|
import Logout from './commands/fetchCommands/Logout'
|
||||||
|
|
||||||
import '../../../styles/general.scss'
|
import '../../../styles/general.scss'
|
||||||
|
|
||||||
|
|
||||||
const IndexConsole = ( user ) => {
|
const IndexConsole = ({ user }) => {
|
||||||
|
|
||||||
useEffect( () => resizeConsoleDiv(), [])
|
useEffect( () => resizeConsoleDiv(), [])
|
||||||
|
|
||||||
|
|
@ -28,7 +29,19 @@ const IndexConsole = ( user ) => {
|
||||||
|
|
||||||
const consoleInput = React.createRef()
|
const consoleInput = React.createRef()
|
||||||
|
|
||||||
let consoleUser = 'guest@00x097 * > '
|
let consoleUser = user.username !== ''
|
||||||
|
? user.username + '@00x097 # > '
|
||||||
|
: 'guest@00x097 * > '
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => {
|
||||||
|
if ( user.username !== '' ) {
|
||||||
|
consoleUser = user.username + '@00x097 # > '
|
||||||
|
} else {
|
||||||
|
consoleUser = 'guest@00x097 * > '
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const detectCommand = (event) => {
|
const detectCommand = (event) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
@ -61,10 +74,18 @@ const IndexConsole = ( user ) => {
|
||||||
</pre>
|
</pre>
|
||||||
<div id='inputForms'>
|
<div id='inputForms'>
|
||||||
<div style={ loginCommand === true ? {display: 'block'} : {display: 'none'} } >
|
<div style={ loginCommand === true ? {display: 'block'} : {display: 'none'} } >
|
||||||
<Login visible={ loginCommand } consoleHistory={ consoleHistory } />
|
<Login
|
||||||
|
consoleHistory={ consoleHistory }
|
||||||
|
setConsoleHistory={ setConsoleHistory }
|
||||||
|
componentVisible={ setVisibleLoginForm }
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div style={ logoutCommand === true ? {display: 'block'} : {display: 'none'} } >
|
<div style={ logoutCommand === true ? {display: 'block'} : {display: 'none'} } >
|
||||||
|
<Logout
|
||||||
|
consoleHistory={ consoleHistory }
|
||||||
|
setConsoleHistory={ setConsoleHistory }
|
||||||
|
componentVisible={ setVisibleLogoutForm }
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<form onSubmit={ detectCommand } style={ loginCommand || logoutCommand ? {display: 'none'} : {display: 'block'} }>
|
<form onSubmit={ detectCommand } style={ loginCommand || logoutCommand ? {display: 'none'} : {display: 'block'} }>
|
||||||
|
|
@ -80,4 +101,8 @@ const IndexConsole = ( user ) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default IndexConsole
|
const mapStateToProps = state => ({
|
||||||
|
user: state.user
|
||||||
|
})
|
||||||
|
|
||||||
|
export default connect(mapStateToProps, )(IndexConsole)
|
||||||
|
|
@ -15,6 +15,7 @@ export const postAuth = (username, password) => async (dispatch) => {
|
||||||
username: username,
|
username: username,
|
||||||
password: password
|
password: password
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
return await AppService._post(
|
return await AppService._post(
|
||||||
endpoint + 'auth',
|
endpoint + 'auth',
|
||||||
body,
|
body,
|
||||||
|
|
@ -33,9 +34,12 @@ export const postAuth = (username, password) => async (dispatch) => {
|
||||||
dispatch(actions.login(serviceUser))
|
dispatch(actions.login(serviceUser))
|
||||||
return { error: 'login success' }
|
return { error: 'login success' }
|
||||||
} catch {
|
} catch {
|
||||||
return response
|
return { error: 'login failed' }
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} catch {
|
||||||
|
return { error: 'connection failed' }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deleteAuth = (token) => async (dispatch) => {
|
export const deleteAuth = (token) => async (dispatch) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue