Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
thalia
ThaliApp
Commits
eb5212fb
Commit
eb5212fb
authored
Apr 28, 2017
by
Gijs Hendriksen
Browse files
Store token in session state
parent
d549c73e
Changes
11
Hide whitespace changes
Inline
Side-by-side
app/actions/actionTypes.js
View file @
eb5212fb
export
const
NAVIGATE
=
'
NAVIGATE
'
;
export
const
BACK
=
'
BACK
'
;
export
const
LOGININIT
=
'
LOGININIT
'
;
export
const
LOGINPROGRESS
=
'
LOGINPROGRESS
'
;
export
const
LOGINSUCCESS
=
'
LOGINSUCCESS
'
;
export
const
LOGINFAILURE
=
'
LOGINFAILURE
'
;
...
...
app/actions/calendar.js
View file @
eb5212fb
import
{
AsyncStorage
}
from
'
react-native
'
;
import
*
as
types
from
'
./actionTypes
'
;
const
TOKENKEY
=
'
@MyStore:token
'
;
export
function
calendarRetrieved
(
eventList
)
{
return
{
type
:
types
.
CALENDARRETREIVED
,
...
...
@@ -15,32 +13,27 @@ export function calendarNotRetrieved() {
};
}
export
function
retrieveCalendar
()
{
export
function
retrieveCalendar
(
token
)
{
return
(
dispatch
)
=>
{
AsyncStorage
.
getItem
(
TOKENKEY
)
const
start
=
new
Date
().
toISOString
().
substring
(
0
,
10
);
let
end
=
new
Date
();
end
.
setMonth
(
end
.
getMonth
()
+
6
);
end
=
end
.
toISOString
().
substring
(
0
,
10
);
const
data
=
{
method
:
'
GET
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
Authorization
:
`Token
${
token
}
`
,
},
};
return
fetch
(
`http://localhost:8000/api/events/eventlist/?start=
${
start
}
&end=
${
end
}
`
,
data
)
.
then
(
(
token
)
=>
{
const
start
=
new
Date
().
toISOString
().
substring
(
0
,
10
);
let
end
=
new
Date
();
end
.
setMonth
(
end
.
getMonth
()
+
6
);
end
=
end
.
toISOString
().
substring
(
0
,
10
);
const
data
=
{
method
:
'
GET
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
Authorization
:
`Token
${
token
}
`
,
},
};
return
fetch
(
`http://localhost:8000/api/events/eventlist/?start=
${
start
}
&end=
${
end
}
`
,
data
)
.
then
(
response
=>
response
.
json
())
.
then
(
responseJson
=>
dispatch
(
calendarRetrieved
(
responseJson
)))
.
catch
(()
=>
{
dispatch
(
calendarNotRetrieved
());
});
},
);
response
=>
response
.
json
())
.
then
(
responseJson
=>
dispatch
(
calendarRetrieved
(
responseJson
)))
.
catch
(()
=>
{
dispatch
(
calendarNotRetrieved
());
});
};
}
app/actions/events.js
View file @
eb5212fb
import
{
AsyncStorage
}
from
'
react-native
'
;
import
*
as
types
from
'
./actionTypes
'
;
import
{
navigate
}
from
'
./navigation
'
;
const
TOKENKEY
=
'
@MyStore:token
'
;
export
function
success
(
data
)
{
return
{
type
:
types
.
LOADEVENTSUCCESS
,
...
...
@@ -17,40 +14,31 @@ export function fail() {
};
}
export
function
loadEvent
(
id
)
{
export
function
loadEvent
(
id
,
token
)
{
return
(
dispatch
)
=>
{
AsyncStorage
.
getItem
(
TOKENKEY
)
const
data
=
{
method
:
'
GET
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
Authorization
:
`Token
${
token
}
`
,
},
};
return
fetch
(
`http://localhost:8000/api/events/data/?event_id=
${
id
}
`
,
data
)
.
then
(
response
=>
response
.
json
(),
)
.
then
(
(
token
)
=>
{
const
data
=
{
method
:
'
GET
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
Authorization
:
`Token
${
token
}
`
,
},
};
return
fetch
(
`http://localhost:8000/api/events/data/?event_id=
${
id
}
`
,
data
)
.
then
(
response
=>
response
.
json
(),
)
.
then
(
(
response
)
=>
{
dispatch
(
success
(
response
));
},
)
.
catch
(
()
=>
{
dispatch
(
fail
());
},
);
(
response
)
=>
{
dispatch
(
success
(
response
));
dispatch
(
navigate
(
'
event
'
));
},
)
.
catch
(
()
=>
{
dispatch
(
fail
());
dispatch
(
navigate
(
'
event
'
));
},
);
dispatch
(
navigate
(
'
event
'
));
};
}
app/actions/login.js
View file @
eb5212fb
...
...
@@ -58,14 +58,6 @@ export function login(username, password) {
};
}
export
function
initLogin
(
username
,
token
)
{
return
{
type
:
types
.
LOGININIT
,
username
,
token
,
};
}
export
function
logout
()
{
AsyncStorage
.
multiRemove
([
USERNAMEKEY
,
TOKENKEY
]);
return
{
...
...
app/app.js
View file @
eb5212fb
...
...
@@ -6,7 +6,7 @@ import thunk from 'redux-thunk';
import
*
as
reducers
from
'
./reducers
'
;
import
ReduxNavigator
from
'
./components/navigator
'
;
import
{
initLogin
}
from
'
./actions/login
'
;
import
{
loginSuccess
}
from
'
./actions/login
'
;
const
createStoreWithMiddleware
=
applyMiddleware
(
thunk
)(
createStore
);
const
reducer
=
combineReducers
(
reducers
);
...
...
@@ -30,7 +30,7 @@ class Main extends Component {
const
token
=
values
[
TOKENKEY
];
if
(
username
!==
null
&&
token
!==
null
)
{
store
.
dispatch
(
initLogin
(
username
,
token
));
store
.
dispatch
(
loginSuccess
(
username
,
token
));
}
});
}
...
...
app/components/Calendar.js
View file @
eb5212fb
...
...
@@ -6,7 +6,7 @@ import EventCard from './EventCard';
const
Calendar
=
(
props
)
=>
{
if
(
!
props
.
calendarFetched
)
{
props
.
retrieveCalendar
();
props
.
retrieveCalendar
(
props
.
token
);
return
(
<
View
>
<
Text
>
...
...
@@ -42,10 +42,16 @@ Calendar.propTypes = {
})).
isRequired
,
calendarFetched
:
React
.
PropTypes
.
bool
.
isRequired
,
retrieveCalendar
:
React
.
PropTypes
.
func
.
isRequired
,
token
:
React
.
PropTypes
.
string
.
isRequired
,
};
const
mapStateToProps
=
state
=>
state
.
calendar
;
const
mapStateToProps
=
state
=>
({
...
state
.
calendar
,
token
:
state
.
session
.
token
,
});
const
mapDispatchToProps
=
dispatch
=>
({
retrieveCalendar
:
()
=>
dispatch
(
actions
.
retrieveCalendar
()),
retrieveCalendar
:
token
=>
dispatch
(
actions
.
retrieveCalendar
(
token
)),
});
export
default
connect
(
mapStateToProps
,
mapDispatchToProps
)(
Calendar
);
app/components/EventCard.js
View file @
eb5212fb
...
...
@@ -9,7 +9,7 @@ const EventCard = props =>
<
Text
>
{
new
Date
(
props
.
event
.
start
).
toISOString
().
substring
(
0
,
10
)}
<
/Text
>
<
i
>
{
props
.
event
.
description
}
<
/i
>
<
Text
>-----------------------------------------<
/Text
>
<
Button
title
=
"
Openen
"
onPress
=
{()
=>
props
.
loadEvent
(
props
.
event
.
id
)}
/
>
<
Button
title
=
"
Openen
"
onPress
=
{()
=>
props
.
loadEvent
(
props
.
event
.
id
,
props
.
token
)}
/
>
<
/View
>
;
...
...
@@ -23,10 +23,15 @@ EventCard.propTypes = {
id
:
React
.
PropTypes
.
number
,
}).
isRequired
,
loadEvent
:
React
.
PropTypes
.
func
.
isRequired
,
token
:
React
.
PropTypes
.
string
.
isRequired
,
};
const
mapStateToProps
=
state
=>
({
token
:
state
.
session
.
token
,
});
const
mapDispatchToProps
=
dispatch
=>
({
loadEvent
:
id
=>
dispatch
(
actions
.
loadEvent
(
id
)),
loadEvent
:
(
id
,
token
)
=>
dispatch
(
actions
.
loadEvent
(
id
,
token
)),
});
export
default
connect
(
()
=>
({})
,
mapDispatchToProps
)(
EventCard
);
export
default
connect
(
mapStateToProps
,
mapDispatchToProps
)(
EventCard
);
app/components/Login.js
View file @
eb5212fb
...
...
@@ -51,7 +51,7 @@ Login.propTypes = {
login
:
React
.
PropTypes
.
func
.
isRequired
,
};
const
mapStateToProps
=
state
=>
state
.
logi
n
;
const
mapStateToProps
=
state
=>
state
.
sessio
n
;
const
mapDispatchToProps
=
dispatch
=>
({
login
:
(
username
,
password
)
=>
dispatch
(
actions
.
login
(
username
,
password
)),
});
...
...
app/reducers/index.js
View file @
eb5212fb
import
logi
n
from
'
./
logi
n
'
;
import
sessio
n
from
'
./
sessio
n
'
;
import
navigation
from
'
./navigation
'
;
import
events
from
'
./events
'
;
import
calendar
from
'
./calendar
'
;
export
{
logi
n
,
sessio
n
,
navigation
,
events
,
calendar
,
...
...
app/reducers/navigation.js
View file @
eb5212fb
...
...
@@ -52,12 +52,6 @@ export default function navigate(state = initialState, action = {}) {
drawerOpen
:
action
.
drawerOpen
,
};
}
case
types
.
LOGININIT
:
{
return
{
...
state
,
loggedIn
:
true
,
};
}
case
types
.
LOGOUT
:
{
return
initialState
;
}
...
...
app/reducers/
logi
n.js
→
app/reducers/
sessio
n.js
View file @
eb5212fb
...
...
@@ -6,7 +6,7 @@ const initialState = {
username
:
''
,
};
export
default
function
logi
n
(
state
=
initialState
,
action
=
{})
{
export
default
function
sessio
n
(
state
=
initialState
,
action
=
{})
{
switch
(
action
.
type
)
{
case
types
.
LOGINSUCCESS
:
return
{
...
state
,
loginState
:
'
success
'
,
username
:
action
.
username
,
token
:
action
.
token
};
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment