Skip to content

Commit a78f9f7

Browse files
committed
Updated and fixed signup and signin
1 parent b0f6c76 commit a78f9f7

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

src/components/form/login.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ const Actions = styled.div`
2323
text-align: center;
2424
`;
2525

26-
const LoginForm = ({ url, authErrors, handleSubmit }) => {
26+
const LoginForm = ({ user, authErrors, handleSubmit }) => {
2727
const buttonText = 'Sign in';
2828
const buttonTextSubmitting = 'Signing in';
2929

3030
return (
3131
<Formik
32-
initialValues={{ username: '', password: '', server: '' }}
32+
initialValues={{ username: user || '', password: '', server: '' }}
3333
validate={values => ({ ...validateAccount(values), ...validatePassword(values) })}
3434
onSubmit={(values, { setSubmitting, setErrors }) => {
35-
handleSubmit(url, values)
35+
handleSubmit(values)
3636
.catch(err => {
3737
setErrors({
3838
server: err.message
@@ -81,7 +81,7 @@ const mapStateToProps = (state) => {
8181

8282
const mapDispatchToProps = (dispatch) => {
8383
return {
84-
handleSubmit(url, { username, password }) {
84+
handleSubmit({ username, password }) {
8585
return dispatch(login({ username, password }));
8686
}
8787
};

src/store/auth/index.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const initialState = {
99
};
1010

1111
const ACTION_IN_PROGRESS = 'lsn/auth/ACTION_IN_PROGRESS';
12-
1312
function fetchingInProgress() {
1413
return {
1514
type: ACTION_IN_PROGRESS,
@@ -18,7 +17,6 @@ function fetchingInProgress() {
1817
}
1918

2019
const LOGGED_USER = 'lsn/auth/LOGGED_USER';
21-
2220
export function loggedUser(username, password) {
2321
return {
2422
type: LOGGED_USER,
@@ -31,19 +29,26 @@ const CREATE_NEW_USER = 'lsn/auth/CREATE_NEW_USER';
3129
export function createdNewUser(username, ethAddress) {
3230
return {
3331
type: CREATE_NEW_USER,
34-
payload: { username, ethAddress }
32+
payload: { username, address: ethAddress }
3533
};
3634
}
3735

3836
const CLEAR_STORED_STATE = 'lsn/auth/CLEAR_STORED_STATE';
39-
4037
export function clearStoredState() {
4138
return {
4239
type: CLEAR_STORED_STATE,
4340
payload: null
4441
};
4542
}
4643

44+
const RECEIVE_AUTH_ERROR = 'lsn/auth/RECEIVE_AUTH_ERROR';
45+
export function receiveAuthError(error) {
46+
return {
47+
type: RECEIVE_AUTH_ERROR,
48+
payload: typeof error === 'string' ? error : error.message
49+
};
50+
}
51+
4752
export function createUser({ username, password }) {
4853
return (dispatch) => {
4954
return new Promise((resolve, reject) => {
@@ -55,6 +60,7 @@ export function createUser({ username, password }) {
5560
resolve(ethAddress);
5661
})
5762
.catch((error) => {
63+
console.error(error);
5864
dispatch(receiveAuthError(error));
5965
reject(error);
6066
});
@@ -66,10 +72,16 @@ export function login({ username, password }) {
6672
return (dispatch, getState) => {
6773
return new Promise((resolve, reject) => {
6874
const userAddr = getUserAddress(getState(), username);
75+
if(!userAddr) {
76+
const errMsg = `User ${username} is not registered`;
77+
dispatch(receiveAuthError(errMsg));
78+
reject(new Error(errMsg));
79+
return;
80+
}
6981
dispatch(unlockAccountAction(userAddr, password))
70-
.then((response) => {
82+
.then(() => {
7183
dispatch(loggedUser(username, password));
72-
resolve(response.token);
84+
resolve(userAddr);
7385
})
7486
.catch((error) => {
7587
if (error.response && typeof error.response.json === 'function') {
@@ -78,6 +90,7 @@ export function login({ username, password }) {
7890
reject(new Error(err.message));
7991
});
8092
} else {
93+
console.error(error);
8194
dispatch(receiveAuthError(error));
8295
reject(error);
8396
}
@@ -96,20 +109,35 @@ export default function authReducer(state = initialState, action = {}) {
96109
case LOGGED_USER:
97110
return {
98111
...state,
99-
username: payload.username,
100-
password: payload.password
112+
error: null,
113+
inProgress: false,
114+
username: action.payload.username,
115+
password: action.payload.password
101116
};
102117
case CREATE_NEW_USER:
103118
return {
104119
...state,
120+
error: null,
121+
inProgress: false,
105122
addresses: {
106123
...state.addresses,
107124
[action.payload.username]: action.payload.address
108125
},
109126
};
110127

128+
case RECEIVE_AUTH_ERROR:
129+
return {
130+
...state,
131+
inProgress: false,
132+
error: action.payload
133+
};
134+
111135
case CLEAR_STORED_STATE:
112-
return initialState;
136+
return {
137+
...initialState,
138+
addresses: state.addresses,
139+
username: state.username
140+
};
113141

114142
default:
115143
return state;
@@ -118,6 +146,6 @@ export default function authReducer(state = initialState, action = {}) {
118146

119147
export const getAuthenticatedUser = (state) => get(state, ['auth', 'username'], null);
120148
export const getUserAddress = (state, username) => get(state, ['auth', 'addresses', username], null);
121-
export const getUserToken = (state) => null;
122-
export const isAuthenticated = (state) => getAuthenticatedUser(state) !== null;
149+
export const getUserToken = (state) => get(state, ['auth', 'token'], null);
150+
export const isAuthenticated = (state) => (get(state, ['auth', 'password'], null) !== null);
123151
export const getAuthErrors = (state) => get(state, ['auth', 'error'], null);

src/store/keystore/index.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import get from 'lodash.get';
2-
import { Web3 } from 'lightstreams-js-sdk';
2+
import { Web3, EthersWallet as EW } from 'lightstreams-js-sdk';
33
import { web3Cfg } from '../../constants/config'
44

55
const initialState = {
@@ -16,9 +16,8 @@ export const KEYSTORE_SET_WEB3_ACTION = 'lsn/keystore/KEYSTORE_SET_WEB3';
1616
export const initializeWeb3Action = () => (dispatch, getState) => {
1717
return new Promise(async (resolve, reject) => {
1818
try {
19-
const { keystore } = getState();
20-
let web3 = getWeb3Engine(keystore);
21-
const encryptedKeys = getEncryptedKeys(keystore);
19+
let web3 = getWeb3Engine(getState());
20+
const encryptedKeys = getEncryptedKeys(getState());
2221
if (web3 !== null) {
2322
throw new Error(`Web3 is already initialized`);
2423
}
@@ -31,7 +30,7 @@ export const initializeWeb3Action = () => (dispatch, getState) => {
3130
}
3231

3332
encryptedKeys.forEach(encryptedJson => {
34-
console.log(`Import ${EW.Account.formatAddress(encryptedJson.address)} into web3 engine`);
33+
console.log(`Imported ${EW.Account.formatAddress(encryptedJson.address)} into web3 engine`);
3534
Web3.keystore.importAccount(web3, { encryptedJson });
3635
});
3736

@@ -51,9 +50,7 @@ export const initializeWeb3Action = () => (dispatch, getState) => {
5150
export const createAccountAction = (password) => (dispatch, getState) => {
5251
return new Promise(async (resolve, reject) => {
5352
try {
54-
const { keystore } = getState();
55-
const web3 = getWeb3Engine(keystore);
56-
await waitFor(0.2); // WHY? We need to switch CPU context to refresh component
53+
const web3 = getWeb3Engine(getState());
5754
const decryptedWallet = EW.Keystore.createRandomWallet();
5855
const encryptedJson = await EW.Keystore.encryptWallet(decryptedWallet, password);
5956
console.log(`Create new account ${EW.Account.formatAddress(encryptedJson.address)}`);
@@ -77,8 +74,7 @@ export const createAccountAction = (password) => (dispatch, getState) => {
7774
export const unlockAccountAction = (ethAddress, password) => (dispatch, getState) => {
7875
return new Promise(async (resolve, reject) => {
7976
try {
80-
const { keystore } = getState();
81-
const web3 = getWeb3Engine(keystore);
77+
const web3 = getWeb3Engine(getState());
8278
Web3.keystore.unlockAccount(web3, { address: ethAddress, password });
8379
const isUnlocked = Web3.keystore.isAccountLocked(web3, { address: ethAddress });
8480
resolve(isUnlocked);
@@ -91,8 +87,7 @@ export const unlockAccountAction = (ethAddress, password) => (dispatch, getState
9187
export const importAccountAction = (decryptedWallet, password) => (dispatch, getState) => {
9288
return new Promise(async (resolve, reject) => {
9389
try {
94-
const { keystore } = getState();
95-
const web3 = getWeb3Engine(keystore);
90+
const web3 = getWeb3Engine(getState());
9691
await waitFor(0.2);
9792
const encryptedJson = await EW.Keystore.encryptWallet(decryptedWallet, password);
9893
const formattedAddress = EW.Account.formatAddress(encryptedJson.address);

0 commit comments

Comments
 (0)