-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
432 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import React from 'react'; | ||
import api from '~/services/api'; | ||
|
||
// import { Container } from './styles'; | ||
|
||
export default function Dashboard() { | ||
api.get('appointments'); | ||
return <h1>Dashboard</h1>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,69 @@ | ||
import { takeLatest, call, put, all } from 'redux-saga/effects'; | ||
import { toast } from 'react-toastify'; | ||
|
||
import history from '~/services/history'; | ||
import api from '~/services/api'; | ||
import history from '~/services/history'; | ||
|
||
import { signInSuccess } from './actions'; | ||
import { signInSuccess, signFailure } from './actions'; | ||
|
||
export function* signIn({ payload }) { | ||
const { email, password } = payload; | ||
try { | ||
const { email, password } = payload; | ||
|
||
const response = yield call(api.post, 'sessions', { | ||
email, | ||
password, | ||
}); | ||
|
||
const { token, user } = response.data; | ||
|
||
if (!user.provider) { | ||
toast.error('O usuário não é prestador de serviço.'); | ||
return; | ||
} | ||
|
||
api.defaults.headers.Authorization = `Bearer ${token}`; | ||
|
||
yield put(signInSuccess(token, user)); | ||
|
||
const response = yield call(api.post, 'sessions', { | ||
email, | ||
password, | ||
}); | ||
history.push('/dashboard'); | ||
} catch (err) { | ||
toast.error('Falha na autenticação, verifique seus dados de acesso.'); | ||
yield put(signFailure()); | ||
} | ||
} | ||
|
||
export function* signUp({ payload }) { | ||
try { | ||
const { name, email, password } = payload; | ||
|
||
yield call(api.post, 'users', { | ||
name, | ||
email, | ||
password, | ||
provider: true, | ||
}); | ||
|
||
const { token, user } = response.data; | ||
history.push('/'); | ||
} catch (err) { | ||
toast.error('Falha no cadastro, verifique seus dados!'); | ||
|
||
if (!user.provider) { | ||
console.tron.error('O usuário não é prestador de serviço.'); | ||
return; | ||
yield put(signFailure()); | ||
} | ||
} | ||
|
||
export function setToken({ payload }) { | ||
if (!payload) return; | ||
|
||
yield put(signInSuccess(token, user)); | ||
const { token } = payload.auth; | ||
|
||
history.push('/dashboard'); | ||
if (token) { | ||
api.defaults.headers.Authorization = `Bearer ${token}`; | ||
} | ||
} | ||
|
||
export default all([takeLatest('@auth/SIGN_IN_REQUEST', signIn)]); | ||
export default all([ | ||
takeLatest('persist/REHYDRATE', setToken), | ||
takeLatest('@auth/SIGN_IN_REQUEST', signIn), | ||
takeLatest('@auth/SIGN_UP_REQUEST', signUp), | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { combineReducers } from 'redux'; | ||
|
||
import auth from './auth/reducer'; | ||
import user from './user/reducer'; | ||
|
||
export default combineReducers({ | ||
auth, | ||
user, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { all } from 'redux-saga/effects'; | ||
|
||
import auth from './auth/sagas'; | ||
import user from './user/sagas'; | ||
|
||
export default function* rootSaga() { | ||
return yield all([auth]); | ||
return yield all([auth, user]); | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import produce from 'immer'; | ||
|
||
const INITIAL_STATE = { | ||
profile: null, | ||
}; | ||
|
||
export default function user(state = INITIAL_STATE, action) { | ||
switch (action.type) { | ||
case '@auth/SIGN_IN_SUCCESS': | ||
return produce(state, draft => { | ||
draft.profile = action.payload.user; | ||
}); | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { all } from 'redux-saga/effects'; | ||
|
||
export default all([]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import storage from 'redux-persist/lib/storage'; | ||
import { persistReducer } from 'redux-persist'; | ||
|
||
export default reducers => { | ||
const persistedReducer = persistReducer( | ||
{ | ||
key: 'gobarber', | ||
storage, | ||
whitelist: ['auth', 'user'], | ||
}, | ||
reducers | ||
); | ||
|
||
return persistedReducer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.