diff --git a/package.json b/package.json
index e023a59..979cfef 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
"private": true,
"dependencies": {
"@rocketseat/unform": "^1.6.2",
- "axios": "^0.19.0",
+ "axios": "^0.19.2",
"date-fns": "^2.0.0-beta.2",
"date-fns-tz": "^1.0.7",
"history": "^4.9.0",
diff --git a/src/pages/SignIn/index.js b/src/pages/SignIn/index.js
index 314bab5..570d2b4 100644
--- a/src/pages/SignIn/index.js
+++ b/src/pages/SignIn/index.js
@@ -1,8 +1,11 @@
import React from 'react';
+import { useDispatch } from 'react-redux';
import { Link } from 'react-router-dom';
import { Form, Input } from '@rocketseat/unform';
import * as Yup from 'yup';
+import { signInRequest } from '~/store/modules/auth/actions';
+
import logo from '~/assets/logo.svg';
const schema = Yup.object().shape({
@@ -15,8 +18,10 @@ const schema = Yup.object().shape({
});
export default function SignIn() {
- function handleSubmit(data) {
- console.tron.log(data);
+ const dispatch = useDispatch();
+
+ function handleSubmit({ email, password }) {
+ dispatch(signInRequest(email, password));
}
return (
diff --git a/src/routes/Route.js b/src/routes/Route.js
index d9e5d51..aabe80b 100644
--- a/src/routes/Route.js
+++ b/src/routes/Route.js
@@ -5,12 +5,14 @@ import { Route, Redirect } from 'react-router-dom';
import AuthLayout from '~/pages/_layouts/auth';
import DefaultLayout from '~/pages/_layouts/default';
+import store from '~/store';
+
export default function RouteWrapper({
component: Component,
isPrivate,
...rest
}) {
- const signed = false;
+ const { signed } = store.getState().auth;
if (!signed && isPrivate) {
return ;
@@ -25,7 +27,7 @@ export default function RouteWrapper({
return (
(
+ render={props => (
diff --git a/src/services/api.js b/src/services/api.js
new file mode 100644
index 0000000..acdf576
--- /dev/null
+++ b/src/services/api.js
@@ -0,0 +1,7 @@
+import axios from 'axios';
+
+const api = axios.create({
+ baseURL: 'http://localhost:3333',
+});
+
+export default api;
diff --git a/src/store/modules/auth/actions.js b/src/store/modules/auth/actions.js
index e69de29..7ce0f1e 100644
--- a/src/store/modules/auth/actions.js
+++ b/src/store/modules/auth/actions.js
@@ -0,0 +1,19 @@
+export function signInRequest(email, password) {
+ return {
+ type: '@auth/SIGN_IN_REQUEST',
+ payload: { email, password },
+ };
+}
+
+export function signInSuccess(token, user) {
+ return {
+ type: '@auth/SIGN_IN_SUCCESS',
+ payload: { token, user },
+ };
+}
+
+export function signFailure() {
+ return {
+ type: '@auth/SIGN_FAILURE',
+ };
+}
diff --git a/src/store/modules/auth/reducer.js b/src/store/modules/auth/reducer.js
index c72a64a..059794f 100644
--- a/src/store/modules/auth/reducer.js
+++ b/src/store/modules/auth/reducer.js
@@ -1,7 +1,18 @@
-const INITIAL_STATE = {};
+import produce from 'immer';
+
+const INITIAL_STATE = {
+ token: null,
+ signed: false,
+ loading: false,
+};
export default function auth(state = INITIAL_STATE, action) {
switch (action.type) {
+ case '@auth/SIGN_IN_SUCCESS':
+ return produce(state, draft => {
+ draft.token = action.payload.token;
+ draft.signed = true;
+ });
default:
return state;
}
diff --git a/src/store/modules/auth/sagas.js b/src/store/modules/auth/sagas.js
index 9939d1c..e491d6e 100644
--- a/src/store/modules/auth/sagas.js
+++ b/src/store/modules/auth/sagas.js
@@ -1,3 +1,28 @@
-import { all } from 'redux-saga/effects';
+import { takeLatest, call, put, all } from 'redux-saga/effects';
-export default all([]);
+import history from '~/services/history';
+import api from '~/services/api';
+
+import { signInSuccess } from './actions';
+
+export function* signIn({ payload }) {
+ const { email, password } = payload;
+
+ const response = yield call(api.post, 'sessions', {
+ email,
+ password,
+ });
+
+ const { token, user } = response.data;
+
+ if (!user.provider) {
+ console.tron.error('O usuário não é prestador de serviço.');
+ return;
+ }
+
+ yield put(signInSuccess(token, user));
+
+ history.push('/dashboard');
+}
+
+export default all([takeLatest('@auth/SIGN_IN_REQUEST', signIn)]);
diff --git a/yarn.lock b/yarn.lock
index 44078b2..e3ccccb 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2112,7 +2112,7 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.9.1.tgz#7e33d8f7d449b3f673cd72deb9abdc552dbe528e"
integrity sha512-wMHVg2EOHaMRxbzgFJ9gtjOOCrI80OHLG14rxi28XwOW8ux6IiEbRCGGGqCtdAIg4FQCbW20k9RsT4y3gJlFug==
-axios@^0.19.0:
+axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==