Auth basico completado
This commit is contained in:
@@ -1,64 +1,52 @@
|
||||
import React, {useState} from 'react';
|
||||
import React from 'react';
|
||||
import {Redirect} from "react-router-dom";
|
||||
import queryString from "query-string";
|
||||
|
||||
import {get_auth, obtain_code} from "../services/auth_service";
|
||||
import {auth, logout} from "../services/auth_service";
|
||||
import {useStateValue} from "../services/State";
|
||||
|
||||
export const AuthCallback = (props) => {
|
||||
return null;
|
||||
}
|
||||
|
||||
export const AuthLogin = (props) => {
|
||||
const [response, setResponse] = useState(null);
|
||||
const [context, dispatch] = useStateValue();
|
||||
|
||||
if (window.localStorage.getItem('refresh_token')) {
|
||||
if (context.user.auth) {
|
||||
// El usuario ya esta logeado, no hay nada que hacer
|
||||
props.history.push('/')
|
||||
}
|
||||
|
||||
if (!response) {
|
||||
if (context.user.auth) {
|
||||
return <Redirect to='/'/>
|
||||
}
|
||||
const params = queryString.parse(props.location.search);
|
||||
auth(params).then((response) => {
|
||||
console.debug(response, window.localStorage);
|
||||
|
||||
if (!window.localStorage.getItem('code_verifier')) {
|
||||
const {redirect, code_verifier} = obtain_code();
|
||||
window.localStorage.setItem('code_verifier', code_verifier);
|
||||
window.location.href = redirect;
|
||||
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')
|
||||
}
|
||||
})
|
||||
|
||||
const parsedParams = queryString.parse(props.location.search);
|
||||
if (parsedParams.error) {
|
||||
console.log(parsedParams.error);
|
||||
window.localStorage.removeItem('code_verifier');
|
||||
return <Redirect to={'/error'}/>
|
||||
}
|
||||
|
||||
const code = parsedParams.code;
|
||||
const code_verifier = window.localStorage.getItem('code_verifier');
|
||||
get_auth(code, code_verifier).then((response) => setResponse(response));
|
||||
|
||||
return null;
|
||||
} else {
|
||||
console.log(response);
|
||||
|
||||
window.localStorage.removeItem('code_verifier');
|
||||
|
||||
const refresh = response.refresh_token;
|
||||
const expires = new Date(new Date().getTime() + ((response.expires_in) * 1000))
|
||||
|
||||
window.localStorage.setItem('refresh_token', refresh);
|
||||
window.localStorage.setItem('expires', expires);
|
||||
|
||||
dispatch({type: 'login', user: {auth: true, access_token: response.access_token}});
|
||||
|
||||
return <Redirect to={'/'}/>
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export const AuthLogout = (props) => {
|
||||
const [context, dispatch] = useStateValue();
|
||||
dispatch({action: 'logout', user: {auth: false}})
|
||||
if (!context.user.auth) {
|
||||
return <Redirect to='/'/>
|
||||
}
|
||||
|
||||
logout(context.user.access_token).then(result => {
|
||||
console.log(result);
|
||||
});
|
||||
|
||||
dispatch({type: 'logout', user: {auth: false}})
|
||||
return <Redirect to='/'/>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user