gpt4 book ai didi

android - 如何在 login.vue 中添加标题?

转载 作者:行者123 更新时间:2023-12-04 14:15:09 24 4
gpt4 key购买 nike

如何更新 apolloProvider 的 header ?

请查看 nativescript-vue 应用程序仓库:

https://github.com/kaanguru/vue-apollo-login

我无法正确解释,所以请查看应用程序。 我不知道如何更新 appolloClient header 。

App repo 有它自己的评论和指令。它很容易安装并由您自己查看。

当前代码结构:

Post 请求提交用户的标识符和密码凭据以进行身份​​验证和 获取 token 在登录页面。

Apollo 需要将 jwt token 放入 Authorization header 中。

  • Main.js:如果有 JWT 以 headers 开头,则启动 apollo 客户端
  • 如果没有 JWT 则转到登录
  • 如果有 JWT,请转到鸟类列表
  • 登录:从服务器获取 jwt 并将其写入本地存储
  • 转到鸟类列表(不显示数据,因为主 js 中已启动 apollo)

  • structure


    import ApolloClient from 'apollo-boost'
    import VueApollo from 'vue-apollo'

    Vue.use(VueApollo)

    const apolloClient = new ApolloClient({
    uri: 'http://sebapi.com/graphql',

    // HEADERS WORK FINE IF TOKEN WAS IN MAIN
    // headers: {
    // authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTg2MzU2NzM2LCJleHAiOjE1ODg5NDg3MzZ9.wpyhPTWuqxrDgezDXJqIOaAIaocpM8Ehd3BhQUWKK5Q`,
    // }

    })
    const apolloProvider = new VueApollo({
    defaultClient: apolloClient,
    })


    LOGIN.VUE



    .then(
    (response) => {
    const result = response.content.toJSON();
    console.log("Result from Server: ", result);
    const token = result.jwt;

    // HOW TO ADD HEADERS TO APOLLOCLIENT this.$apollo.provider.defaultClient

    // this.$apollo.provider.defaultClient({
    // request: (operation) => {
    // operation.setContext({
    // headers: {
    // authorization: `Bearer ${result.jwt}` ,
    // },
    // });
    // },
    // });

    },

    感谢您的关注。

    注意 : 请评论了解更多详情。 sebapi.com 后端是一个strapi graphql 服务器。

    相关文档:

    Apollo authentication

    Apollo link composition

    Vue apolloProvider Usage

    最佳答案

    问题是您需要使用 ApolloLink 才能将其用于所有请求。你使用的方式是行不通的。

    您必须使用中间件 apollo-link-context为达到这个。

    根据 Apollo-link-context docs

    apollo-link-context: Used to set a context on your operation, which is used by other links further down the chain.

    The setContext function takes a function that returns either an object or a promise that returns an object to set the new context of a request. Add the below code to app.js and modify some changes and check.


    import { setContext } from 'apollo-link-context'

    const authLink = setContext((_, { headers }) => {
    // get the authentication token from ApplicationSettings if it exists
    const token = ApplicationSettings.getString("token");

    // return the headers to the context so HTTP link can read them
    return {
    headers: {
    ...headers,
    authorization: token ? `Bearer ${token}` : null
    }
    }
    })

    // update apollo client as below
    const apolloClient = new ApolloClient({
    link: authLink.concat(httpLink),
    cache: new InMemoryCache() // If you want to use then
    })


    Login.vue 的变化
    .then(
    (response) => {
    const result = response.content.toJSON();
    console.log("Result from Server: ", result);
    const token = result.jwt;
    // Set token using setString
    ApplicationSettings.setString("token", result.jwt);
    },

    希望这可以帮助!

    关于android - 如何在 login.vue 中添加标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915530/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com