Skip to content
GitLab
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
4585e5fc
Commit
4585e5fc
authored
Mar 08, 2017
by
Gijs Hendriksen
Browse files
Merge branch 'login' of gitlab.science.ru.nl:thalia/ThaliApp-react into feature-navigation
parents
df375a13
0533a6e7
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/actions/actionTypes.js
View file @
4585e5fc
export
const
LOGIN
=
'
LOGIN
'
;
export
const
NAVIGATE
=
'
NAVIGATE
'
;
export
const
BACK
=
'
BACK
'
;
export
const
LOGINPROGRESS
=
'
LOGINPROGRESS
'
;
export
const
LOGINSUCCESS
=
'
LOGINSUCCESS
'
;
export
const
LOGINFAILURE
=
'
LOGINFAILURE
'
;
app/actions/login.js
View file @
4585e5fc
import
*
as
types
from
'
./actionTypes
'
;
export
function
login
(
username
,
password
)
{
export
function
login
Success
(
username
,
token
)
{
return
{
type
:
types
.
LOGIN
,
success
:
password
===
'
42
'
,
type
:
types
.
LOGINSUCCESS
,
loginState
:
'
loggedIn
'
,
username
,
token
,
};
}
export
function
loginProgress
()
{
return
{
type
:
types
.
LOGINPROGRESS
,
loginState
:
'
progress
'
,
};
}
export
function
loginFailure
()
{
return
{
type
:
types
.
LOGINFAILURE
,
loginState
:
'
failure
'
,
};
}
export
function
login
(
username
,
password
)
{
return
(
dispatch
)
=>
{
dispatch
(
loginProgress
());
const
data
=
{
method
:
'
POST
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
},
body
:
JSON
.
stringify
({
username
,
password
,
}),
};
return
fetch
(
'
http://localhost:8000/api/token-auth
'
,
data
)
.
then
(
response
=>
response
.
json
())
.
then
(
(
responseJson
)
=>
{
if
(
responseJson
.
token
)
{
return
dispatch
(
loginSuccess
(
username
,
responseJson
.
token
));
}
return
dispatch
(
loginFailure
());
})
.
catch
(()
=>
dispatch
(
loginFailure
()));
};
}
app/components/Login.js
View file @
4585e5fc
...
...
@@ -4,6 +4,17 @@ import { connect } from 'react-redux';
import
*
as
actions
from
'
../actions/login
'
;
const
loginResult
=
(
status
)
=>
{
switch
(
status
)
{
case
'
progress
'
:
return
'
Logging in
'
;
case
'
failure
'
:
return
'
Login failed
'
;
default
:
return
''
;
}
};
class
Login
extends
Component
{
constructor
(
props
)
{
super
(
props
);
...
...
@@ -14,12 +25,12 @@ class Login extends Component {
}
render
()
{
const
{
login
Error
,
login
}
=
this
.
props
;
const
{
login
State
,
login
}
=
this
.
props
;
return
(
<
View
>
<
TextInput
placeholder
=
"
Username
"
onChangeText
=
{
user
name
=>
this
.
setState
({
user
name
})}
onChangeText
=
{
name
=>
this
.
setState
({
name
})}
/
>
<
TextInput
placeholder
=
"
Password
"
...
...
@@ -27,15 +38,15 @@ class Login extends Component {
onChangeText
=
{
password
=>
this
.
setState
({
password
})}
/
>
<
Button
title
=
"
Log in
"
onPress
=
{()
=>
login
(
this
.
state
.
username
,
this
.
state
.
password
)}
/
>
<
Text
>
{
login
Error
?
'
Login faal
'
:
''
}
<
/Text
>
<
Text
>
{
login
Result
(
loginState
)
}
<
/Text
>
<
/View
>
);
}
}
Login
.
propTypes
=
{
loginState
:
React
.
PropTypes
.
string
.
isRequired
,
login
:
React
.
PropTypes
.
func
.
isRequired
,
loginError
:
React
.
PropTypes
.
bool
.
isRequired
,
};
const
mapStateToProps
=
state
=>
state
.
login
;
...
...
app/reducers/login.js
View file @
4585e5fc
import
*
as
types
from
'
../actions/actionTypes
'
;
const
initialState
=
{
loggedIn
:
false
,
loginError
:
false
,
loginState
:
''
,
token
:
''
,
username
:
''
,
};
export
default
function
login
(
state
=
initialState
,
action
=
{})
{
switch
(
action
.
type
)
{
case
types
.
LOGIN
:
if
(
action
.
success
)
{
return
{
...
state
,
loginError
:
false
,
loggedIn
:
true
};
}
return
{
...
state
,
loginError
:
true
};
case
types
.
LOGINSUCCESS
:
return
{
...
state
,
loginState
:
'
success
'
,
username
:
action
.
username
,
token
:
action
.
token
};
case
types
.
LOGINFAILURE
:
return
{
...
state
,
loginState
:
'
failure
'
};
case
types
.
LOGINPROGRESS
:
return
{
...
state
,
loginState
:
'
progress
'
};
default
:
return
{
...
state
};
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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