Creating User Logins and API Authentication

In the last two chapters, we started working on session and JSON Web Token (JWT) authentication in Nuxt apps. We used sessions for authentication in Chapter 10, Adding a Vuex Store, to exercise nuxtServerInit. Then we used sessions and tokens together for authentication in Chapter 11, Writing Route Middlewares and Server Middlewares, to exercise per-route middleware, for example:

// store/index.jsnuxtServerInit({ commit }, { req }) {  if (req.ctx.session && req.ctx.session.authUser) {    commit('setUser', req.ctx.session.authUser)  }}// middleware/token.jsexport default async ({ store, error }) => {  if (!store.state.auth.token) {    // handle error  } axios.defaults.headers.common['Authorization'] = Bearer: ...

Get Hands-on Nuxt.js Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.