Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt auth SSR on refresh error in production but not error in develpment #1313

Closed
ridoananda opened this issue Sep 29, 2021 · 1 comment
Closed
Labels

Comments

@ridoananda
Copy link

hi, i have problem when production mode.
after successful login, it's redirect to / and then when i refresh page it's not redirect to login but render the view login and then error page.
previous view like this:
Screenshot (14)
after:
Screenshot (13)
in development mode which is work fine and no errors.
store/index.js

async nuxtServerInit({ commit }, { req }) {
    let auth = null
    console.log(req.headers.cookie)
    if (req.headers.cookie) {
        // cookie found
        try {
            // check data user login with cookie
            const { data } = await this.$axios.get('/user/me')
            // server return the data is cookie valid loggedIn is true
            auth = data // set the data auth
        } catch (err) {
            // No valid cookie found
            auth = null
        }
    }
    console.log(auth) // return the user logged in object
    this.$auth.setUser(auth)
},

login.vue

export default {
    layout: 'login',
    data() {
        return {
            errors: '',
            loginForm: {
                userName: '',
                password: '',
            },
        }
    },
    methods: {
        async login() {
            try {
                const response = await this.$auth.loginWith('local', {
                    data: this.loginForm,
                })
                const user = await this.$axios.get('/user/me')
                this.$auth.setUser(user.data)
                this.$toast.show(
                    `Login success! welcome ${this.$auth.user.fullName}`,
                    {
                        type: 'success',
                        action: {
                            text: 'Close',
                            onClick: (e, toastObject) => {
                                toastObject.goAway(0)
                            },
                        },
                        duration: 2000,
                    }
                )
                this.$router.push('/')
            } catch (e) {
                this.errors = e.response.data.errors
            }
        },
    },
}

nuxt.config.js

auth: {
  localStorage: false,
  cookie: {
      options: {
          expires: 1,
      },
  },
  strategies: {
      local: {
          token: {
              property: 'token',
              global: true,
              required: true,
              type: 'Bearer',
          },
          user: {
              property: false,
              autoFetch: false,
          },
          endpoints: {
              login: {
                  url: '/auth/login',
                  method: 'post',
                  propertyName: 'token',
              },
              logout: { url: '/auth/logout', method: 'post' },
              user: { url: '/user/me', method: 'get' },
          },
      },
  },
},

there is something wrong?
Sorry my english is bad.

@ridoananda ridoananda added the bug label Sep 29, 2021
@bmulholland
Copy link
Contributor

See #1197 for debugging tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants