diff --git a/src/components/Auth.jsx b/src/components/Auth.jsx new file mode 100644 index 0000000..f9b4958 --- /dev/null +++ b/src/components/Auth.jsx @@ -0,0 +1,52 @@ +import React from 'react'; +import {Redirect} from "react-router-dom"; +import queryString from "query-string"; + +import {auth, logout} from "../services/auth_service"; +import {useStateValue} from "../services/State"; + +export const AuthLogin = (props) => { + const [context, dispatch] = useStateValue(); + + if (context.user.auth) { + // El usuario ya esta logeado, no hay nada que hacer + props.history.push('/') + } + + const params = queryString.parse(props.location.search); + auth(params).then((response) => { + console.debug(response, window.localStorage); + + if(response.status === 'redirect_to_code'){ + // Se va o ya se redirigió hacia la pagina que obtiene el código, no hay nada que hacer. + return null; + } else if (response.status === 'code_error') { + // Hubo un error al obtener el código + props.history.push('/error') + } else if (response.status === 'done') { + // Termino gud, refresh_token y expires debería estar en local storage y access_token + // debió ser retornado con la response + props.history.push('/') + dispatch({type: 'login', user:{auth: true, access_token: response.access_token}}); + } else { + // Ocurrió un error inesperado al hacer auth :c + props.history.push('/error') + } + }) + + return null; +} + +export const AuthLogout = (props) => { + const [context, dispatch] = useStateValue(); + if (!context.user.auth) { + return + } + + logout(context.user.access_token).then(result => { + console.log(result); + }); + + dispatch({type: 'logout', user: {auth: false}}) + return +} diff --git a/src/components/Nav.jsx b/src/components/Nav.jsx index 283b0e0..224072c 100644 --- a/src/components/Nav.jsx +++ b/src/components/Nav.jsx @@ -1,13 +1,41 @@ import React from "react"; import {Link} from "react-router-dom"; import './Nav.scss'; +import {useStateValue} from '../services/State' -export const Nav = () => ( - -) +export const Nav = (props) => { + const context = useStateValue()[0]; + + const showLogin = () => { + return context.user.auth === false; + } + + const buttons = () => { + if (showLogin()) { + return + }else { + return + } + } + + return ( + + ) +} diff --git a/src/index.jsx b/src/index.jsx index cc17ff4..0fd8d65 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -6,6 +6,8 @@ import './styles/reset.css'; import './styles/main.scss'; import './styles/tabs.scss'; +import {StateProvider} from "./services/State"; + import {Nav} from "./components/Nav"; import {SearchBar} from "./components/SearchBar"; import {ScrollToTopRouter} from "./components/ScrollToTop"; @@ -17,27 +19,28 @@ import {ReleaseView} from "./views/Release"; import {Recomended} from "./views/Recomended"; import {SongView} from "./views/Song"; import {Grid, RowCol} from './components/Grid'; +import {AuthLogin, AuthLogout} from "./components/Auth"; const Main = (props) => { const navigate = (query) => props.history.push(`/search?query=${query}`); return ( - -

Busca la musica que te gusta!

- - -
+ +

Busca la musica que te gusta!

+ + +
) } const NoRoute = (props) => { return ( - -

Esa pagina no existe

- - - -
+ +

Esa pagina no existe

+ + + +
); } @@ -48,10 +51,15 @@ const App = () => (