When i add Authorization in to headers outside from my function setToken()
anywhere from the app, then it work, but if i do this, i do it like manually just for test, cuze i dont know how to get my token outside the setToken()
const tokentest =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwiaWF0IjoxNjEwMzE3NDcwLCJleHAiOjE2OTY2MzEwNzB9.TikP-RQknI5YMVZmirTH69agd2P0zsW5jIHFcK27kO8';
api.defaults.headers.Authorization = `Bearer ${tokentest}`;
now the problem is not more send the authorization, now i dont know how to get my token outside the setToken function that using a actions thats i generated by the react-perssit library, so here my code
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 { signInSuccess, signFailure } from './actions';
// const test = api.defaults.headers.Authorization;
// console.log(test);
//here it work, but how can i get my token from here?
//why just working here and not inside my fucntion?
const tokentest =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwiaWF0IjoxNjEwMzE3NDcwLCJleHAiOjE2OTY2MzEwNzB9.TikP-RQknI5YMVZmirTH69agd2P0zsW5jIHFcK27kO8';
api.defaults.headers.Authorization = `Bearer ${tokentest}`;
export function* signIn({ payload }) {
try {
const { email, password } = payload;
const response = yield call(api.post, 'session', {
email,
password,
});
const { token, user } = response.data;
if (!user.provider) {
toast.error('usario no es porveedor');
}
//but here doe'snt work nothing when i do this
api.defaults.headers.Authorization = `Bearer ${token}`;
yield put(signInSuccess(token, user));
history.push('/admin');
} catch (err) {
toast.error('error de autenticacion, verifique sus datos');
yield put(signFailure());
}
}
export function* signUp({ payload }) {
try {
const { name, email, password, whatsapp, repeatPassword } = payload;
yield call(api.post, 'users', {
name,
email,
password,
whatsapp,
repeatPassword,
provider: true,
});
history.push('/sesion');
} catch (err) {
toast.error('error al registarse');
yield put(signFailure());
}
}
//here not working too
export function setToken({ payload }) {
if (!payload) return;
const { token } = payload.auth;
console.log(token);
if (token) {
api.defaults.headers.Authorization = `Bearer ${token}`;
}
}
export default all([
takeLatest('@auth/persist/REHYDRATE', setToken),
takeLatest('@auth/SIGN_IN_REQUEST', signIn),
takeLatest('@auth/SIGN_UP_REQUEST', signUp),
]);
why just working here and not inside my fucntion?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…