@@ -9,7 +9,6 @@ const initialState = {
99} ;
1010
1111const ACTION_IN_PROGRESS = 'lsn/auth/ACTION_IN_PROGRESS' ;
12-
1312function fetchingInProgress ( ) {
1413 return {
1514 type : ACTION_IN_PROGRESS ,
@@ -18,7 +17,6 @@ function fetchingInProgress() {
1817}
1918
2019const LOGGED_USER = 'lsn/auth/LOGGED_USER' ;
21-
2220export function loggedUser ( username , password ) {
2321 return {
2422 type : LOGGED_USER ,
@@ -31,19 +29,26 @@ const CREATE_NEW_USER = 'lsn/auth/CREATE_NEW_USER';
3129export function createdNewUser ( username , ethAddress ) {
3230 return {
3331 type : CREATE_NEW_USER ,
34- payload : { username, ethAddress }
32+ payload : { username, address : ethAddress }
3533 } ;
3634}
3735
3836const CLEAR_STORED_STATE = 'lsn/auth/CLEAR_STORED_STATE' ;
39-
4037export 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+
4752export 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
119147export const getAuthenticatedUser = ( state ) => get ( state , [ 'auth' , 'username' ] , null ) ;
120148export 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 ) ;
123151export const getAuthErrors = ( state ) => get ( state , [ 'auth' , 'error' ] , null ) ;
0 commit comments