-
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
1 parent
4bb78cc
commit 424e1a6
Showing
13 changed files
with
331 additions
and
155 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,39 +1,49 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import './index.css'; | ||
import App from './App'; | ||
import registerServiceWorker from './registerServiceWorker'; | ||
import {BrowserRouter} from "react-router-dom" | ||
import {Provider} from "react-redux" | ||
import {createStore, applyMiddleware, compose, combineReducers} from "redux" | ||
import thunk from "redux-thunk" | ||
|
||
import authReducer from "./store/reducers/auth" | ||
import burgerBuilderReducer from "./store/reducers/burgerBuilder" | ||
import orderReducer from "./store/reducers/order" | ||
|
||
const composeEnhancers = process.env.NODE_ENV === "development" ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ : null || compose; | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import "./index.css"; | ||
import App from "./App"; | ||
import registerServiceWorker from "./registerServiceWorker"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import { Provider } from "react-redux"; | ||
import { createStore, applyMiddleware, compose, combineReducers } from "redux"; | ||
import thunk from "redux-thunk"; | ||
import createSagaMiddelware from "redux-saga"; | ||
|
||
import { watchAuth, watchBurgerBuilder, watchOrder } from "./store/sagas/index"; | ||
import authReducer from "./store/reducers/auth"; | ||
import burgerBuilderReducer from "./store/reducers/burgerBuilder"; | ||
import orderReducer from "./store/reducers/order"; | ||
|
||
const composeEnhancers = | ||
process.env.NODE_ENV === "development" | ||
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ | ||
: null || compose; | ||
|
||
const rootReducer = combineReducers({ | ||
burgerBuilder: burgerBuilderReducer, | ||
order: orderReducer, | ||
auth: authReducer, | ||
}) | ||
burgerBuilder: burgerBuilderReducer, | ||
order: orderReducer, | ||
auth: authReducer, | ||
}); | ||
|
||
const store = createStore(rootReducer, composeEnhancers( | ||
applyMiddleware(thunk) | ||
)) | ||
const sagaMiddleware = createSagaMiddelware() | ||
|
||
const store = createStore( | ||
rootReducer, | ||
composeEnhancers(applyMiddleware(thunk, sagaMiddleware)) | ||
); | ||
|
||
sagaMiddleware.run(watchAuth) | ||
sagaMiddleware.run(watchBurgerBuilder) | ||
sagaMiddleware.run(watchOrder) | ||
|
||
const app=( | ||
<Provider store={store}> | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter> | ||
</Provider> | ||
|
||
) | ||
const app = ( | ||
<Provider store={store}> | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter> | ||
</Provider> | ||
); | ||
|
||
ReactDOM.render(app, document.getElementById('root')); | ||
ReactDOM.render(app, document.getElementById("root")); | ||
registerServiceWorker(); |
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,102 +1,64 @@ | ||
import * as actionTypes from "./actionTypes" | ||
|
||
import axios from "axios" | ||
|
||
|
||
export const authStart = () =>{ | ||
return{ | ||
type: actionTypes.AUTH_START | ||
} | ||
} | ||
import * as actionTypes from "./actionTypes"; | ||
|
||
export const authStart = () => { | ||
return { | ||
type: actionTypes.AUTH_START, | ||
}; | ||
}; | ||
|
||
export const authSuccess = (token, userId) => { | ||
|
||
return{ | ||
type: actionTypes.AUTH_SUCCESS, | ||
idToken: token, | ||
userId: userId, | ||
} | ||
} | ||
return { | ||
type: actionTypes.AUTH_SUCCESS, | ||
idToken: token, | ||
userId: userId, | ||
}; | ||
}; | ||
|
||
export const authFail = (error) => { | ||
return{ | ||
type: actionTypes.AUTH_FAIL, | ||
error: error | ||
} | ||
} | ||
return { | ||
type: actionTypes.AUTH_FAIL, | ||
error: error, | ||
}; | ||
}; | ||
|
||
export const checkAuthTimeout = (expirationTime) =>{ | ||
return dispatch =>{ | ||
setTimeout(()=>{ | ||
dispatch(logout()) | ||
}, expirationTime * 1000) | ||
} | ||
} | ||
export const checkAuthTimeout = (expirationTime) => { | ||
return { | ||
type: actionTypes.AUTH_CHECK_TIMEOUT, | ||
expirationTime: expirationTime, | ||
}; | ||
}; | ||
|
||
export const logout = ()=>{ | ||
localStorage.removeItem("token") | ||
localStorage.removeItem("expirationDate") | ||
localStorage.removeItem("userId") | ||
return{ | ||
type: actionTypes.AUTH_LOGOUT | ||
} | ||
} | ||
export const logout = () => { | ||
return { | ||
type: actionTypes.AUTH_INITIATE_LOGOUT, | ||
}; | ||
}; | ||
|
||
const key = "" | ||
export const logoutSucceed = () => { | ||
return { | ||
type: actionTypes.AUTH_LOGOUT, | ||
}; | ||
}; | ||
|
||
|
||
export const auth = (email, password, isSignUp) => { | ||
return dispatch => { | ||
dispatch(authStart()) | ||
const authData = { | ||
email: email, | ||
password: password, | ||
returnSecureToken: true, | ||
} | ||
let url = "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=" + key | ||
if(!isSignUp){ | ||
url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=" + key | ||
return { | ||
type: actionTypes.AUTH_USER, | ||
email: email, | ||
password: password, | ||
isSignUp: isSignUp, | ||
}; | ||
}; | ||
|
||
} | ||
axios.post(url, authData) | ||
.then(response=>{ | ||
console.log(response) | ||
localStorage.setItem("token", response.data.idToken) | ||
const expirationDate = new Date(new Date().getTime() + response.data.expiresIn*1000) | ||
localStorage.setItem("expirationDate" ,expirationDate) | ||
localStorage.setItem("userId", response.data.localid) | ||
dispatch(authSuccess(response.data.idToken, response.data.localId)) | ||
dispatch(checkAuthTimeout(response.data.expiresIn)) | ||
}) | ||
.catch(err => { | ||
console.log(err) | ||
dispatch(authFail(err.response.data.error)) | ||
}) | ||
} | ||
} | ||
export const setAuthRedirectPath = (path) => { | ||
return { | ||
type: actionTypes.SET_AUTH_REDIRECT_PATH, | ||
path: path, | ||
}; | ||
}; | ||
|
||
export const setAuthRedirectPath = (path) =>{ | ||
export const authCheckState = () => { | ||
return{ | ||
type: actionTypes.SET_AUTH_REDIRECT_PATH, | ||
path: path | ||
} | ||
} | ||
|
||
export const authCheckState = () =>{ | ||
return dispatch =>{ | ||
const token = localStorage.getItem("token") | ||
if(!token) dispatch(logout()) | ||
else{ | ||
const expirationDate = new Date(localStorage.getItem("expirationDate")) | ||
if(expirationDate <= new Date()){ | ||
dispatch(logout()) | ||
} | ||
else{ | ||
const userId = localStorage.getItem("userId") | ||
dispatch(authSuccess(token, userId)) | ||
dispatch(checkAuthTimeout((expirationDate.getTime() - new Date().getTime())/ 1000)) | ||
} | ||
} | ||
type: actionTypes.AUTH_CHECK_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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.