import { useState } from 'react';

export default function useToken() {
    const getToken = () => {
        const tokenString = localStorage.getItem('token');
        // @ts-ignore
        const userToken = JSON.parse(tokenString);
        return userToken?.token
    };

    const [token, setToken] = useState(getToken());

    // @ts-ignore
    const saveToken = userToken => {
        localStorage.setItem('token', JSON.stringify(userToken));
        setToken(userToken.token);
    };

    const deleteToken = () => {
        localStorage.removeItem('token');
    }

    return {
        setToken: saveToken,
        deleteToken: deleteToken,
        token
    }
}
