Skip to content

Commit dc75de8

Browse files
committed
simplify login action
1 parent b6d1204 commit dc75de8

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/containers/Login/Login.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ export default class Login extends Component {
2828

2929
onFacebookLogin = (err, data) => {
3030
if (err) return;
31-
this.props.login('facebook', data, false).then(this.successLogin).catch(error => {
32-
if (error.message === 'Incomplete oauth registration') {
33-
this.context.router.push({
34-
pathname: '/register',
35-
state: { oauth: error.data }
36-
});
37-
}
38-
});
31+
this.props
32+
.login('facebook', data)
33+
.then(this.successLogin)
34+
.catch(error => {
35+
if (error.message === 'Incomplete oauth registration') {
36+
this.context.router.push({
37+
pathname: '/register',
38+
state: { oauth: error.data }
39+
});
40+
}
41+
});
3942
};
4043

4144
login = data => this.props.login('local', data).then(this.successLogin);
@@ -49,18 +52,19 @@ export default class Login extends Component {
4952
return data;
5053
};
5154

52-
FacebookLoginButton = ({ facebookLogin }) =>
53-
(<button className="btn btn-primary" onClick={facebookLogin}>
55+
FacebookLoginButton = ({ facebookLogin }) => (
56+
<button className="btn btn-primary" onClick={facebookLogin}>
5457
Login with <i className="fa fa-facebook-f" />
55-
</button>);
58+
</button>
59+
);
5660

5761
render() {
5862
const { user, logout } = this.props;
5963
return (
6064
<div className="container">
6165
<Helmet title="Login" />
6266
<h1>Login</h1>
63-
{!user &&
67+
{!user && (
6468
<div>
6569
<LoginForm onSubmit={this.login} />
6670
<p>This will "log you in" as this user, storing the username in the session of the API server.</p>
@@ -71,19 +75,19 @@ export default class Login extends Component {
7175
onLogin={this.onFacebookLogin}
7276
component={this.FacebookLoginButton}
7377
/>
74-
</div>}
75-
{user &&
78+
</div>
79+
)}
80+
{user && (
7681
<div>
77-
<p>
78-
You are currently logged in as {user.email}.
79-
</p>
82+
<p>You are currently logged in as {user.email}.</p>
8083

8184
<div>
8285
<button className="btn btn-danger" onClick={logout}>
8386
<i className="fa fa-sign-out" /> Log Out
8487
</button>
8588
</div>
86-
</div>}
89+
</div>
90+
)}
8791
</div>
8892
);
8993
}

src/redux/modules/auth.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,15 @@ export function load() {
160160
export function register(data) {
161161
return {
162162
types: [REGISTER, REGISTER_SUCCESS, REGISTER_FAIL],
163-
promise: ({ app }) => app.service('users').create(data).catch(catchValidation)
163+
promise: ({ app }) =>
164+
app
165+
.service('users')
166+
.create(data)
167+
.catch(catchValidation)
164168
};
165169
}
166170

167-
export function login(strategy, data, validation = true) {
171+
export function login(strategy, data) {
168172
const socketId = socket.io.engine.id;
169173
return {
170174
types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAIL],
@@ -178,7 +182,7 @@ export function login(strategy, data, validation = true) {
178182
.then(setToken({ client, app, restApp }))
179183
.then(setCookie({ app }))
180184
.then(setUser({ app, restApp }))
181-
.catch(validation ? catchValidation : error => Promise.reject(error))
185+
.catch(strategy === 'local' ? catchValidation : error => Promise.reject(error))
182186
};
183187
}
184188

0 commit comments

Comments
 (0)