Как сохранить состояние пользователя при обновлении страницы sh в nuxt js - PullRequest
0 голосов
/ 26 мая 2020
• 1000 значения из магазина по refre sh.

Я попытался использовать промежуточное программное обеспечение для получения данных из локальной обработки, но это невозможно, поскольку промежуточное программное обеспечение работает на стороне сервера, пытался установить данные пользователя в файлах cookie, но не смог установить файлы cookie.

может кто-нибудь предложить лучший подход к поддержанию состояния пользователя на странице refre sh.

userLogin() {
      try {
        this.$axios.post('api/auth/Signin', this.login).then((res) => {
          this.$auth.setToken('local', 'Bearer ' + res.data.data.token)

          this.$axios.setHeader(
            'Authorization',
            'Bearer ' + res.data.data.token
          )
          this.$auth.ctx.app.$axios.setHeader(
            'Authorization',
            'Bearer ' + res.data.data.token
          )
          this.$axios.get('api/auth/User').then((res) => {
            this.$auth.setUser(res.data.user)
            this.$cookies.set('user', res.data.user, {
              path: '/',
              maxAge: 60 * 60 * 24 * 7
            })            
            this.$router.push('/')
          })
        })

      } catch (err) {
        console.log(err)
      }
    }

// middleware function 
export default function({ req, $auth, redirect, store }) {
  // redirect to login page if the user is not authenticated
  if (!$auth.isloggedIn) {
    if (!process.client) {
      const cookie = require('cookie')
      const cookies = req.headers.cookie
      const parsedCookies = cookie.parse(cookies)
      const token = parsedCookies['auth._token.local']
      const user = parsedCookies.user

      if (!token) return redirect('/login')
      else {
        store.dispatch('loginStatus', true)
        store.dispatch('setUser', user)
      }
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...