首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSRF令牌和Nuxt-auth

CSRF令牌和Nuxt-auth
EN

Stack Overflow用户
提问于 2021-12-27 16:01:58
回答 1查看 1.7K关注 0票数 2

现在,我正在尝试使用诺克斯奥斯编写登录功能。

我有一个FastAPI服务器,它被设置为可以使用HTTPOnly cookie,因此它需要一个csrf令牌来将用户抛给我的客户端。我无法处理令牌,因为它是HTTPOnly,所以没有LocalStorage

登录很好,但我无法设法获得存储的用户。在向我的/login端点发出请求之后,Nuxt还在/me端点上请求一个用户。但我得到了401的回复

缺失cookie access_token_cookie

/me上出错。我不知道怎么处理。

我的登录请求方法

代码语言:javascript
复制
async userLogin() {
  await this.$auth.loginWith('cookie', {
    data: `grant_type=&username=${this.emailInput}&password=${this.passwordInput}&scope=&client_id=&client_secret=&`,
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  })
  await this.$router.push('/account')
}

我读到过nuxt不擅长曲奇模式,但这篇文章是2018年发布的,我们现在有了一个“cookie”策略。那么,是否有更好的解决办法是手动处理身份验证?

我的auth密钥( nuxt.config.js )

代码语言:javascript
复制
auth: {
  strategies: {
    cookie: {
      endpoints: {
        login: {
          url: "/api/v1/login/login",
          method: "post",
          withCredentials: true
        },
        logout: { url: "/api/v1/login/logout", method: "post" },
        user: {
          url: "/api/v1/users/me",
          method: "get"
        }
      },
      tokenType: "bearer"
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-12-28 08:53:53

我有一个基于Nuxt + Django的基于http的cookie安装程序。

我的Nuxt应用程序反向代理API请求到后端。因此,它可以在服务器端读取cookie。

因此,我创建了auth-ssr.ts中间件来检查用户loggedIn。

代码语言:javascript
复制
import { Context, Middleware } from '@nuxt/types'
import { parse as parseCookie } from 'cookie' // this is lib https://github.com/jshttp/cookie

/**
 * This middleware is needed when running with SSR
 * it checks if the token in cookie is set and injects it into the nuxtjs/auth module
 * otherwise it will redirect to login
 * @param context
 */
const authMiddleware: Middleware = async (context: Context) => {
  if (process.server && context.req.headers.cookie != null) {
    const cookies = parseCookie(context.req.headers.cookie)
    const token = cookies['session'] || '' // here your cookie name
    if (token) {
      context.$auth.$state.loggedIn = true
    }
  }
}

export default authMiddleware

在这里我的nuxt.config.js

代码语言:javascript
复制
  auth: {
    strategies: {
      cookie: {
        user: {
          property: 'user',
        },
        endpoints: {
          login: {
            url: '/api/v2/auth/login/',
            method: 'post',
          },
          user: {
            url: '/api/v2/auth/user/',  
            method: 'get',
          },
          logout: {
            url: '/api/v2/auth/logout/',
            method: 'post',
          },
        },
      },
    },
    redirect: {
      login: '/login',
    },
    plugins: ['@plugins/axios.ts'],
  },
  
  router: {
    middleware: ['auth-ssr', 'auth'],
  },
  
  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    proxy: true,
  },

  proxy: {
    '/api': {
      target: 'https://backend.com/',
    },
  },
  
  ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70497746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档