diff --git a/src/context/auth.js b/src/context/auth.js index e263d89..1d5fb19 100644 --- a/src/context/auth.js +++ b/src/context/auth.js @@ -1,20 +1,5 @@ import { createContext, useContext, useEffect, useState } from 'react'; -import { sleep } from '../utils/sleep'; - -const getUser = shouldFail => - sleep(3000).then(() => { - if (shouldFail) { - return { message: 'Falha na autenticação' }; - } else { - return { - id: '0021564', - username: 'leonardomurca', - name: 'Leonardo', - lastName: 'Murça', - token: 'skdfb9458hnsdfsif4w38r9234ry98423', - }; - } - }); +import { getUser } from '../services/user-service'; const AuthContext = createContext(); @@ -43,7 +28,6 @@ function AuthProvider(props) { if (shouldFail) { return setState({ status: 'error', user: null, error: data }); } else { - window.localStorage.setItem('$USER', JSON.stringify(data)); return setState({ status: 'success', user: data, error: null }); } }); diff --git a/src/services/mocks.js b/src/services/mocks.js index 5010121..5d18cf1 100644 --- a/src/services/mocks.js +++ b/src/services/mocks.js @@ -166,4 +166,16 @@ const faq = [ }, ]; -export { allClassrooms, allAssignments, faq }; +const user = { + id: '0021564', + username: 'leonardomurca', + name: 'Leonardo', + lastName: 'Murça', + token: 'skdfb9458hnsdfsif4w38r9234ry98423', +}; + +const authFailure = { + message: 'Falha na autenticação', +}; + +export { allClassrooms, allAssignments, faq, user, authFailure }; diff --git a/src/services/user-service.js b/src/services/user-service.js index bae7c73..4dcc3f8 100644 --- a/src/services/user-service.js +++ b/src/services/user-service.js @@ -1,5 +1,5 @@ import { sleep } from '../utils/sleep'; -import { allClassrooms, allAssignments, faq } from './mocks'; +import { allClassrooms, allAssignments, faq, user, authFailure } from './mocks'; const getClassrooms = userId => sleep(3000).then(() => { @@ -33,4 +33,14 @@ const getFaq = () => }; }); -export { getClassrooms, getClassroomById, getAssignments, getFaq }; +const getUser = shouldFail => + sleep(3000).then(() => { + if (shouldFail) { + return authFailure; + } else { + window.localStorage.setItem('$USER', JSON.stringify(user)); + return user; + } + }); + +export { getClassrooms, getClassroomById, getAssignments, getFaq, getUser };