import {useState, useCallback, useEffect} from 'react'
const storageName = "userDat"

export const useAutorization = () =>{
    const [token, setToken]=useState(null)
    const [userId,setUserId]=useState(null)
    const logIn = useCallback((jwtToken,id)=>{
        setToken(jwtToken)
        setUserId(id)
        localStorage.setItem(storageName,JSON.stringify({userId: id,token: jwtToken}))
    },[])
    const logOut = useCallback(()=>{
        setToken(null)
        setUserId(null)
        localStorage.removeItem(storageName)
    },[])
    useEffect(()=>{
        const data = JSON.parse(localStorage.getItem(storageName))
        if(data && data.token){
            logIn(data.token,data.userId)
        }
    },[logIn])
    return(logIn,logOut,token,userId)
}