priviligies

This commit is contained in:
wrzesinski-hubert 2021-01-25 18:37:47 +01:00
parent 0ecdc6db20
commit f319e022fe
2 changed files with 8 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import React, { useState, useEffect, createContext, ReactNode } from 'react';
import { LoggedUser } from '../types';
import { axiosInstance } from '../utils/axiosInstance';
import { axiosInstance, getNewTokens } from '../utils/axiosInstance';
export interface CASContext {
user: LoggedUser | undefined;
@ -42,6 +43,9 @@ export const CASProvider = ({ children }: CASProviderProps) => {
localStorage.setItem('refreshToken', user.refreshToken);
setIsFetchingToken(false);
}
else{
getNewTokens();
}
const token = localStorage.getItem('userToken');
const refreshToken = localStorage.getItem('refreshToken');
const role = localStorage.getItem('userPrivilige');

View File

@ -3,12 +3,14 @@ import axios from 'axios';
export const axiosInstance = axios.create();
//getting new tokens
const getNewTokens = async () => {
export const getNewTokens = async () => {
try {
const refreshToken = localStorage.getItem('refreshToken');
const { data } = await axiosInstance.get(
`${process.env.REACT_APP_API_URL}/token/refresh?refreshToken=${refreshToken}`,
);
console.log("12312321",data);
localStorage.setItem('userPrivilige',data.authorityRole);
localStorage.setItem('userToken', data.token);
localStorage.setItem('refreshToken', data.refreshToken);
return data.token;