小编Jar*_*red的帖子

如何将 Axios 拦截器与 Next-Auth 结合使用

我正在将我的CRA应用程序转换为拦截器模式Nextjs并遇到一些问题Axios

它有效,但我被迫创建一个Axios实例并将其传递给每个 api 调用。

有一个更好的方法吗?

这是我现在所拥有的:

Profile.js:

import { useSession } from 'next-auth/react'

function Profile(props) {
    const { data: session } = useSession()
    const [user, setUser] = useState()
    useEffect(()=> {
      const proc= async ()=> {
        const user = await getUser(session?.user?.userId)
        setUser(user)
      }
      proc()
    },[])
    
    return <div> Hello {user.userName}<div>
}

Run Code Online (Sandbox Code Playgroud)

getUser.js:


export default async function getUser(userId) {
    const axiosInstance = useAxios()
    const url = apiBase + `/user/${userId}`
    const { data } …
Run Code Online (Sandbox Code Playgroud)

reactjs axios next.js react-query next-auth

6
推荐指数
1
解决办法
2万
查看次数

Nextjs:如何将 jwt 令牌附加到每个 API 调用的标头

我正在尝试将我的应用程序移植CRANextJs,但有一件事阻止了我:

我必须将身份验证令牌标头附加到数百个 API 调用中的每一个调用中。

我的身份验证流程非常简单,我使用/authenticate用户名和密码调用我们的 Express 服务器 API,它返回一个 JWT 令牌。然后,我需要将该令牌与标头中的所有连续 API 调用一起传递。

我需要一种方法将其附加到中间件或其他东西中,这样我就不必手动将其添加到每个 api 调用中。

在我的 CRA 应用程序中,我axios为此使用了拦截器。

这是我的设置:( 但NextJs 13使用pages,而不是/app dir

Iron-Session版本6

axios最新的

我在我的middleware.ts

import { getIronSession } from 'iron-session'
import { sessionOptions } from './lib/session'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export async function middleware(request: NextRequest) {
    // Clone the request headers and …
Run Code Online (Sandbox Code Playgroud)

authentication jwt axios next.js

6
推荐指数
0
解决办法
2108
查看次数

React-Native Expo 应用程序 Auth0 登录,AuthSession - authURL 失败

错误截图我正在尝试使用 AuthSession 设置登录到我的 React Native Expo 应用程序。

我不断收到此错误:ValidationError:子“authUrl”失败,因为 [“authUrl”必须是有效的 uri]”。

奇怪的是,如果我通过控制台记录正在传递的 URI,复制它并将其粘贴到浏览器中,它就可以工作!(按预期拉出 Auth0 登录屏幕)。

为什么我在尝试从 Expo 客户端应用程序调用它时会收到此错误?

这是我的代码:

 function toQueryString(params) {
  return '?' + Object.entries(params)
    .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
    .join('&');
}

class SignInScreen extends React.Component {

logIn = async () => {
  const redirectUrl = AuthSession.getRedirectUrl();
  console.log(`Redirect URL: ${redirectUrl}`);

  // Structure the auth parameters and URL
  const queryParams = toQueryString({
    client_id: auth0ClientId,
    redirect_uri: redirectUrl,
    response_type: 'id_token', // id_token will return a JWT token
    scope: 'openid profile', // retrieve the user's …
Run Code Online (Sandbox Code Playgroud)

authentication auth0 react-native expo

4
推荐指数
1
解决办法
2386
查看次数

如何使用 React-Hook-Form Autosave 进行 React-Query

我正在尝试使用 React-Hook-Form 和 React-Query 制作一个表单,只要用户更改任何字段(去抖),它就会自动保存。我已经很接近了,但是当我变异时它会产生无限循环。这是我所拥有的:

"@tanstack/react-query": "^4.2.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.34.2",
Run Code Online (Sandbox Code Playgroud)
import React from 'react'
import { useMutation, useQuery } from '@tanstack/react-query'
import { useForm } from 'react-hook-form'
import { yupResolver } from '@hookform/resolvers/yup'
import * as Yup from 'yup'
import debounce from 'just-debounce-it'

type NameType = {
    id: number
    firstName: string
    lastName: string
}

const schemaValidation = Yup.object().shape({
    id: Yup.number().required('Required'),
    firstName: Yup.string().required('Required'),
    lastName: Yup.string()
        .min(2, 'Must be greater than 1 character')
        .max(50, 'Must be less than 50 …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-hook-form react-query

4
推荐指数
1
解决办法
4316
查看次数

如何更改 Typescript 类实例中的值?

我有一个水果课:

export class Fruit {
    constructor(public id: number, public name: string) {}

    public changeName(_name: string): void {
        console.log('changing name')
        this.name = _name
    }
}
Run Code Online (Sandbox Code Playgroud)

我像这样实现它:

import React from 'react'
import { Fruit } from '../classes/fruit'

const HomePage = () => {
    let fruit = new Fruit(1, 'apple')

    return (
        <div>
            {fruit.name} <----- I am expecting this to update on the DOM when i click the button *********
            <button onClick={() => fruit.changeName('banana')}>
                change the name
            </button>
        </div>
    )
}

export default …
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs es6-class react-typescript

3
推荐指数
1
解决办法
2995
查看次数

try catch语句中的变量范围问题c#

我不想在这里问这个简单的问题,但我已经研究了一段时间而无济于事.它严重限制了我的申请.

为什么我在try块中的excelWorksheet变量上出现红色波浪形(在此范围内无法声明名为"excelWorksheet"的局部或参数,因为该名称用于封闭的本地范围).如果我删除了null声明,那么try catch语句之后的所有'excelWorksheet'实例都会变为红色.非常感谢任何帮助.这是代码:

 Excel.Worksheet excelWorksheet = null;          
        try
        {
            //declare the worksheet variable raw data
            string currentSheet = "Raw Data";
            Excel.Worksheet excelWorksheet = (Excel.Worksheet)ExcelSheets.get_Item(currentSheet);
        }
        catch(Exception r)
        {
            MessageBox.Show("The Raw Data sheet in the report does not exist. " + r);
            return;
        }
Run Code Online (Sandbox Code Playgroud)

c# try-catch excel-interop

0
推荐指数
1
解决办法
310
查看次数