M
M
maxemga2021-04-25 16:05:23
React
maxemga, 2021-04-25 16:05:23

How to implement the display of the name in the personal account?

I have an application with registration, login and personal account. When I login, I want the page to display the name of the current account and I did it
. When I login, I execute a function that sends the name to the custom hook, take the name in App.js and, using the context, send the name to the component I need so that there display it:

const Login = async (values) => {
        try {
            const data = await request('/api/auth/login', 'POST', values)
            console.log(data)

            auth.login(data.token, data.userId, data.name)
        }
        catch(e) {
            console.log("Не удалось войти")
        }
    }

Everything works, but when the page is reloaded, the name is reset (and this is logical, because the name is taken only when logging in, and nothing is taken when reloading). And the question is, how can I do this correctly and so that the Name is saved when rebooting?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Matyushkin, 2021-04-25
@maxemga

Well, for example:
We send some data (which the user fills in) to the server. If the data is correct, then the server gives us a personal token for the user. Having received this token, the user becomes authorized. We put this token in the local storage. Now we can check every reboot of the site: if there is a token, then we make a request to the server to get information about the user. If there is no token, then the user is not authorized.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question