考虑这个模式:
model Comment {
id Int @id @default(autoincrement())
reply Comment? @relation(fields: [replyId], references: [id], onDelete: SetNull)
replyId Int? @default(null)
comment String
}
Run Code Online (Sandbox Code Playgroud)
这里 和reply
都是replyId
可以为空的。当我迁移时,我收到此错误:
error: Error parsing attribute "@default": Expected a numeric value, but received literal value "Null".
--> schema.prisma:70
|
69 | reply Comment? @relation(fields: [replyId], references: [id], onDelete: SetNull)
70 | replyId Int? @default(null)
|
Run Code Online (Sandbox Code Playgroud)
为什么?
我想将经过身份验证的用户放入 zustand 商店中。我使用反应查询获取经过身份验证的用户,这会导致一些问题。我不确定我为什么要这样做。我希望与身份验证相关的所有内容都可以在钩子中访问,所以我认为 zustand 是一个不错的选择。
这是获取 auth 用户的钩子:
const getAuthUser = async () => {
const { data } = await axios.get<AuthUserResponse>(`/auth/me`, {
withCredentials: true,
});
return data.user;
};
export const useAuthUserQuery = () => {
return useQuery("auth-user", getAuthUser);
};
Run Code Online (Sandbox Code Playgroud)
我想将 auth 用户放入该商店:
export const useAuthStore = create(() => ({
authUser: useAuthUserQuery(),
}));
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。出现这种情况可能是由于以下原因之一。
你可以在react文档中阅读它: https://reactjs.org/warnings/invalid-hook-call-warning.html
(为了便于理解,我更改了本文中一些函数的名称。useMeQuery = useAuthUserQuery)
我了解该错误,但我不知道如何修复它。
最近我开始使用 Passport.js 和 oauth2 策略进行身份验证。起初,我使用会话,一切都很棒,但我希望我的 API 是无状态的。我发现唯一可以帮助我解决这个问题的是承载策略(passport-http-bearer)。我找不到任何同时使用这两种策略的好例子,所以我有点迷失。也许我的方式是错误的。让我解释一下我想要做什么。
假设我已经配置了 google 策略(passport-google-oauth2),如下所示:
passport.use(
new GoogleStrategy(
{
clientID: <googleClientId>,
clientSecret: <googleClientSecret>,
callbackURL: `localhost:4000/auth/google/callback`,
scope: ["profile", "email"],
},
async function (_accessToken, _refreshToken, profile, done) { // this is the verify function
let user = <create new user>
return done(null, user);
}
)
);
Run Code Online (Sandbox Code Playgroud)
此路由将用户重定向到谷歌,他们将在其中进行身份验证:
app.get("/auth/google", passport.authenticate("google", { session: false }));
Run Code Online (Sandbox Code Playgroud)
这个处理响应并让用户登录:
app.get(
"/google/callback",
passport.authenticate("google", {
session: false,
})
);
Run Code Online (Sandbox Code Playgroud)
Google 策略发出一个不记名令牌,我想将该令牌返回给用户,以便我可以将其存储在客户端的 localStorage 中,并将其发送到Authorization
每个请求的标头中以对用户进行身份验证。我的第一个问题是如何以及在哪里?我可以访问策略验证令牌中的令牌,但我不知道应该如何在响应正文中将其返回给用户。
我使用承载策略(passport-http-bearer)保护需要经过身份验证的用户的路由。我已经这样配置了:
passport.use(
new BearerStrategy(async function (token, done) { // …
Run Code Online (Sandbox Code Playgroud) authentication node.js oauth-2.0 passport.js passport-google-oauth2
如果过期,该拦截器应该刷新我的令牌。
declare module "axios" {
export interface AxiosRequestConfig {
_retry?: boolean;
}
}
axios.interceptors.response.use(
(res) => res,
(error: AxiosError) => {
const originalRequest = error.config;
console.log(originalRequest._retry); // always undefined
if (error?.response?.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
return axios
.post("/auth/refresh-token", {}, { withCredentials: true })
.then((res) => {
if (res.status === 201) {
return axios(originalRequest);
}
})
}
return Promise.reject(error);
}
);
Run Code Online (Sandbox Code Playgroud)
关键点是_retry
我添加到请求配置中的属性,因此它应该防止无限循环。但事实恰恰相反!它创建了一个无限循环,因为_retry
总是undefined
。
我在他们的 Github 页面上发现了一些问题,但没有找到解决方案。
编辑: Su Yu 使用最新版本的 axios 尝试了此代码,并且它有效。所以也许这是一个错误。我在 …
typescript ×2
axios ×1
node.js ×1
oauth-2.0 ×1
orm ×1
passport.js ×1
prisma ×1
react-hooks ×1
react-query ×1
reactjs ×1
zustand ×1