Giter Club home page Giter Club logo

Comments (12)

DaviTeodoro avatar DaviTeodoro commented on May 29, 2024 4

A proper place to authenticate the user is at 'layouts'. You may do:

random-layout.vue

computed: {
    ...mapState('auth', ['payload']),
  },

  methods: {
    ...mapActions({
      authenticate: 'auth/authenticate',
      logout: 'auth/logout',
    }),
  },

  async created() {
    try {
      if (this.payload) await this.authenticate();
    } catch (e) {
      this.logout();
      throw e;
    }
  },

from feathers-vuex.

samburgers avatar samburgers commented on May 29, 2024 4

I'm doing it in Nuxt middleware for my SPA setup:

// middleware/authenticated.js

export default function({ store }) {
  store
    .dispatch("auth/authenticate")
    .then(result => {
      console.log(result);
    })
    .catch(error => {
      console.error(error);
    });
}
// nuxt.config.js

module.exports = {
  mode: "spa",
  router: {
    middleware: ["authenticated"]
  }
}

from feathers-vuex.

dw72 avatar dw72 commented on May 29, 2024 3

I have in store actions:

import feathersVuex from 'feathers-vuex'
import feathersClient from '../services/feathers-client'
import { initAuth } from 'feathers-vuex/lib/utils'

const { service, auth } = feathersVuex(feathersClient, { idField: '_id' })

...

export const actions = {
  nuxtServerInit({ commit }, { req }) {
    return initAuth({
      commit,
      req,
      moduleName: 'auth',
      cookieName: 'feathers-jwt'
    })
  }

in feathers-client.js

import feathers from '@feathersjs/client'
import io from 'socket.io-client'
import { CookieStorage } from 'cookie-storage'
import { host, port } from '../../config/default.json'

const socket = io(`http://${host}:${port}`, { transports: ['websocket'] })

const feathersClient = feathers()
  .configure(feathers.socketio(socket))
  .configure(feathers.authentication({ storage: new CookieStorage() }))

export default feathersClient

and then in mounted() of page

...
async mounted() {
    await this.authenticate()
    this.findUsers()
},
...

If you run this.authenticate() without params then it try to load token from cookie... and it works for me, but I suppose that there must be a better place to call this function, not all pages separately. BTW findUsers is a mapped find action from service using ...mapActions('users', { findUsers: 'find' }), in component (page here) methods.

from feathers-vuex.

marshallswain avatar marshallswain commented on May 29, 2024 3

I've done it using middleware, as shown in the previous comment, but I've also seen it dispatched after the initAuth utility:

  nuxtServerInit({ commit }, { req }) {
    return initAuth({
      commit,
      req,
      moduleName: 'auth',
      cookieName: 'feathers-jwt'
    })
      .then(() => {
        // dispatch here
      })
  }

from feathers-vuex.

Alfredo-Delgado avatar Alfredo-Delgado commented on May 29, 2024 3

Unless I'm missing something, dispatching from initAuth inside of nuxtServerInit doesn't authenticate the front-end client. Any authenticated endpoints that don't get called on the server fail.

I arrived at the same conclusion as @DaviTeodoro, but it seemed unnatural and the lack of documentation calling out what I think should be a common issue led me to believe that I must be missing something.

from feathers-vuex.

NickBolles avatar NickBolles commented on May 29, 2024 3

nuxtClientInit is a good solution for this. I just made an initauth store action that i dispatch in both nuxtServerInit and nuxtClientInit

Otherwise persisting the state like in #93 is the way to go. I'm not sure that works with all of nuxt's modes though

from feathers-vuex.

daenuprobst avatar daenuprobst commented on May 29, 2024

Could you elaborate on this? For me authentication is currently not working at all. Log-in works, the jwt token and payload are stored in the state (as seen with vue debugger for chrome), but I only get NotAuthenticated: No auth token upon requesting protected resources...

from feathers-vuex.

dw72 avatar dw72 commented on May 29, 2024

@DaviTeodoro i try yours example but if do things this way then authentication itself is working, but findUsers (find action mapped from store) on my page is reporting 'No auth token'... I think is called before auth done.

from feathers-vuex.

marshallswain avatar marshallswain commented on May 29, 2024

I'll close this after this gets added to the docs.

from feathers-vuex.

YuanTian1991 avatar YuanTian1991 commented on May 29, 2024

Currently, I have successfully run auth in middleware in SPA mode, like @samburgers , with auth function in store/index.js. However, after changing it into universal mode, it's not working... Then I have to use @DaviTeodoro 's solution.

But I want to do it in middleware even in universal mode.

from feathers-vuex.

marshallswain avatar marshallswain commented on May 29, 2024

@NickBolles thank you for your PR, which I've just merged. I'd really appreciate if you could update the guide, here: https://github.com/feathers-plus/docs/blob/master/source/v1/feathers-vuex/guide.md.

Once done, I will close all issues around auth and Nuxt. Thank you so much for your contributions!

from feathers-vuex.

marshallswain avatar marshallswain commented on May 29, 2024

I'm closing this now that we have a full Nuxt example in the docs. Thanks!

from feathers-vuex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.